gpt4 book ai didi

php - 如何解决对php中非对象的成员函数query()的调用?

转载 作者:行者123 更新时间:2023-11-29 11:05:05 25 4
gpt4 key购买 nike

我正在从 URL 调用函数,但收到错误 Call to a member function query() on a non-object in this line( $result =$conn->query( $sql);)。我的数据库连接是完美的。我正在从一个表中检索记录并插入到第二个表中。我的 SQL 查询正在运行。您能帮我吗?

URl:-http://domain.com/admin/delete.php?function=restore&res_id=7

$id = $_GET['res_id']; //retrive id from URL

include('db/connection.php');
switch($_GET['function']) {
case 'delete':delete();break;
case 'restore':restore();break;
default : redirect('index.php');
}

function restore(){
$sql="INSERT INTO request(Id, Name, Email, Mobile_no) SELECT Id, Name, Email, Mobile_no FROM trash WHERE Id=$id";
$result =$conn->query($sql);
$delete="DELETE FROM trash WHERE Id='$id'";
if ($conn->query($delete) === TRUE) {
header('Location:dashboard.php?submitted=1');
} else {
echo "Error deleting record: " . $conn->error;
}
}

最佳答案

希望这能解决您的问题。

<?php
$conn = new mysqli('localhost', 'root', '', 'stakcoverflow');

/*
* This is the "official" OO way to do it,
* BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
*/
if ($conn->connect_error) {
die('Connect Error (' . $conn->connect_errno . ') '
. $conn->connect_error);
}

/*
* Use this instead of $connect_error if you need to ensure
* compatibility with PHP versions prior to 5.2.9 and 5.3.0.
*/
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}

//echo 'Success... ' . $conn->host_info . "\n";

function restore($conn){ //function restore(){
$id = 1;
echo $sql="INSERT INTO request(Id, Name, Email, Mobile_no) SELECT Id, Name, Email, Mobile_no FROM trash WHERE Id=".$id."";
$result = $conn->query($sql);
}

if(restore($conn))
{
echo "Success";
};

$conn->close();

关于php - 如何解决对php中非对象的成员函数query()的调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41543627/

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