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);
jleft
  • 3,457
  • 1
  • 23
  • 35
  • You're mixing single and double quotes. You cannot start a string with a single quote and end it with a double quote. – lanzz Apr 14 '13 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`](http://www.php.net/manual/en/mysqlinfo.api.choosing.php). – jleft Apr 14 '13 at 16:02

1 Answers1

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.

Sam Dufel
  • 17,560
  • 3
  • 48
  • 51
  • 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 ! :) –  Apr 14 '13 at 16:03
  • 1
    @code.atodi Read the documentation here: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single and here: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double . Also, **DO NOT** use `mysql_*` functions, they are deprecated, use `mysqli_*` or PDO. – ulentini Apr 14 '13 at 16:05
  • 1
    @code.atodi - the php manual page on strings has some nice examples, and a thorough overview of features. http://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. – Sam Dufel Apr 14 '13 at 16:07
  • @SamDufel I use notepad++, that's basic and fast. And thanks both of you for the links ! :) –  Apr 14 '13 at 16:10