簡體   English   中英

通過PHP變量選擇MySQL查詢

[英]Selecting MySQL query via PHP variables

我在$booked_num得到零,我在SQL中嘗試使用值代替變量,它運行正常。 但我不知道我在哪里弄錯了,請幫幫忙。 我已經回應了每個變量,一切都很好,但$booked_row沒有任何$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);
$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注入攻擊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM