2

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 !

Gvidas
  • 1,964
  • 2
  • 14
  • 21
Chathurika
  • 419
  • 2
  • 6
  • 18
  • Possible duplicate of [mysql\_fetch\_array()/mysql\_fetch\_assoc()/mysql\_fetch\_row() expects parameter 1 to be resource or mysqli\_result, boolean given](http://stackoverflow.com/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-expects-parameter-1-to) – Gvidas May 24 '16 at 07:08
  • 2
    echo your query and check directly into phpmyadmin – Saty May 24 '16 at 07:09
  • 3
    mysql is deprecated try to use mysqi_* or PDO – JYoThI May 24 '16 at 07:12

5 Answers5

3

Try this

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

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

kiran mulmi
  • 113
  • 7
2

Try with this , you need to use mysql_query() function and you pass string directly to mysql_fetch_array()

$query  = mysql_query("SELECT * FROM employee WHERE name like '" . $_POST["keyword"] . "%' ORDER BY name LIMIT 0,6");
$result = mysql_fetch_array($query);

Note : mysql_* functions deprecated and removed in PHP 7.x. Use MySQLi or PDO_MySQL extension

Maninderpreet Singh
  • 2,569
  • 2
  • 17
  • 31
2

You need to actually perform the query before being able to fetch the results:

$result = mysql_query("SELECT id, name FROM mytable");
$rows = mysql_fetch_array($result);

Check out the PHP docs for more in-depth examples: http://php.net/manual/de/function.mysql-fetch-array.php

On a sidenote: using mysql_* function has been deprecated for a while, have a look into mysqli!

Karl Wolf
  • 188
  • 3
  • 10
1

you have execute the query first

Deprecated features in PHP 5.5.x

The original MySQL extension is now deprecated, and will generate E_DEPRECATED errors when connecting to a database. Instead, use the MYSQLi or PDO_MySQL extensions.

 <?php
 // include 'func/db_connect.php';


 global $conn;

$servername = "localhost";  //host name

$username = "username"; //username

$password = "password"; //password

$mysql_database = "dbname"; //database name

//mysqli prepared statement 

$conn = mysqli_connect($servername, $username, $password) or die("Connection failed: " . mysqli_connect_error());

 mysqli_select_db($conn,$mysql_database) or die("Opps some thing went wrong");


if(!empty($_POST["keyword"])) {

$name_val = '%'.$_POST["keyword"].'%';

$stmt = $conn->prepare("SELECT * FROM employee WHERE name like ? ORDER BY name LIMIT 0,6");

            $stmt->bind_param('s',$name_val);

            $qry_res=$stmt->execute();


if($row_count>0) {
?>
<ul id="name-list">
    <?php
  while($row = $qry_res->fetch_assoc())
    {
        ?>
        <li onClick="selectName('<?php echo $row["name"]; ?>');"><?php echo $row["name"]; ?></li>
    <?php } ?>
</ul>
     $stmt->close();
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

You can try this:

$cn=mysql_connect("localhost","root","");
if(!$cn)
{
    echo "Unable to connect";
    die();
}

    $query = "Select * from table";
    $result= mysql_query($query,$cn);
    $n = mysql_num_rows($result);

    if($n>0)
{
    while($rw=mysql_fetch_array($result))
    {

    $owenername=$rw["owenername"];?>
    <p>Owner Name:<?php echo $owenername;?> </p>
    <?php}
}
else
{
    ?>data not found
    <?PHP 
}
    ?>
nishant02
  • 23
  • 3