2013-03-08 60 views
0

每當我嘗試使用php連接到數據庫時,我總是得到這個錯誤:Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /homepages/12/d441172468/htdocs/Organizer/dashboard/index.php on line 258我該如何解決這個問題?我的代碼是顯示mysql數據庫表時出現php錯誤

  $email = '[email protected]_classes'; 

      echo $email; 

      $result = mysqli_query($con,"SELECT * FROM $email"); 

      $classcount = 1; 

      while($row = mysqli_fetch_array($result)) 
       { 

       $period = $row['period']; 
       $teacher = $row['teacher']; 
       $subject = $row['subject']; 
       $subjecto = strtolower($subject); 
       $subjecto = str_replace(' ', '', $subjecto); 
       $grade = $row['grade']; 

       echo "<li id='button" . $classcount . "' onclick='" . $subjecto . "(),homework" . $classcount . "()'>" . $classcount . ". " . $subject . "-" . $grade . "</li>\n"; 

       $classcount += 1; 

       } 

$email變量正常工作時,我贊同它。 128線是while($row = mysqli_fetch_array($result))部分

+4

您正在傳遞'$ email'作爲表名稱。當然你的意思是''SELECT * FROM your_table WHERE email ='$ email''''通過mysqli_real_escape_string()轉義'$ email'後 – 2013-03-08 01:52:49

+0

您確定$ con正在工作嗎? – 2013-03-08 01:53:00

+0

'$ email'的值是我的表名,我確定我的'$ con'正在工作 – 2013-03-08 01:53:54

回答

2

如果$email的價值真的是你的表名,你需要爲它包含了不加引號table names允許的字符(@)引用它:

$result = mysqli_query($con,"SELECT * FROM `$email`"); 

請注意,您需要引用表 - 和mysql中的反向列名。

+0

謝謝!我用'''引用它 – 2013-03-08 01:59:25

+0

@ThomasLai不,如果是表名,則需要反引號,請參閱我鏈接到的手冊頁:**標識符引號字符是反引號(「'」)** – jeroen 2013-03-08 01:59:58

0

表名稱中允許ASCIIhttp://dev.mysql.com/doc/refman/5.1/en/identifiers.html

Permitted characters in unquoted identifiers: 

    ASCII: [0-9,a-z,A-Z$_] (basic Latin letters, digits 0-9, dollar, underscore) 

    Extended: U+0080 .. U+FFFF 

    Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+0000: 

    ASCII: U+0001 .. U+007F 

    Extended: U+0080 .. U+FFFF 

    ASCII NUL (U+0000) and supplementary characters (U+10000 and higher) are not permitted in quoted or unquoted identifiers. 

    Identifiers may begin with a digit but unless quoted may not consist solely of digits. 

    Database, table, and column names cannot end with space characters. 

    Before MySQL 5.1.6, database and table names cannot contain 「/」, 「\」, 「.」, or characters that are not permitted in file names. 

這很奇怪,你已經手動添加表用繩子@。我認爲這不能作爲表名處理?

跳過它下面


您的查詢是無效的,改變你的代碼,例如:與表的名稱

mysqli_query($con,"SELECT * FROM TABLE_NAME WHERE 'email' = $email"); 

變化TABLE_NAME。 使用表TABLE_NAME上的列電子郵件名稱更改email

你是mysql注入的受害者,所以不會檢查entites和specialchars!

如果您的VALUE $電子郵件地址是TABLE_NAME,應該是帶有字符的LIMITED!

+0

但我的表名是'$ email'的值 – 2013-03-08 01:55:14

+1

你的表是用電子郵件命名的?! – Kermit 2013-03-08 01:55:53

+0

您的TABLE_NAME是TABLE名稱。不是TABLE_NAME中的COLUMN。 – 2013-03-08 01:56:52