2014-01-06 161 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')");