2013-03-08 62 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