0

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);
2
  • You're mixing single and double quotes. You cannot start a string with a single quote and end it with a double quote. Commented Apr 14, 2013 at 15:57
  • I'd avoid using mysql_* functions, as of PHP 5.5.0 they are deprecated. You should considering using mysqli or PDO. Commented Apr 14, 2013 at 16:02

1 Answer 1

2
$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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot @SamDufel !! Actually have to make a project on Railway Reservation and literally fed up of working since last two days. Could anybody please help me with the concepts of these quotes in sql_query and php script? Thanks again ! :)
@code.atodi Read the documentation here: php.net/manual/en/… and here: php.net/manual/en/… . Also, DO NOT use mysql_* functions, they are deprecated, use mysqli_* or PDO.
@code.atodi - the php manual page on strings has some nice examples, and a thorough overview of features. php.net/manual/en/language.types.string.php If there's anything specific you're not understanding, you can post another question about it. Also, I highly recommend you use an editor with syntax highlighting - it will make it much easier to see if you're quoting something incorrectly.
@SamDufel I use notepad++, that's basic and fast. And thanks both of you for the links ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.