fatal error in php and mysql - php

i am having a problem with my script in php/mysql. here is the error displayed by the server:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'if exists (select * from notificacoes where uid in () order by id desc' at line 1' in C:\wamp\www\bigui\classes\Notificacoes.class.php on line 57
and here is my php code:
static function listar(){
$strIdAmigos = Amizade::$strIdAmigos;
$query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc');
return $query->fetchAll(PDO::FETCH_ASSOC);
}
my table in the mysql is empty, with no values. when i insert a value in it, the error goes away and everything is fine. any help?

If $strIdAmigos is empty, it causes syntax errors.
Before you execute this query, you should check the $strIdAmigos value whether it's empty or not to avoid this issue. Not to forget to escape the values if needed.

When you run your query with nothing in the variable $strIdAmigos, it will error out.
Try initializing and/or checking your variable, $strIdAmigos, before running your query:
$strIdAmigos = "";
if (empty($strIdAmigos)) {
/* Uh oh, throw an error */
} else {
$query = self::getConn()->query('select * from notificacoes where uid in ('.$strIdAmigos.') order by id desc');
}
Note that if $strIdAmigos = "0" , the empty($strIdAmigos) will still evaluate to true and, hence, will NOT run the query.

Related

PDOException in MySQL 5.7

I'm getting:
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MySQL server version for the right syntax to use near 'LIMIT 1' at
line 1'
I'm trying to upgrade some code from MySQL 5.6 to 5.7 and I'm not sure how to rewrite this?
public function getPlayerInfo($uid){
$this->session->newQuery();
$sqlSelect = "SELECT COUNT(*) AS total, login, gameIP, homeIP, gamePass, email FROM users WHERE id = $uid LIMIT 1";
$data = $this->pdo->query($sqlSelect)->fetch(PDO::FETCH_OBJ);
if($data->total == 0){
exit();
}
To clarify what this does, it's supposed to return relevant player data, for example, further down in this file, I have:
if($doomStats['DOOM']['clanID'] == 0){
$doomedBy = ''.self::getPlayerInfo($doomStats['DOOM']['creatorID'])->login.'';
} else {
$clan = new Clan();
$clanInfo = $clan->getClanInfo($doomStats['DOOM']['clanID']);
$doomedBy = ''.$clanInfo->name.'';
$doomedBy .= ' <span class="small nomargin">(Released by '.self::getPlayerInfo($doomStats['DOOM']['creatorID'])->login.')</span>';
}
I hope this clarifies.
using count without an aggregate function? eg: groupby also put $uid in single quotes

mysql fulltext stripslashes not working

I want to perform this mysql search :
SELECT ida, MotsClef FROM Actes WHERE MATCH (MotsClef )
AGAINST ('+"dette" +"plège"' IN BOOLEAN MODE);
Using php, I use regular expressions to add the +" and " to the expressions received via $_POST so a var_dump gives :
'motcle' => string '+"dette" +"plège"'
So that's fine too. However, I use prepared statements using PDO class and I have this piece of code for that:
if($r['motcle']!=''){
$motclef = $r['motcle'];
$demMotsClef = " AND WHERE MATCH (MotsClef ) AGAINST (:motsclef IN BOOLEAN MODE) ";
}
else{
$demMotsClef='';
}
than:
$f = "SELECT COUNT(*) FROM Actes, Bibliographie WHERE id = idBiblio".$demMotsClef;
$demande = $this->prepare($f);
if($r['motcle']!=''){$demande->bindValue(':motsclef',stripslashes($motclef));}
$demande->execute(); //the error is on this line//
I get a MySQL error message saying I have an error in your SQL syntax:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]:
Syntax error or access violation: 1064 You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near 'WHERE MATCH (MotsClef ) AGAINST
('+\"dette\" +\"plège\"' IN BOOLEAN MODE) AND a' at line 1' in
/Library/WebServer/Documents/messources/actions.class.php on line 547.
The error in mysql syntax is that the slashes are added, hence the use of stripslashes (doesn't work).
Any idea on how to solve that - I would rather not change ini settings either in php.ini or in a .php function since that would mess up all my other mysql requests.
Thanks!
Ohh, well took me a while to find the error but this is definetly wrong:
$demMotsClef = " AND WHERE MATCH (MotsClef ) AGAINST (:motsclef IN BOOLEAN MODE) ";
$f = "SELECT COUNT(*) FROM Actes, Bibliographie WHERE id = idBiblio".$demMotsClef;
If you look at this, you'll have double WHERE, which is not allowed, you should make this change:
$demMotsClef = " AND MATCH (MotsClef ) AGAINST (:motsclef IN BOOLEAN MODE) ";

Yii CDbCommand failed to execute the SQL statement: SQLSTATE[42000]:

I get this error when I didn't do anything for a while, I'm not sure if this is a Session problem or not.
The error message is:
CDbCommand failed to execute the SQL statement: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '=='1')' at line 1. The SQL statement executed was: SELECT * FROM games_developers_app t WHERE (status LIKE :ycp0) AND (developer_id==:ycp1)
The code is:
public function actionSortReject(){
$util = new Utility();
$util->detectMobileBrowser();
$util->checkWebSiteLanguageInCookies();
$this->layout = "masterLayout";
$count = '0';
$id = Yii::app()->user->getState('id');
$searchsort = "REJ";
$sort = new CDbCriteria();
$sort->addSearchCondition('status', $searchsort);
$sort->addCondition('developer_id='.$id);
$models = GamesDevelopersApp::model()->findAll($sort,array('developer_id'=>$id));
$this->render('/register/applist',array('models'=>$models,'count'=>$count));
}
It seems that everything worked fine, if I missed something in my code please tell me. Thanks =)
The problem is a combination how you have called compare and added additional parameters to findAll.
Your compare should be as follows:
$sort->compare('developer_id', $id);
And your findAll should be:
$models = GamesDevelopersApp::model()->findAll($sort);
You could also use addCondition as follows:
$sort->addCondition('developer_id=:developer_id');
$sort->params[':developer_id'] = $id;

Why does Explicitly putting Null on sql statement yields SQLSTATE[42000] error?

Because putting NULLs inside the variables gives headache here in PHP, I resorted to explicitly putting NULL on the prepared statement
The script is a csvupload script originally came from here Import CSV into MySQL
$linemysql = implode("','",$linearray);
$linemysql = "'".$linemysql."'";
$sql="SELECT * FROM `".$tblmei."` WHERE `".$shuHint."` = ".$linearray[0];
$stmt = $setsu->query($sql);
$rwCnt=$stmt->rowCount();
if ($rwCnt==0){
$fumeiKazu=substr_count($linemysql,"'Unknown'");
echo "<br>fumeiKazu=".$fumeiKazu;
if ($fumeiKazu==1)
{
$fumeiPos=mb_strpos($linemysql,"'Unknown'");
$l1=mb_substr($linemysql,0, $fumeiPos);
echo "<br>l1=".$l1;
$sfumeiPos=$fumeiPos+9;
echo "<br>sfumeiPos=".$sfumeiPos;
$l2=mb_substr($linemysql,$sfumeiPos);
echo "<br>l2=".$l2;
echo "<br>".$l1.NULL.$l2;
$tsuika = $setsu->prepare("INSERT INTO ".$tblmei." VALUES (".$l1.NULL.$l2.")");
$tsuika->execute();
$dataHaitaKazu++;
}
}
The idea of this php script block is when it finds Unknown, post it as NULL as the row's Risk during query
I made sure the the Risk column in the table structure phpmyadmin accepts null and default is null.
This is what I came up
$tsuika = $setsu->prepare("INSERT INTO ".$tblmei." VALUES (".$l1.NULL.$l2.")");
And it yiedls this error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ',,'testArea','0')' at line 1' in

Erreur : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

when i try to execute an update statement i got the following error :
Erreur : SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Issy-les-Moulineaux ' where ssiphone_idstation=46' at line 1
my update statement is :
$bdd->exec("update ssiphone_stationdeservice set $cle='$element' where ssiphone_idstation=$id");
this is in a php code, THX in advance for your help :)
$cle and $element are in array, my code is :
foreach($table1 as $cle => $element)
{
$bdd->exec("update ssiphone_stationdeservice set $cle='$element' where ssiphone_idstation=$id");
}
now table1 is an array which contain the columns name of my table and its values :
$table1=array();
$table1['ssiphone_etatstation']=$etat;
$table1['ssiphone_commerce']=$commerce;
$table1['ssiphone_stationdelavage']=$lavage;
$table1['ssiphone_typescarburants']=$lescarburants;
$table1['ssiphone_joursdelasemaine']=$jourssemaines;
$table1['ssiphone_horaires ']=$this->horaires;
$table1['ssiphone_telephone ']=$telephone;
$table1['ssiphone_sensdecirculation ']=$this->sensDeCirculation;
$table1['ssiphone_adresse ']=$this->adresse;
$table1['ssiphone_ville']=$this->ville;
$table1['ssiphone_departement']=$this->departement;
$table1['ssiphone_nomstation ']=$this->nomStation;
Most likely your $cle variable isn't set, making the query look like:
... set ='Issy-les-moulineaux ' where ...
comment followup:
Change your code to look like this, then:
$query = "update ssiphone_stationdeservice set $cle='$element' where ssiphone_idstation=$id";
$result = $bdd->exec($query);
if ($result === FALSE) {
print_r($bdd->errorInfo());
die("Query: " . $query);
}
This way you have the complete query string in a variable you can inspect (e.g. by echoing out). Obviously there's something wrong with the query - but the mysql error string doesn't show the entire query, so you have to take measures to capture it.

Categories