Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

php - Function getResult call

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:xampphtdocscadfunc.php on line 92

Warning: mysqli_query() expects at least 2 parameters, 1 given in C:xampphtdocscadfunc.php on line 92

Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:xampphtdocscadfunc.php on line 96
SQL Retrieve Error:

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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());
        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...