SET and SELECT query not executing when run in PHP code - php

I have this php code -:
$q = "SET #session = '1', #buddys = '12,7,10', #rejects = 'post_0'; SELECT f.* FROM feed as f"; $r = mysqli_num_rows($q);.
This results in this error -: 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 'SELECT f.* FROM feed as f' at line 3.
But surprisingly, when the same mysql query is run in phpmyadmin it runs as needed.
Whats the matter? Thanks for help...

your problem is the semicolone ; before select
try replace it by comma ,
like that
$q = "SET #session = '1', #buddys = '12,7,10', #rejects = 'post_0' , SELECT f.* FROM feed as f";
$r = mysqli_num_rows($q);
or you ould also separate your query like that
$q = "SET #session = '1', #buddys = '12,7,10', #rejects = 'post_0' ";
$q .= "SELECT f.* FROM feed as f";

Related

UPDATE Syntax in mysql 5.7

Im trying to run a simple UPDATE query:
UPDATE students SET enterTime = '".$enterTime."' WHERE s_id = '".$s_id."'; UPDATE timeLimit SET listed = listed + 1 WHERE enterTime = '".$enterTime."' AND building = '".$building."';"
It works fine in MySql version 5.1 , my server has been upgraded to MySql version 5.7 and I get the following error:
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 'UPDATE students SET enterTime = '09:00' WHERE ' at line 1
BEGIN; UPDATE students SET enterTime = '09:00' WHERE s_id = '312433931' ; UPDATE timeLimit SET listed = listed + 1 WHERE enterTime = '09:00' AND building = '2'; UPDATE timeLimit SET listed = listed - 1 WHERE enterTime = '08:00' AND building = '2'; COMMIT;
PHP code:
function update_time_for_student($student,$enterTime,$oldTime){
$s_id = $student['s_id'];
$building = $student['building'];
$query = "BEGIN; UPDATE students
SET enterTime = '".$enterTime."'
WHERE s_id = '".$s_id."' ;
UPDATE timeLimit
SET listed = listed + 1
WHERE enterTime = '".$enterTime."' AND building = '".$building."';
UPDATE timeLimit
SET listed = listed - 1
WHERE enterTime = '".$oldTime."' AND building = '".$building."'; COMMIT;";
if (mysql_query($query)){
$fullName = $student['fname']." ".$student['lname'];
send_email($student['email'],$enterTime,$fullName,$s_id);
header("Location:/index.php?d=1");
} else {
echo mysql_error();
echo "<br/>";
echo $query;
//header("Location:/index.php?d=3");
}
}
Thank you very much for your help !
Option-1
Try breaking the lines into multiple php statements:
$query = "BEGIN";
mysql_query($query) or die (mysql_error());
$query = "UPDATE students SET enterTime = '".$enterTime."' WHERE s_id = '".$s_id."';";
mysql_query($query) or die (mysql_error());
$query = "UPDATE timeLimit SET listed = listed + 1 WHERE enterTime = '".$enterTime."' AND building = '".$building."';";
mysql_query($query) or die (mysql_error());
$query = "COMMIT";
mysql_query($query) or die (mysql_error());
Option-2
you need to use multi_query instead. Docs here
Note: mysql_* commands are deprecated and removed in PhP7. Instead use mysqli or PDO. Refer the accepted answer here

Same query works different

I made php script that updates database:
<?php
include 'config.php';
$PauseID = "2";
$ProductionID = "1411979966";
$sql = "SET #max = (SELECT MAX(Id) FROM tblproductionbreaks); UPDATE tblproductionbreaks SET IDPause = '$PauseID' WHERE ProductionID = '$ProductionID' AND Id = #max;";
mysql_query($sql) or die(mysql_error());
mysql_close($connect);
?>
While executing this script it returns error:
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 'UPDATE tblproductionbreaks SET IDPause = '2' WHERE ProductionID
= '1411979966' A' at line 1
But if I try same update query execute by command line it works
SET #max = (SELECT MAX(Id) FROM tblproductionbreaks); UPDATE tblproductionbreaks SET IDPause = '2' WHERE ProductionID = '1411979966' AND Id = #max;
I don't understand how the same thing works differently.
You can not run multiple queries as one statement with PHP. Try like this:
$sql = "UPDATE tblproductionbreaks SET IDPause = '$PauseID' WHERE ProductionID = '$ProductionID' ORDER BY Id DESC LIMIT 1;";
You don't need this variable anyway.
Its is because your PHP interpreter treats your $sql as string. Change your code to
$sql = "UPDATE tblproductionbreaks SET IDPause = '{$PauseID}' WHERE ProductionID = '{$ProductionID}' AND Id = (SELECT MAX(Id) FROM tblproductionbreaks)";
Also you can debug it by echo $sql and see what actually the $sql returning

Error: You have an error in your SQL syntax; check the manual that corresponds .. syntax to use near '1' at line 1

Error: 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 code is a follows where i get the error and that is come going from http://cms2.br-de.tk/editinfo.php to http://cms2.br-de.tk/updateinfo.php
<?php
mysql_connect("mysql10.000webhost.com","******_12","*******") or die("Error:".mysql_error());
mysql_select_db("******_1");//add your dbname
//get the variables we transmitted from the form
$Title = $_POST['Title'];
$Author = $_POST['Author'];
$Date = $_POST['Date'];
$Content = $_POST['Content'];
//replace TestTable with the name of your table
//replace id with the ID of your user
$sql = "UPDATE `posts` SET `Tilte` = '$Tilte',`Author` = '$Author',`Date` = '$Date',`Content` = '$Content' WHERE `posts`.`ID` = '$ID' 1 ";
mysql_query($sql) or die ("Error: ".mysql_error());
echo "Database updated. <a href='editinfo.php'>Return to edit info</a>";
?>
You have added additional 1 at the end of query. It should be like this:
$sql = "UPDATE `posts` SET `Tilte` = '$Title',`Author` = '$Author',`Date` = '$Date',`Content` = '$Content' WHERE `posts`.`ID` = '$ID'";
You have a spare 1 at the end of your statement.
UPDATE `posts` SET `Tilte` = '$Title',`Author` = '$Author',`Date` = '$Date',`Content` = '$Content' WHERE `posts`.`ID` = '$ID';"
As Grigore correctly spotted, you might also have a typo in your statement depending on your column names.
UPDATE `posts` SET `Title` = '$Title',`Author` = '$Author',`Date` = '$Date',`Content` = '$Content' WHERE `posts`.`ID` = '$ID';"
`Tilte` = '$Title'
maybe this is title not tilte, besides that there's a "1" right at the ending of the query

Mysql syntax error issue

I've got a message while i'm run following sql query...
"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 'group = 'dfdfd' WHERE id = '39'' at line 1"
Sql query:
$sql_update = mysql_query("UPDATE addcontacts SET surename = '$surname_g', group =
'$g_g' WHERE id = '$id'");
Please use ` to enclose group, it is being treated as special (group by keyword of SQL) by mysql
Use the following:
UPDATE addcontacts SET surename = '$surname_g', `group` = '$g_g' WHERE id = '$id'
Note `group` and not group
Try:
$sql_update = mysql_query("UPDATE addcontacts SET surename = '".$surname_g."', `group` = '".$g_g."' WHERE id = '".$id."'");
Your id might be an integer and you are enclosing it with two single quotes (') and that would really produce the error.
$sql_update = mysql_query("UPDATE addcontacts SET surename = '{$surname_g}', group =
'{$g_g}' WHERE id = {$id}");
Thank you :)

mysql syntax error on performing a ranking query

i want to perform a ranking query but i'm not sure what is the right syntax
here is my query:
static public function sortranks(){
global $db;
$sql ="TRUNCATE TABLE `ranking`";
$db->query($sql);
$sql = "INSERT INTO `ranking` (`user_id`) VALUES
( SELECT `employe_id` FROM `rates_employe` WHERE `status` = '0' ORDER BY rawpoint DESC ) ";
$db->query($sql);
$sql = "UPDATE rates_employe , ranking SET rates_employe.rank = ranking.rank WHERE
rates_employe.employe_id = ranking.user_id ";
$db->query($sql);
echo 'ok';
exit;
}
i keep getting syntax error when i run this query
Database query failed: 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 'SELECT employe_id FROM rates_employe WHERE
status = '0' ORDER BY rawpoint ' at line 2
You should execute the queries one by one, instead of joining them together.
Change the .= string concatenation with a simple assignment, and after assigning each query, execute it. Such as:
$sql = "TRUNCATE TABLE ranking";
$db->query($sql);
$sql = "INSERT INTO `ranking`(`user_id`) VALUES ...";
$db->query($sql);
Also remove VALUES from the query:
INSERT INTO ranking (user_id)
( SELECT employe_id FROM rates_employe WHERE status = '0' ORDER BY rawpoint DESC )
Sorry if I sound pedantic, but in English Employee is spelt with two e at the end of the word.
Try running each queries separately:
static public function sortranks(){
global $db;
$sql ="TRUNCATE TABLE ranking";
$db->query($sql);
$sql = "INSERT INTO `ranking`(`user_id`) VALUES
( SELECT `employe_id` FROM `rates_employe` WHERE `status` = '0' ORDER BY `rawpoint` DESC ) ";
$db->query($sql);
$sql = "UPDATE rates_employe , ranking SET rates_employe.rank = ranking.rank WHERE
rates_employe.employe_id = ranking.user_id ";
$db->query($sql);
echo 'done';
exit;
}

Categories