2013-12-14 147 views
-7

给出的下面的错误是在我的程序来错误:警告:mysql_fetch_assoc()预计参数1是资源,布尔在

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Users\joshi\Documents\EasyPHP-12.1\www\a.php on line 5

的代码如下:

$con = mysqli_connect("localhost","root","","search"); 
$str = "Akshat"; 
$result = mysqli_query($con,"SELECT * from index WHERE name LIKE '%$str%'"); 
while($row = mysql_fetch_assoc($result)) 
{ 
    echo "Name: " . $row["name"]; 
    echo "<br>Desc: " . $row["desc"]; 
} 

回答

2

index是保留字http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

需要用反引号包装

即:`index`

$result = mysqli_query($con,"SELECT * from `index` WHERE name LIKE '%$str%'"); 
             ^ ^

再加上你与mysqli_功能混合mysql_

你应该一直使用mysqli_fetch_assoc()代替mysql_fetch_assoc()连同您的查询mysqli_query

在保持均匀性与MySQL的功能,它是一个或另一个;不是都。

重写:

<?php 
$con = mysqli_connect("localhost","root","","search"); 
$str = "Akshat"; 
$result = mysqli_query($con,"SELECT * from `index` WHERE name LIKE '%$str%'"); 
while($row = mysqli_fetch_assoc($result)) 
{ 
echo "Name: " . $row["name"]; 
echo "<br>Desc: " . $row["desc"]; 
} 
+0

downvoter,解释PLZ - 我相信这是一个有效的答案。 –

+0

对不起,其实我不习惯使用这个网站。它只是错误发生。我想投“+1”,但错误地投了-1。 –

+0

这个(你的链接)和使用'SELECT * FROM index'的OP有什么关系? @Zarazthuztra'index'是一个保留字,应该用反引号包裹,这在OP代码中显然没有显示。 –

-1

尝试的

mysqli_fetch_assoc() 

代替

mysql_fetch_assoc() 

编号:http://www.php.net/manual/en/mysqli-result.fetch-assoc.php

+0

为什么要Downvote Mr.Downvoter? –

+1

因为这是本课题的第n个重复的,你有足够的代表处和经验投票关闭,而不是促进较差研究职位...... – brasofilo

+0

因为我回顾了坏编辑:http://stackoverflow.com/review/suggested -edits/3582813,谢谢理解。 – brasofilo

相关问题