Php 未从mysql数据库中选择值

Php 未从mysql数据库中选择值,php,mysql,database,rows,fetch,Php,Mysql,Database,Rows,Fetch,我有这个php <?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 wher

我有这个php

<?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;
    } 
?>
我尝试回显
$numrows
,但它始终返回0。我尝试了mysql\u错误,没有任何错误。最后尝试了error\u get\u,也没有错误

我确信用于连接数据库的另一个connect.php和all都正常工作。button_id变量也被填充。但是select语句似乎不起作用?有帮助吗

更新


我发现我的按钮id中有空格
button\u id='mmrw6FJFfDWBYt2aSO1qm'
。为什么会发生这种情况。VARABLE最初没有。我如何删除它?

首先尝试调试$buttonid,看看它是否确实是一个包含内容的变量

print_r($buttonid);
如果这是真的,那么将查询更改为

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

如果只需要返回一行,那么应该在查询的末尾加上LIMIT 1

然而,您的代码似乎很好,您确定按钮id已正确填充吗?尝试使用:

var_dump($buttonid);
此外,您使用了$buttonid,但html中元素的名称似乎是button_id,这可能是键入错误吗

此外,请尝试打印已解决的查询:

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

并将结果放入phpmyadmin中进行调试,直到它工作为止,然后再使用mysql_查询,这是一个非常有用的提示。

对于整型字段,需要用“'”引号括起来, 试试这个

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

您好,您应该使用引号和php将能够得到这一点。 像这样的

如果数据库中的按钮id为
int
number
,则不会重新键入此项

$query=mysql_query("select * from button where button_id=$buttonid");
如果数据库中的按钮id为
varchar
string
则使用此解决方案

$query=mysql_query("select * from button where button_id='" . $buttonid . "'");
这样定义查询会更好

和php代码

$result = mysql_fetch_array($query);
$result['columnInDb'];
一,-

2-

而不是
$row=mysql\u fetch\u assoc($query)

我发现我的按钮id中有空格button_id='mmrw6FJFfDWBYt2aSO1qm'。为什么会发生这种情况。VARABLE最初没有它。我如何删除它

使用函数。它删除尾随空格

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

尝试回显
SELECT
语句,以确保它包含您期望的内容。如果没有行,唯一可能的原因是没有任何行与
$buttonid
匹配。根据您发布的信息,很难猜出错误。最好发布你的
按钮id
输入值和表格数据屏幕截图。现在。。。现在看见你所谓的
buttonid
是一个
varchar
,每个人都在想,由于命名惯例,这是一个数字,并对其他人建议放回引号的答案进行向下投票。请注意,您使用的是一种不推荐使用的连接方法。为什么连接而不是替换会产生影响?我尝试了这个方法,发现在button_id button_id='mmrw6FJFfDWBYt2aSO1qm'之前和之后都有空格为什么发生如何删除它?如果您知道值应该是数字的,那么最简单的删除方法就是类型转换,这也带来了对sql注入的安全性:echo“select*from button where button_id=”。intval($buttonid)。'";
$query=mysql_query("select * from button where button_id=$buttonid");
$buttonid  >>> without single because it num
use $row = mysql_fetch_array($query);
$query=mysql_query("select * from button where button_id=trim('$buttonid')");