Getting sum in zend - zend-framework

trying to get value1 * value2
$wTrakcie = $pozycja->select('SUM(ilosc * cenaSprzedazy) AS sum')
->from('pozycja',array())
->join('preorder',array())
->where('preorder.sprzedawca_id = ?',$sprzedawca_id)
->where('preorder.preorder_id = pozycja.preorder_id')
->setIntegrityCheck(false);
$wynik = $pozycja->fetchAll($wTrakcie);
return $wynik->sum;
but I got
Notice: Undefined property: Zend_Db_Table_Rowset::$sum in C:\xampp\htdocs\spms\application\models\DbTable\Pozycja.php on line 29
what's the problem?
query is correct!

Your code should look like:
$wTrakcie = $pozycja->select()
->from('pozycja', array('sum' => 'SUM(ilosc * cenaSprzedazy)'))
->join('preorder', array())
->where('preorder.sprzedawca_id = ?', $sprzedawca_id)
->where('preorder.preorder_id = pozycja.preorder_id')
->setIntegrityCheck(false);
$wynik = $pozycja->fetchRow($wTrakcie);
return $wynik->sum;

Related

change mysqli check exit and insert into pdo statement

i want to change my mysqli check exist and insert into pdo.. my statement look like below:
<?php
$addSearch = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $search);
if ($CheckMusic = $mysqli->query("SELECT * FROM search WHERE term='$addSearch'")) {
$CheckRow = mysqli_fetch_array($CheckMusic);
$CheckId = $CheckRow['id'];
$CheckCnt = $CheckMusic->num_rows;
$CheckMusic->close();
} else {
printf("Error: %s\n", $mysqli->error);
}
$Now = strtotime("now");
$addSearch = $mysqli->escape_string($addSearch);
if ($CheckCnt < 1) {
$mysqli->query("INSERT INTO search (term, datetime) VALUES('$addSearch','$Now') ") or die(mysqli_error());
} else {
$mysqli->query("UPDATE search SET datetime='$Now' WHERE id='$CheckId'") or die(mysqli_error());
}
?>
hope someone can help with sample if can ;q thanks for your advance!
<?php
$addSearch = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $search);
$CheckMusic = $mysqli->query("SELECT * FROM search WHERE term='$addSearch'");
$Now = strtotime("now");
if($CheckMusic->num_rows == 0){
$mysqli->query("INSERT INTO search (term, datetime) VALUES('$addSearch','$Now') ") or die(mysqli_error());
}else{
$CheckRow = mysqli_fetch_array($CheckMusic);
$CheckId = $CheckRow['id'];
$addSearch = $mysqli->escape_string($addSearch);
$mysqli->query("UPDATE search SET datetime='$Now' WHERE id='$CheckId'") or die(mysqli_error());
}
I hope it will work.

sphinx autocomplete CALL SNIPPETS excerpts not returning result set

i am able to get a result set from print_r($stmt->fetchAll()); however, i cannot get a result set from print_r($r); below is the exact code from https://github.com/adriannuta/SphinxAutocompleteExample. the only change is the sql_query inside the sphinx.conf file.
ajax_suggest_excerpts.php
require_once 'common.php';
require_once 'functions.php';
$indexes = 'simplecomplete';
$arr =array();
$q = trim($_GET['term']);
$stmt = $ln_sph->prepare("SELECT * FROM $indexes WHERE MATCH(:match) LIMIT 0,10 OPTION ranker=sph04");
$aq = explode(' ',$q);
if(strlen($aq[count($aq)-1])<3){
$query = $q;
}else{
$query = $q.'*';
}
$stmt->bindValue(':match', $query,PDO::PARAM_STR);
$stmt->execute();
$docs = array();
$title = "";
$stmsnp = $ln_sph->prepare("CALL SNIPPETS(:doc,'simplecomplete',:query)");
$stmsnp->bindValue(':query',$query,PDO::PARAM_STR);
$stmsnp->bindParam(':doc',$title,PDO::PARAM_STR);
print_r($stmt->fetchAll()); //THIS RETURNS RESULT SET
foreach($stmt->fetchAll() as $r){
$title = $r['city'];
$stmsnp->execute();
$r = $stmsnp->fetch();
print_r($r); //THIS DOES NOT.
$arr[] = array('id' => utf8_encode($r['city']),'label' =>utf8_encode( $r['city']));
}
echo json_encode($arr);
exit();
sphinx.conf
source simplecomplete
{
type = mysql
sql_host = localhost
sql_user = swflorib
sql_pass = cii65419
sql_db = swflorib_db
sql_port = 3306 # optional, default is 3306
sql_query = SELECT id, Sic_Code_description, Sic_Code6_Description, City, hidden_keywords FROM no_admin_emails GROUP BY Company_Name
sql_field_string = Sic_Code_description
sql_field_string = Sic_Code6_Description
sql_field_string = City
sql_field_string = hidden_keywords
}
index simplecomplete
{
source = simplecomplete
path = /usr/local/sphinx/var/data/simplecomplete
docinfo = extern
charset_type = utf-8
min_word_len = 3
enable_star = 1
min_prefix_len = 3
}
From the documentation of fetchAll
http://us3.php.net/manual/en/pdostatement.fetchall.php
returns an array containing all of the remaining rows in the result set.
So the fetchAll inside the print_r() fetches all the rows from the database. The fetchAll inside the foreach() then has no rows left to fetch!
Looks like a misunderstanding of PHP/pdo.

Uncaught exception 'MongoCursorException' with message .... duplicate key error index:

Here's my code:
When i try to work it, there's an error as it:
"Fatal error: Uncaught exception 'MongoCursorException' with message
'localhost:27017: E11000 duplicate key error index:
futbol_db.maclar.$kod_1_link_1 dup key:
{ : null, : null }' in /var/www/html/cagkansokmen/futbol/db/maclar.php:182
Stack trace: #0 /var/www/html/cagkansokmen/futbol/db/maclar.php(182):
MongoCollection->insert(Array) #1 {main} thrown in
/var/www/html/cagkansokmen/futbol/db/maclar.php on line 182"
The problem is about insert. Because, when i drop insert code, there's no problem about this code. But if i try to insert data, there's an error just as i said.
How can i fix it?
$collection1 = $db->lig_kod;
$collection_maclar = $db->maclar;
$collection1->ensureIndex(array("kod" => 1, "link" => 1), array("unique" => true, "dropDups" => true));
$a = $collection1->find(array())->limit(15)->skip(0);
foreach($a as $b){
$kod = $b["kod"];
$link = $b["link"];
$parc = explode(',', $link);
$ligkoduson = $parc[0].",".$parc[1];
$url2 = "http://www.stats.betradar.com/s4/gismo.php?&html=1&id=1424&language=tr&clientid=35&state=".$ligkoduson.",5_".$kod.",9_fixtures,231_fixtures,23_1,242_21&child=0";
$url2 = curl($url2);
$url2 = str_replace("\t","",$url2);
$url2 = str_replace("\n","",$url2);
$url2 = str_replace("\r","",$url2);
$bul = ara("<![CDATA[", "]]>", $url2);
$sonuc = ara("setState('", ",", $bul[0]);
$say = count($sonuc);
for($i = 0; $i<$say; $i++){
$sezonbul = $sonuc[$i];
$lonk = "http://www.stats.betradar.com/s4/gismo.php?&html=1&id=2127&language=tr&clientid=35&state=".$ligkoduson.",".$sezonbul.",9_fixtures,231_full,23_1&child=2";
$fiksturlink = curl("http://www.stats.betradar.com/s4/gismo.php?&html=1&id=2127&language=tr&clientid=35&state=".$ligkoduson.",".$sezonbul.",9_fixtures,231_full,23_1&child=2");
$fiksturlink = str_replace("\t","",$fiksturlink);
$fiksturlink = str_replace("\n","",$fiksturlink);
$fiksturlink = str_replace("\r","",$fiksturlink);
$kategori = ara('title="', '"', $fiksturlink);
$kategori_parcala = explode(' > ', $kategori[0]);
$tur = trim($kategori_parcala[0]);
$ulke = trim($kategori_parcala[1]);
$lig = $kategori_parcala[2];
$lig_parcala = explode(' ', $lig);
$lig_bosluk = count($lig_parcala)-1;
$sezon = trim($lig_parcala[$lig_bosluk]);
$lig_son = trim(str_replace($sezon, "", $lig));
$takimlar = ara('<span class="teams">', '</a>', $fiksturlink);
$timebul = ara('<td class="datetime">', '</td>', $fiksturlink);
$fhbul = ara('<td class="p1 ">', '</td>', $fiksturlink);
$ftbul = ara('<td class="nt ftx ">', '</td>', $fiksturlink);
$dongusay = count($takimlar);
echo $dongusay."<br>";
for($dongu = 0; $dongu<$dongusay; $dongu++){
$takimlarbul = ara('">', '</span>', $takimlar[$dongu]);
$home = trim($takimlarbul[0]);
$away = trim($takimlarbul[2]);
$time = trim($timebul[$dongu]);
$time_ayir = explode(' ', $time);
$yil_ayir = explode('/', $time_ayir[0]);
$gun = $yil_ayir[0];
$ay = $yil_ayir[1];
$yil = $yil_ayir[2];
$saat_ayir = explode(':', $time_ayir[1]);
$saat = $saat_ayir[0];
$dk = $saat_ayir[1];
$time_sonuc = mktime($saat, $dk, 0, $ay, $gun, $yil);
$fh = trim($fhbul[$dongu]);
if(empty($fh)){
$fh1 = null;
$fh2 = null;
}else{
$fh_ayir = explode(':', $fh);
$fh1 = $fh_ayir[0];
$fh2 = $fh_ayir[1];
}
$ft = trim($ftbul[$dongu]);
if(empty($ft)){
$ft1 = null;
$ft2 = null;
}else{
if(strpos($ft, '(')){
$parcala1 = explode('(', $ft);
$ft_ayir = explode(':', $parcala1[0]);
$ft1 = $ft_ayir[0];
$ft2 = $ft_ayir[1];
}else{
$ft_ayir = explode(':', $ft);
$ft1 = $ft_ayir[0];
$ft2 = $ft_ayir[1];
}
}
echo $ligkoduson."-".$sezonbul."-".$tur."-".$ulke."-".$lig_son."-".$sezon."-".$home."-".$away."-".$time_sonuc."-".$fh1."-".$fh2."-".$ft1."-".$ft2."<br>";
$collection_maclar->insert(array(
'ulke_kodu'=>$ligkoduson,
'sezon_kodu'=>$sezonbul,
'tur'=>$tur,
'ulke'=>$ulke,
'lig'=>$lig_son,
'sezon'=>$sezon,
'home'=>$home,
'away'=>$away,
'tarih'=>$time_sonuc,
'fh1'=>$fh1,
'fh2'=>$fh2,
'ft1'=>$ft1,
'ft2'=>$ft2
));
}
}
}
You have an unique index on "kod" and "link", but the document you are inserting doesn't include either of these fieldnames.
That means the first document you insert will have these values as null, and the second one will too.. but fails as it violoates the unique index you created.
Note that the "dropDupe" flag you provide with the ensureIndex() command only means "drop existing duplicates", not "if I try to use that key again, drop the previous document".
Your current code seems to be ensuring this index on the "lig_kod" collection, but I suspect you may have previously (maybe accidentally) used the $collection_maclar variable, rather then the $collection1 variable and executed the code, resulting in creating the index on the maclar collection.
-Hannes

Querying for dates in doctrine

I have a table called active_plans in which there are two columns: activated_date and last_billed_at. Basically, I want to create a query that looks at these two columns like this:
select all from active_plans
where last_billed_at = null AND activated_date + 1 month = today,
or if last_billed_at + 1 month = today.
I can't seem to figure out how to do this, though.
I wound up doing something a lot different:
public function getBillToday()
{
$activePlans = $this->entityManager
->createQuery('SELECT adp FROM adp
WHERE adp.activationDate IS NOT NULL')
->getResult();
$data = array();
foreach ($activePlans as $plan) {
$recur = $plan->getPlan()->getRecurringInMonths();
$nextBillTime = strtotime('-'.$recur.' months', time());
$nextBill = date('Y-m-d', $nextBillTime);
$activationDate = $plan->getActivationDate();
$lastBilledAt = $plan->getLastBilledAt() != NULL ? $plan->getLastBilledAt()->format('Y-m-d') : 0;
if ($activationDate == $nextBill || $lastBilledAt == $nextBill) {
$data[] = $plan->getId();
}
}
return $data;
}

Propel: what is the equivalent of OR in SQL?

what is the method to create OR?
I mean: I know to craate this SQL clause:
SELECT * FROM author WHERE author.FIRST_NAME = 'Karl' AND author.LAST_NAME <> 'Marx';
I should do this:
<?php
$c = new Criteria();
$c->add(AuthorPeer::FIRST_NAME, "Karl");
$c->add(AuthorPeer::LAST_NAME, "Marx", Criteria::NOT_EQUAL);
$authors = AuthorPeer::doSelect($c);
But if i want to create:
SELECT * FROM author WHERE author.FIRST_NAME = 'Karl' OR author.LAST_NAME <> 'Marx';
what should i do?
Regards
Javi
$c = new Criteria();
$cton1 = $c->getNewCriterion(AuthorPeer::FIRST_NAME, "Karl");
$cton2 = $c->getNewCriterion(AuthorPeer::LAST_NAME, "Marx", Criteria::NOT_EQUAL);
$cton1->addOr($cton2);
$c->add($cton1);
Check orWhere(), combine(), and the new _or() operator:
http://www.propelorm.org/wiki/Documentation/1.5/ModelCriteria#CombiningSeveralConditions
http://propel.posterous.com/using-or-in-propel-queries-becomes-much-easie