通过PHP变量选择MySQL查询(Selecting MySQL query via PHP var

2019-10-29 14:35发布

我在获得零$booked_num ,我试着用到位的变量的值在SQL查询,它工作得很好。 但我不知道我犯了一个错误,请大家帮忙。 我已经回应每一个变量,一切都很好,但没有什么是有在$booked_row$booked_num是呼应为零。

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);

Answer 1:

$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_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());

此外,你应该考虑切换到PDO库。 除其他事项外,它会帮助你避免在查询SQL注入攻击。



文章来源: Selecting MySQL query via PHP variables