您好,我在使用插入查询中的选择查询中的数据时遇到问题。
这是我的 php 代码 -
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbName = "arestaurant";
$ID = $_GET["Id"];
$QUANTITY = $_GET["Quantity"];
$TABLE = $_GET["Table"];
//Make connection
$conn = new mysqli($servername,$username,$password,$dbName);
// check connection
if(!$conn) {
die("Connection Failed. ".mysqli_connect_error());
}
$sql = "INSERT INTO tableorders (tableNumber,isPaid)
VALUES ('".$TABLE."','0')";
$result = mysqli_query($conn,$sql);
$y = "SELECT orderId from tableorders WHERE tableNumber='$TABLE' ORDER by orderDate DESC limit 1 offset 0 ";
$resulty = mysqli_query($conn,$y);
if ($resulty !== false) {
$value = mysqli_fetch_field($resulty);
$orderid = $value['orderId']; < -- error
}
$sqlquery = "INSERT INTO orderitems (orderId, productId, quantity)
VALUES ('".$orderid."','".$ID."','".$QUANTITY."')";
$result2 = mysqli_query($conn,$sqlquery);
?>
但我得到 -
Fatal error: Cannot use object of type stdClass as array on line 30.
我有几种方法来存储它然后再次使用它,但我似乎可以找到解决方案。
请帮忙?
最佳答案
此错误是因为函数 mysqli_fetch_field 返回一个对象,您可能需要阅读 http://php.net/manual/es/mysqli-result.fetch-field.php 这个函数返回每列的信息。
更改此:
$value = mysqli_fetch_field($resulty);
$orderid = $value->orderId;
也许您需要使用:fetch_assoc
http://php.net/manual/es/mysqli-result.fetch-assoc.php
祝你好运
关于PHP - 存储 SELECT 查询并在 INSERT 查询中使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47270456/