Php Messenger平台SQL数据库访问

Php Messenger平台SQL数据库访问,php,mysql,facebook-messenger-bot,Php,Mysql,Facebook Messenger Bot,我在这方面遇到了问题,我想返回item_name的值 $productnum = "1001"; $mysqli = new mysqli("localhost", "ewconline_db_user", "steve030405", "ewconline_db"); $result = $mysqli->query("SELECT * FROM `shop_products` WHERE `item_id` = '$productnum' "); $row = $re

我在这方面遇到了问题,我想返回item_name的值

$productnum = "1001";

$mysqli = new mysqli("localhost", "ewconline_db_user", "steve030405", "ewconline_db");
    $result = $mysqli->query("SELECT * FROM `shop_products` WHERE `item_id` = '$productnum' ");
    $row = $result->fetch_assoc();
    $item_name = $row['item_name'];

    $response = "{
                    'recipient':{
                        'id': $senderId
                        },
                    'message':{
                        'text': $item_name
                    }
                }";
它似乎没有得到该值并向用户发送回复

如果我在不同的url上运行sql程序。它非常好用。

你可以这样试试

while ($row = $result->fetch_assoc()) {
   $item_name = $row['item_name'];
}

您应该始终检查您的连接是否成功开始。因此,在构建mysqli实例后,添加如下内容:

if (mysqli_connect_errno()) {
    printf("Database connection failed: %s\n", mysqli_connect_error());
    die(); // or throw an exception or whatever
}
这应该告诉你为什么你没有得到你的预期结果

$mysqli = new mysqli("localhost", "ewconline_db_user", "steve030405", "ewconline_db");
$result = $mysqli->query("SELECT * FROM `shop_products` WHERE `item_id` = '".$productnum."'");
$row = $result->fetch_assoc();
$answer = $row['item_name'];

$response = [
    'recipient' => [ 'id' => $senderId ],
    'message' => [ 'text' => $answer ]
];
我是这样做的,而且很有效


并使用json_编码($response)

您可以尝试使用$item\u name=$row[0]['item\u name']先生?它仍然不起作用。不要“手动”以字符串形式创建JSON。创建适当的数据结构,然后使用json_encode。“如果我在不同的url上运行sql程序,它工作得非常好。”-然后进行适当的调试…?WTH!。行得通,先生。谢谢谢谢谢谢!。不要以字符串形式“手动”创建JSON。创建适当的数据结构,然后使用json_encode。