I'm new at this, what are the problems with this statement:
$sql=" SELECT * FROM `calendar` WHERE `DayId` ='".$day."'";
$result = mysql_query($sql, $conn);
if (!$result){
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysql_error();
exit;
}
while ($row = mysql_fetch_array($result)) { //set $dayType
$dayType = $row[DayType];
}
I keep getting the error:
DB Error, could not query the database
MySQL 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 '' at line 1
but when I put an "echo $result;" in after the line that starts with $result=... then I get a value for $result of "Resource id #2"
You need to enclose your "day" variable in quotes (and you should be escaping it if you haven't already!)
$sql = "SELECT * FROM calendar WHERE DayId = '" . mysql_real_escape_string($day) . "'";
Shouldn't it be
$sql="SELECT * FROM `calendar` WHERE `DayId` = '".$day."'";
It seems likely to me that your $day variable is not getting populated ... Try echoing the SQL statement before you run it to make sure everything looks as it should ...
If it's date(z) change it to date('z').
Related
I get an error with my PHP code when updating the table patient. I cannot find the problem.
Here is my error:
Verification 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
<?php
$edit = mysql_query("UPDATE `patient` SET `date`='$date', `fname`='$fname', `lname`='$lname', `birthday`='$dob', `address`='$address', `work`='$work', `civil`='$civil', `gender`='$sex', `btype`='$bloodtype', `height`='$hgt', `weight`='$wgt', `fallergy`='$fallergy', `mallergy`='$mallergy' WHERE `patientid`='$vara'");
$result = mysql_query($edit) or die("Verification Error: " . mysql_error());
You are calling mysql_query twice; the second time you pass the result, of the first call, into it as an argument. That is not how mysql_query works. The SQL should just be a string:
$edit = "UPDATE `patient` SET `date`='$date', `fname` ...";
$result = mysql_query($edit) or die("Verification Error: " . mysql_error());
We cannot see the rest of your code, so we do not know if there are more problems, but this should fix the problem in your question.
My code is throwing this error:
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 '-contact-info' at line 1
my code:
<?php
//connect
$connection = mysqli_connect("myh","myu","myp","mydb") or die("Error " . mysqli_error($connection));
//consultation:
$query = "SELECT * FROM web-contact-info";
//execute the query.
$result = mysqli_query($connection, $query);
if (!$result) {
printf("Error: %s\n", mysqli_error($connection));
exit();
}
//display information:
while($row = mysqli_fetch_array($result)) {
echo $row["live_name"] . "<br>";
}
?>
I've tried to put quotes around web-contact-info and get a slightly different error:
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 ''web-contact-info'' at line 1
What am I writing wrong?
You can try this:
SELECT * FROM `web-contact-info`
As mysql_* is deprecated consider switching to mysqli or PDO.
Try and use the name of the table within simple quotes like this
$query = "SELECT * FROM `web-contact-info`";
This question already has an answer here:
MySQL syntax error: near '1' at line 1
(1 answer)
Closed 1 year ago.
I'm making a website for the first time using PHP with a MySQL database and getting a syntax error that I don't understand. I just want to delete everything from tblreparation with a certain ID and also everything from rblrepstat with that same ID. I am using this code:
<?php
$con=mysqli_connect("localhost","root","MYPASS","repair");
$ID = $_REQUEST['ID'];
$sql = mysqli_query($con, "DELETE FROM tblreparation WHERE ID = {$ID}");
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
$sql = mysqli_query($con, "DELETE FROM tblrepstat WHERE repID = {$ID}");
if (!mysqli_query($con,$sql)) {
die('Error2: ' . mysqli_error($con));
}
echo "1 record deleted";
mysqli_close($con);
?>
And this is the error I am getting:
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
Near 1? I don't even see a '1'...
You're calling mysqli_query twice for each query.
The second time you call it you're actually passing a resource as the query parameter, which causes the error you're getting.
Try changing your code to this:
<?php
$con=mysqli_connect("localhost","root","MYPASS","repair");
$ID = $_REQUEST['ID'];
$sql = "DELETE FROM tblreparation WHERE ID = {$ID}";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
$sql = "DELETE FROM tblrepstat WHERE repID = {$ID}";
if (!mysqli_query($con,$sql)) {
die('Error2: ' . mysqli_error($con));
}
echo "1 record deleted";
mysqli_close($con);
?>
Your $ID might contain 1. Which also the location of the error in the query.
DELETE FROM tblreparation WHERE ID = $ID
Or if ID is containing a string or stored in another format which is string related (varchar) then wrap it in quotes:
DELETE FROM tblreparation WHERE ID = '$ID'
My Code looks like below.
$var = 'ID="'. mysql_real_escape_string($data[0]).'" AND SYS="'.mysql_real_escape_string($data[2]). '" AND TITLE="'.mysql_real_escape_string($data[1]).'"';
$sql = 'SELECT * FROM `table_name` WHERE '. $var;
$result = mysql_query($sql);
In the where condition, TITLE when using a single quote(') I am facing the below error even though the mysql_real_escape_string() function is being used.
The error thrown is
Resource id #5You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 's Created', 'Test', 'Test', '0000-00-00 00:00:00', ' at line 25
after your dumping looks like you have problem with apostroph
you may change your quotes like that
$var = "ID='". mysql_real_escape_string($data[0])."' AND SYS='".mysql_real_escape_string($data[2]). "' AND TITLE='".mysql_real_escape_string($data[1])."' ";
$sql = "SELECT * FROM `table_name` WHERE ". $var;
$result = mysql_query($sql);
$finalvar=stripslashes($var);
$sql = 'SELECT * FROM table_name WHERE '. $finalvar;
Try dumping your SQL query in its compete form right before it is sent.
You'll be able to spot the error that way.
I'm getting this error:
Invalid 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 'INET_ATON('188.92.x.x')' at line 1
While trying to insert IP Address in database. The column type is:
'LastIP int(10) unsigned NOT NULL,'.
The function to execute the query is:
function onNewUser($ip, $hostname, $con)
{
$query = "INSERT INTO tableMachine (LastIP, LastHostName) VALUES ".
"INET_ATON('".mysql_real_escape_string($ip, $con)."'), ".
"'".mysql_real_escape_string($hostname, $con)."'";
$result= mysql_query($query, $con);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
}
I call this function with the parameters:
$ip = $_SERVER['REMOTE_ADDR'];
$hostname = #gethostbyaddr($ip);
onNewUser($ip, $hostname, $con);
What's wrong with it guys?
your values list should be encapsulated inside of parenthesis if I am not mistaken
You should try this :
$query = "INSERT INTO tableMachine (LastIP, LastHostName) VALUES (".
"INET_ATON('".mysql_real_escape_string($ip, $con)."'), ".
"'".mysql_real_escape_string($hostname, $con)."')";
I just add parenthesis for VALUES(...)
Also, as #Shamil said, the functions mysql_* are depricated. You should use mysqli_*This link should help you with the mysqli_* functions.