使用PHP将变量添加到mysql select语句会破坏该语句

时间:2012-11-02 19:47:14

标签: php mysql

我在php页面上使用了以下代码,我只是试图让它工作,它运行第一个查询罚款(这是一个插入)并返回正确的值到$ orderid
但它接下来不会为select查询返回任何内容,如果我取出变量$ myorderid并输入一个数字,它将返回预期的结果,但是一旦我尝试使用变量它就不会返回任何内容。
我已经尝试过使用mysql_escape_string,sprintf和现在的intval方法,但似乎没有任何工作 没有错误从mysql返回只是一个空结果集。

mysql_query($sSQL) or print(mysql_error());
$orderid = mysql_insert_id();

$myorderid =    $orderid;
$myquery = "SELECT cartOrderID, Sum(pDonationAmt) as amount 
FROM products INNER JOIN cart ON ( cartProdID = pID ) 
WHERE cartOrderID = " . intval($myorderid). " GROUP BY cartOrderID ";

$myresults = mysql_query($myquery);
while ($row = mysql_fetch_assoc($myresults)) 
  { error_log('orderid inserted is:.$orderid.' OrderID in   mysql:'.$row['cartOrderID'].' and the total is: '.$row['amount'], 3, 'inc/atestingdata.txt'); }

感谢您对此提供任何帮助!

2 个答案:

答案 0 :(得分:0)

尝试使用引号intval($myorderid)围绕'

 $myquery = "SELECT cartOrderID, Sum(pDonationAmt) as amount 
 FROM products INNER JOIN cart ON ( cartProdID = pID ) 
 WHERE cartOrderID = '" . intval($myorderid). "' GROUP BY cartOrderID ";

答案 1 :(得分:0)

旧的“回声”可以帮助...尝试:

$myquery = "SELECT cartOrderID, Sum(pDonationAmt) as amount 
 FROM products INNER JOIN cart ON (pID = cartProdID) 
 WHERE cartOrderID = $orderid GROUP BY cartOrderID ";
echo $myquery;