Selecting MySQL query via PHP variables - php

I am getting zero in $booked_num, I tried the query in SQL with values in place of variables, it worked fine. But I don't know where I am making a mistake, please help.
I already echoed every variable and everything is fine, but nothing is there in $booked_rowand $booked_num is echoing zero.
require_once 'mysql_connector.php';
$booked_result = mysql_query('select * from booked where train_no = ".$train_no." and date = ".$date." and st_from = ".$st_from." and st_to = ".$st_to.";') or die(mysql_error()) ;
$booked_num = mysql_num_rows($booked_result);
echo $booked_num;
$booked_row = mysql_fetch_array($booked_result,MYSQL_ASSOC);
print_r($booked_row);

$booked_result = mysql_query('select * from booked where train_no = ".$train_no." and date = ".$date." and st_from = ".$st_from." and st_to = ".$st_to.";') or die(mysql_error()) ;
This syntax is incorrect - you need to close the string before concatenating variables.
Something like:
$booked_result = mysql_query('select * from booked where train_no = "' .$train_no. '" and date = "' .$date. '" and st_from = "' .$st_from. '" and st_to = "' .$st_to. '";') or die(mysql_error());
Also, you should consider switching to the PDO library. Among other things, it will help you avoid sql injection attacks in your queries.

Related

Records exists but not found by query

I'm making a query to my database, but the query always return empty. If I copy the query and paste it directly to mysql, it show one record, as it should be.
This is my query in php:
$sql = "SELECT * FROM disponibilities WHERE st_week = '" .
$st_week . "' AND en_week = '" . $en_week . "' AND boat = '" .
$_POST['boat'] . "'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
$disponibility = $row;
}
} else {
echo "0";
echo $_POST['boat'];
}
And this is the query (echo of the previous query) that show one record if used in phpmyadmin:
SELECT * FROM disponibilities WHERE st_week = '2021-11-13' AND en_week = '2021-11-20' AND boat = 'sapphire'
I've tried converting the date in this way, but still don't work
$sql= "SELECT * FROM disponibilities WHERE st_week =
STR_TO_DATE('$st_week','%Y-%m-%d') AND en_week = STR_TO_DATE('$en_week','%Y-%m-%d')";
EDIT:
this is my phpmyadmin query response:
Thanks in advance
try to run
SELECT * FROM disponibilities WHERE st_week = '2021-11-13' AND en_week = '2021-11-20' AND boat = 'sapphire';
this query from your code what i mean is that run the query with static values instead of variables and see if it returns anything if it does then you are getting wrong value in your variables...
let me if this is the problem if not also so that i can help you further...

updating mysql database with php variable

I want to update my mysql database, using php using variable method but it is not updating. I don't know what the problem is. This is my code:
$result = mysql_query("SELECT * FROM total") or die(mysql_error());
$i=$row['number'];
$n=$i+1;
$result = mysql_query("UPDATE total SET number = " . $n . " WHERE number = " . $i . "") or die(mysql_error());
How can I update my mysql database using php?
You can increment the column_value like this column_name = column_name + 1 without using SELECT.
UPDATE total SET number = number + 1
It can be just with SQL without need of select. When it is not required don't use php. What can be done in mysql should be done in mysql. It's faster.
UPDATE `total` SET number = number + 1;
Moreover, you should read the red box on mysql_* documentation. These functions are depracated and will be removed in future. Consider using MYSQLI or PDO
your query syntax is wrong, try this,
$result = mysql_query("UPDATE total SET number = '" . $n . "' WHERE number = '" . $i . "'");
The syntax fo your query is wrong it should be
UPDATE `total` SET number = number + 1;
you have done
UPDATE `total` S number = number + 1;
refer this mysql doc

mysql query works with plain text, but not with variable

I am trying to print out some topic information, but it is not going so well. This is my query:
SELECT * FROM topics WHERE id='$read'
This doesn't work. I've echo'ed the $read variable, it says 1. So then if I do like this:
SELECT * FROM topics WHERE id='1'
It works perfectly. I don't get what is the problem. There's no hidden characters in $read or anything else like that.
Try like this:
$query = "SELECT * FROM topics WHERE id='" . $read . "'"
ID is normally a numeric field, it should be
$id = 1;
$query = "SELECT * FROM topics1 WHERE id = {id}"
If you are using strings for some reason, fire a query like
$id = '1';
$query = "SELECT * FROM topics1 WHERE id = '{$id}'"
SELECT * FROM topics WHERE id=$read
it consider it as string if you put i single quotes
I wonder why all the participants didn't read the question that clearly says that query with quotes
SELECT * FROM topics WHERE id='1'
works all right.
As for the question itself, it's likely some typo. Probably in some other code, not directly connected to $read variable
try
$query = sprintf("SELECT * FROM topics WHERE id='%s';",$read);
Also remember to escape the variable if needed.
Looks like you might have an issue with the query generation as everyone else is pointing to as well. As Akash pointed out it's always good to build your query in to a string first and then feed that string to the MySQL API. This gives you easy access to handy debugging techniques. If you are still having problems try this.
$id = 1;
$query = "SELECT * FROM `topics1` WHERE `id`={$id}";
echo ": Attempting Query -> {$query}<br />";
$res = mysql_query($query, $dblink);
if($res <= 0)
die("The query failed!<br />" . mysql_error($dblink) . "<br />");
$cnt = mysql_num_rows($res);
if($cnt <= 0)
{
$query = "SELECT `id` FROM `topics1`";
echo "No records where found? Make sure this id exists...<br />{$query}<br /><br />";
$res = mysql_query($query, $dblink);
if($res <= 0)
die("The id listing query failed!<br />" . mysql_error($dblink) . "<br />");
while($row = mysql_fetch_assoc($res))
echo "ID: " . $row['id'] . "<br />";
}
This will at least let you monitor between calls, see what your query actually looks like, what mysql says about it and if all else fails make sure that the ID you are looking for actually exists.
try with this : SELECT * FROM topics WHERE id=$read

Mysql_fetch_array supplied argument is not a valid MYSQL result

How do I fix the "Mysql_fetch_array supplied argument is not a valid MYSQL result" error?
This is the code that had the error:
<?php
require "connect.php";
$query = mysql_query("SELECT author, date
FROM articles
WHERE id = ' . $id . '") or die(mysql_error());
$runrows = mysql_fetch_array('$query');
$author = $runrows['author'];
$date = $runrows['date'];
?>
$query = mysql_query("SELECT author, date FROM articles WHERE id = ' . $id . '") or die(mysql_error());
$runrows = mysql_fetch_array($query);
There should be no quotes for $query. Its a variable, not a string.
$runrows = mysql_fetch_array('$query');
Should be
$runrows = mysql_fetch_array($query);
or
$runrows = mysql_fetch_array("$query");
Using '$variable' just prints out $variable as text. You want to use the value of the variable.
SQL Injection is when you use that $id variable from user interaction.
E.g. you have the following URL:
http://example.com/articles.php?id=1
If you just add the id variable to your query people can inject code in the variable.
http://en.wikipedia.org/wiki/SQL_injection
To prevent this you can simply do:
$id = mysql_real_escape_string($id);
Or use prepared statements if it is supported.
http://en.wikipedia.org/wiki/Prepared_statements#Parameterized_statements
Remove the dots and use something like this in the third line
$query = mysql_query("SELECT author, date FROM articles WHERE id = '$id'") or die(mysql_error());
This has happened to me when I had not closed the connection using mysql_close($conn). I guess the socket between from the db was still open, and next time when i again connected to the same db, this particular error came. There were no other issues. Once I explicitly closed the connection and then again restarted the connection, this issue was resolved. Hope it helps.

php mysql update blob

New to PHP/mySql and having trouble inserting and retrieving binary data. I have a mySql table called usr_pressdata. The field 'BinDat' is of type mediumblob.
$dat = $this->parseOverview($sql);
// $dat is now a binary string
$datsql = "Update usr_pressdata Set BinDat = " . $dat;
$datresult = mysql_query($datsql, $this -> conn) or die(mysql_error());
$getdat = "Select * from usr_pressdata";
$getdatresult = mysql_query($getdat, $this -> conn) or die(mysql_error());
$row = mysql_fetch_array( $getdatresult );
$retval = $row['BinDat'];
In this example my goal is that $retval == $dat but it does not. I suspect that my query string $datsql is incorrect. Can someone correct this example code? Thank you.
When inserting values in a table (or more generally, when including a value in an SQL request):
the string must be enclosed between quotes ('...')
the string must be “escaped” using mysql_real_escape_string so as to prevent SQL injection.
So you need to write something like:
$request = "UPDATE usr_pressdata SET bindat= '" . mysql_real_escape_string($dat) . "';";
I suspect you may want to add a WHERE someColumn = someCondition clause at the end, because as it is now, it would affect all the rows in the table.

Categories