使用mysqli时出错

问题描述:

I've decided to using mysqli instead of mysql and I've having some errors, this is my first time using mysqli and I don't know what the errors are, any suggestions?

Warning: mysql_query() expects parameter 2 to be resource, string given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysql_query() expects parameter 2 to be resource, string given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/u250000297/public_html/forum/system/db.php on line 45
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in /home/u250000297/public_html/forum/system/db.php on line 33
Warning: mysqli_error() expects exactly 1 parameter, 0 given in /home/u250000297/public_html/forum/system/db.php on line 33

Line 32-36

function query($query) {
    $sql = mysqli_query($query, $this->db) or die(mysqli_error());
    return $sql;
    mysqli_free_result($sql);
}

Line 44-48

function fetch($query) {
    $sql = mysqli_fetch_array(mysql_query($query, $this->db));
    return $sql;
    mysqli_free_result($sql);
}

Try this, You have used mysql_query instead of mysqli_query

function query($query) {
 $sql = mysqli_query($this->db, $query) or die(mysqli_error());
 ...
}

function fetch($query) {
  mysqli_fetch_array(mysqli_query($this->db, $query));
 ....
}

instead of

function fetch($query) {
  mysqli_fetch_array(mysql_query($query, $this->db));
  ...
}

Try using a mysqli_query() function like this maybe

$data = mysqli_query($dbc, $query);

mysqli_fetch_array($data)

Notice that with mysqli_query() two arguments are passed:

  1. the database connection variable
  2. the query variable

Then use the the result of the mysqli_query() function as the argument to mysqli_fetch_array() function :D