Function getResult call

我的未来我决定 提交于 2019-12-25 01:45:45

问题


I'm working transferring my functions from MySQL to MySQLi standards.

This is my old function for getresult

  function getResult($sql) {
            $result = mysql_query($sql, $this->conn);
            if ($result) {
                return $result;
            } else {
                die("SQL Retrieve Error: " . mysql_error());
            }
        }

However, I have been following the W3schools on the mysqli_query function. Here's where I'm presently at.

 function getResult($connection){
        $result = mysqli_query($connection->conn);
        if ($result) {
            return $result;
        } else {
            die("SQL Retrieve Error: " . mysqli_error());
        }
    }

Now, the examples on W3schools are just a bit different from how I want to use mysqli_query. I'm trying to make it be directed at my DB, not some input or constants as per examples on W3S.

Notice: Trying to get property of non-object in C:\xampp\htdocs\cad\func.php on line 92

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\cad\func.php on line 92

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\xampp\htdocs\cad\func.php on line 96
SQL Retrieve Error:

Thank you


回答1:


Your version won't work for a number of reasons, not the least of which is that you haven't included the query you want to send. Assuming $this->conn is set up properly somehow, try this:

 function getResult($sql) {
        $result = mysqli_query($this->conn, $sql);  //Note parameters reversed here.
        if ($result) {
            return $result;
        } else {
            die("SQL Retrieve Error: " . mysql_error());
        }
    }



回答2:


From the PHP help files I found these function definitions

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )
string mysqli_error ( mysqli $link )

mysqli_query() takes the link and the actual query as mandatory parameters, and mysqli_error takes the link as one as well.

http://php.net/manual/en/mysqli.query.php

http://www.php.net/manual/en/mysqli.error.php



来源:https://stackoverflow.com/questions/17517058/function-getresult-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!