msqli_error()请帮助我成为PHP的初学者

时间:2014-09-16 11:15:17

标签: php mysql mysqli

当我尝试运行以下代码时,给我这个错误:

错误消息

Warning: mysqli_error() expects exactly 1 parameter, 0 given in
FUNCTIONS.PHP on line 5
Database query failed    

代码

<?php
//This File is the place to store all basic functions
function confirm_query($result_set){
if (!$result_set) {
    die("Database query failed" . mysqli_error());
    }
}
function get_all_subjects() {
global $connection;
$query = "SELECT * 
    FROM subjects 
    ORDER BY position ASC";
    $subject_set = mysqli_query($connection, $query);
    confirm_query($subject_set);
    return $subject_set;
}
function get_pages_for_subjects($subject_id) {
    global $connection;
    $query = "SELECT * 
    FROM tblPages 
    WHERE subject_id = {$subject_id} 
    ORDER BY position ASC";

    $page_set = mysqli_query($connection, $query);
    confirm_query($page_set);
    return $page_set;
}
function get_subject_by_id($subject_id){
    global $connection;
    $query = "SELECT * ";
    $query .= "FROM subjects ";
    $query .= "WHERE id=" . $subject_id ." ";
    $query .= "LIMIT 1";
    $result_set = mysqli_query($connection, $query);
    confirm_query($result_set);  


// REMEMBER: 
    // if no rows are returned, fetch_array will return false
    if ($subject = mysqli_fetch_array($result_set)){
    return $subject;
    } else {
    return NULL;
    }
    }
?>  

3 个答案:

答案 0 :(得分:0)

confirm_query($subject_set, $connection);

function confirm_query($result_set, $connection){
    if (!$result_set) {
        die("Database query failed" . mysqli_error($connection)); //pass the connection
    }
}

参考 - manual

答案 1 :(得分:0)

JUst传递与mysqli_error的连接。使用下面的代码

  die("Database query failed" . mysqli_error($connection));

希望这有助于你

答案 2 :(得分:0)

您也可以通过连接确认查询功能

 function confirm_query($result_set,$connection){
  if (!$result_set) {
       die("Database query failed" . mysqli_error($connection));
    }
 }