2015-06-14 38 views
1

我有一個問題要問。 當我輸入值從report_id搜索始終發生的字段 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in 。所有字段可以搜索,但report_id場不能搜索,我不知道如何解決它以表單中的多個字段搜索MySQL

if(isset($_POST['submit'])) { 
       // define the list of fields 
        $fields = array('report_name', 'report_id', 'abstract', 'student_name', 'teacher_name'); 
        $conditions = array(); 
        foreach($fields as $field){ 
    // if the field is set and not empty 
        if(isset($_POST[$field]) && $_POST[$field] != '') { 
     // create a new condition while escaping the value inputed by the user (SQL Injection) 
         $conditions[] = "`$field` LIKE '%" . mysql_real_escape_string($_POST[$field]) . "%'"; 
        } 
        } 
        $query = "SELECT report.report_id, report_year, report_status, report_type, teacher_name, abstract, report_position, report_name, 
        GROUP_CONCAT(student_name ORDER BY student_name ASC SEPARATOR ', ') AS student_name 
        FROM student RIGHT JOIN report ON student.report_id = report.report_id GROUP BY report_name 
        ORDER BY report_name ASC"; 
// if there are conditions defined 
        if(count($conditions) > 0) { 
    // append the conditions 
        $query = "SELECT report.report_id, report_year, report_status, report_type, teacher_name, abstract, report_position, report_name, 
        GROUP_CONCAT(student_name ORDER BY student_name ASC SEPARATOR ', ') AS student_name 
        FROM student RIGHT JOIN report ON student.report_id = report.report_id WHERE " . implode (' AND ', $conditions) . " GROUP BY report_name 
        ORDER BY report_id asc, report_name ASC"; 
        } 

        $result = mysql_query($query); 
       } 

HTML

<form action="searching.php" method="post" > 
    <p><label>Project Name</label></p> 
    <input name="report_name" type="text" class="form-control" placeholder="Enter Report name" id="report_name"> 
    <p><label>Project ID</label></p> 
    <input name="report_id" type="text" class="form-control" placeholder="Enter ID Report" id="report_id"> 
    <p><label>Keyword</label></p> 
    <input name="abstract" type="text" class="form-control" placeholder="Enter Some Keyword" id="abstract"> 
    <p><label>Student Name</label></p> 
    <input name="student_name" type="text" class="form-control" placeholder="Enter Student Name" id="student_name"> 
    <p><label>Advisor Name</label></p> 
    <input name="teacher_name" type="text" class="form-control" placeholder="Enter Advisor Name" id="teacher_name"> 
    <br><button type="submit" name="submit" class="btn btn-primary" >Submit</button> 

回答

1

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in最可能的原因是,你的電話到mysql_connect返回FALSE,因爲它無法連接到您的數據庫。來自docs

成功返回MySQL鏈接標識符或失敗時返回FALSE。

+0

爲什麼所有字段都可以搜索,但只有report_id字段它總是發生警告:mysql_fetch_array()期望參數1是資源, – Phatter