Reputation: 419
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 !
Upvotes: 2
Views: 88
Reputation: 23
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
}
?>
Upvotes: 0
Reputation: 12085
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();
Upvotes: 1
Reputation: 111
Try this
$result=mysql_query($query);
while($data = mysql_fetch_assoc($result))
{
$row[] = $data;
}
And change !empty($result)
to count($row) > 1
Upvotes: 3
Reputation: 2587
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
Upvotes: 2
Reputation: 198
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!
Upvotes: 2