gpt4 book ai didi

php - 试图获取非对象的属性 : Using SELECT

转载 作者:搜寻专家 更新时间:2023-10-30 23:41:14 25 4
gpt4 key购买 nike

我的代码没有什么问题...因为我尝试从数据库中SELECT sth 然后INSERT 一些值到另一个表,但是在正常代码中来自 w3school我得到了错误

Tryingo to get property of non-object

这是我的代码:

<?php 
session_start();

function connectionDB(){
$host = "localhost";
$username = "root";
$password = "";
$db_name = "project";

$conn = new mysqli($host, $username, $password, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
return $conn;
}
function getID(){
$id = $_GET['id'];

return $id;
}
$login =$_SESSION['login'];
$conn =connectionDB();
$idCar = getID();
echo $idCar;
$sqluser = "SELECT ID_USER FROM login_table WHERE LOGIN = $login";

if($result->num_rows > 0)
$result = $conn->query($sqluser);
{
while($row = $result->fetch_assoc())
{
$sql = "INSERT INTO cart (id_user) VALUES ('".$row['ID_USER']."')";
}
}else echo"error";
?>

这是产品方面的代码

最佳答案

请查看您的 IF 循环附近的变化

    $sqluser = "SELECT ID_USER FROM login_table WHERE LOGIN = $login";
$result = $conn->query($sqluser); //check result first

if($result->num_rows > 0) //get number of rows
{
while($row = $result->fetch_assoc())
{
$sql = "INSERT INTO cart (id_user) VALUES ('".$row['ID_USER']."')";
}
}else echo"error";

关于php - 试图获取非对象的属性 : Using SELECT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34923955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com