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

如何在php中解析对非对象的成员函数query()的调用?,php,mysql,function,Php,Mysql,Function,我正在从URL调用函数,并且在此行的非对象上($result=$conn->query($sql);)对成员函数query()进行错误调用。我的数据库连接非常完美。我从一个表中检索记录并插入到第二个表中。我的SQL查询正在运行。您能帮我吗 URl:-http://domain.com/admin/delete.php?function=restore&res_id=7 $id = $_GET['res_id']; //retrive id from URL include('db/c

我正在从URL调用函数,并且在此行的非对象上(
$result=$conn->query($sql);
)对成员函数
query()
进行错误调用。我的数据库连接非常完美。我从一个表中检索记录并插入到第二个表中。我的
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();

传递$conn变量以恢复功能,例如

恢复($conn)

和改变功能

函数restore(){

功能恢复($conn){

案例“restore”:restore();break

案例“restore”:restore($conn);break

…希望对你有帮助

下面是您的更新代码

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($conn);break; //case 'restore':restore();break;
    default : redirect('index.php');
}

function restore($conn){ //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);
    if ($conn->query($delete) === TRUE) {
        header('Location:dashboard.php?submitted=1');
    } else {
        echo "Error deleting record: " . $conn->error;
    }
}
也改变

$sql=“插入请求(Id、姓名、电子邮件、手机号码)从Id=$Id的垃圾箱中选择Id、姓名、电子邮件、手机号码”


$sql=“插入请求(Id、姓名、电子邮件、手机号码)从垃圾箱中选择Id、姓名、电子邮件、手机号码,其中Id=“(int)$\u GET['res\u Id'];

您正在使用
mysqli\u connect
功能吗?感谢回复Didar先生,请检查我的连接,$conn=new mysqli($servername、$username、$password、$dbname);将$conn变量传递给restore($conn)等还原函数,并将function restore(){更改为function restore($conn){…希望这能帮助您感谢回复Aamir先生,我现在遇到错误“restore()缺少参数1”使用
var\u dump($conn)
要检查连接是否存在。您的问题是否已解决,请让我知道。谢谢您的帮助,迪达尔先生,但我正在将函数用于开关箱好的,使用函数很好。然后您可以调用它多少次。如果我的努力确实对您有所帮助,请给我投票。谢谢。迪达尔您能帮助我吗当然可以。但是你需要提供完整的页面和你的期望输出。这将有助于我给你一个正确的解决方案。谢谢。谢谢你的回复,Aamir先生,错误发生了,但是在一个函数中,我的查询不起作用。酷!!!现在它起作用了。我添加了$id=$\u GET['res\u id'];您建议我使用的内部功能,工作非常完美。谢谢您的帮助。请向我这边投票