Chathurika Chathurika - 6 months ago 15
SQL Question

MYSQL Error when making Auto complete text box

I want to make a auto complete text box for select employee name from DB. But it makes query error which is

Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in





Following is my code.

<?php
include 'func/db_connect.php';
if(!empty($_POST["keyword"])) {
$query ="SELECT * FROM employee WHERE name like '" . $_POST["keyword"] . "%' ORDER BY name LIMIT 0,6";

$result=mysql_fetch_array($query);

if(!empty($result)) {
?>
<ul id="name-list">
<?php
foreach($result as $name) {
?>
<li onClick="selectName('<?php echo $name["name"]; ?>');"><?php echo $name["name"]; ?></li>
<?php } ?>
</ul>
<?php } } ?>





What is the wrong with this code, can anyone help me !

Answer

Try this

$result=mysql_query($query);
while($data = mysql_fetch_assoc($result))
{
    $row[] = $data;
}

And change !empty($result) to count($row) > 1