簡體   English   中英

SQL查詢中的變量-PHP MYSQL

[英]variable in SQL query - PHP MYSQL

我有2個可變持有日期。 使用變量時,SQL語句中出現語法錯誤。

$from_date = '2013-02-13';
$to_date = '2013-02-20';
$query="SELECT * FROM mytable where datex >= ".$fromdate." AND datex <= ".$todate.";

幫助我識別並糾正此語法錯誤?

謝謝。

您需要引用變量:

$query="SELECT * FROM mytable where datex >= '".$fromdate."' AND datex <= '".$todate." . "'";

但是最好使用帶有綁定變量的預備語句。 然后您的查詢可能看起來像(PDO):

$query="SELECT * FROM mytable where datex >= :fromdate AND datex <= :todate";

我做類似的事情,這將是正確的方法嗎?

  <?php
  include 'connect.php';

  $id1 = $_POST['PatientID']; //Text box the user searches in
  $result = mysqli_query($con,"SELECT * FROM PatientRecords WHERE PatientID= '".$id1."'");
  while($row = mysqli_fetch_array($result))

暫無
暫無

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

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