2014-01-06 273 views
0

我有這個PHP值不從mysql數據庫中選擇

<?php  
    require("./connect.php"); 

    $cust_name=$_POST['name']; 
    $cust_addr=$_POST['shippingAddr']; 
    $cust_pincode=$_POST['pincode']; 
    $buttonid=$_REQUEST['button_id']; 

    $query=mysql_query("select * from button where button_id='$buttonid'"); 
    $numrows = mysql_num_rows($query); 
    //echo "error is ".$numrows; 
     if($numrows==1) 
    { 
     $row = mysql_fetch_assoc($query); 
     $merchant_id = $row['merchant_id']; 
     echo "hi SUCCESS id is ".$buttonid; 
    } 
    else 
    { 
     echo "<br>EROR NOT inserted id is ".$buttonid; 
    } 
?> 

林不能夠使用

$query=mysql_query("select * from button where button_id='$buttonid'"); 

我試圖呼應$numrows來獲取數據,但它返回0 always.I試圖mysql_error,我沒有有任何errors.and嘗試error_get_last,也沒有錯誤。

我確定其他connect.php用於連接數據庫,並且所有工作都正常。並且button_id變量也被填充。但是select語句does not似乎工作?任何幫助。

UPDATE

我發現有空格button_id=' mmrw6FJFfDWBYt2aSO1qm '我button_id.Why這是否happen.The vairable原本不具有it.How我可以刪除嗎?

+1

嘗試回顯'SELECT'語句以確保它包含您的期望。 – Barmar

+1

如果沒有行,唯一可能的原因是沒有任何匹配'$ buttonid'的行。 – Barmar

+0

隨着您發佈的信息,很難猜出錯誤。更好地發佈您的'buttonid'輸入值和表格數據屏幕截圖。 –

回答

0

阿里納斯事先你一個變量如果您只希望返回一行,應該在查詢結尾處放置LIMIT 1。

但是你的代碼看起來相當不錯,你確定button_id被正確填充了嗎?嘗試使用:

var_dump($buttonid); 

除此之外,您還用$ buttonid但在你的HTML元素的名稱似乎button_id,這是一個打字錯誤吧?

此外嘗試打印解決查詢:

echo "select * from button where button_id='$buttonid'"; 

,並把結果在phpMyAdmin調試它,直到它使用的mysql_query,一般有用的提示之前的作品。

+0

我試過這個,我發現button_id button_id ='mmrw6FJFfDWBYt2aSO1qm'之前和之後都有空白爲什麼會發生這種情況?以及如何刪除它? – user3163619

+0

如果你知道這個值應該是數字的,最簡單的方法就是將它轉換爲類型,這會帶來對sql注入的安全性: echo「select * from button where button_id ='」。 intval($ buttonid)。 「「; – Steini

-1

首先嚐試調試$ buttonid,看看它實際上是包含內容

print_r($buttonid); 

如果這是真的,然後將查詢更改爲

$query=mysql_query("select * from button where button_id='" . $buttonid . "'"); 
+3

爲什麼連接而不是替代會有所作爲? – Barmar

0

對於整型提交你需要用「「」引號, 試試這個

$query=mysql_query("select * from button where button_id=$buttonid"); 
0

你好,你應該使用引號和PHP就可以得到了。 這樣的事

如果你的數據庫中的button_id是intnumber你不會重新輸入這個。

$query=mysql_query("select * from button where button_id=$buttonid"); 

如果你的數據庫button_id是varcharstring使用該解決方案

$query=mysql_query("select * from button where button_id='" . $buttonid . "'"); 

這將是更好的定義查詢這個樣子。

和PHP代碼

$result = mysql_fetch_array($query); 
$result['columnInDb']; 
0

1-

$query=mysql_query("select * from button where button_id=$buttonid"); 
$buttonid >>> without single because it num 

2-

use $row = mysql_fetch_array($query); 

代替$row = mysql_fetch_assoc($query);

0

我發現在我的button_id中有空白button_id ='mmrw6FJFfDWBYt2aSO1qm'。爲什麼會發生這種情況。可以放棄的原本沒有它。我可以如何刪除它?

使用TRIM功能。它刪除尾隨空格。

$query=mysql_query("select * from button where button_id=trim('$buttonid')");