-7

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

I am getting this error while i am trying to delete a record the query is working but this line remains on the page. i want to echo "Deleted" written in the while should show up but the while loop is not working, i have tried and searched alot nothing helps!

 mysql_fetch_array() expects parameter 1 to be resource, boolean given in delete.php on line 27

delete.php

<html>
<body>
<form method="post">
Id : <input type="text" name="id">
Name : <input type="text" name="name">
Description  : <input type="text" name="des">
<input type="submit" value="delete" name="delete">
</form>
<?php 
include("connect.php");
 $id = $_POST['id'];
 $name = $_POST['name'];
 $des = $_POST['des'];

 $result = mysql_query("DELETE FROM fact WHERE id='$id'") or die(mysql_error());

 while($row = mysql_fetch_array($result))
  {

   echo "Deleted";
   }

 mysql_close($con);  ?>
 </body>
 </html>

connect.php

<?php 
  $con = mysql_connect("localhost","root","");
 if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
   mysql_select_db("Dataentry", $con);
  ?>

How should i make the while loop work..

Community
  • 1
  • 1
iOSBee
  • 217
  • 7
  • 22

7 Answers7

2

The issue is that a delete statement doesn't produce a result set!

$result = mysql_query("DELETE FROM fact WHERE id='$id'") or die(mysql_error());
if (mysql_affected_rows() > 0)
{
    echo 'Record deleted!';
}

Also: As mentioned in the comments, you are vulnerable to SQL injection, and also using a deprecated extension. Please have a look through this awesome post for some guidance on these issues.

Community
  • 1
  • 1
Dale
  • 10,384
  • 21
  • 34
1

when you run a DELETE command, I believe nothing is returned, thus you can't mysql_fetch_array(). You would normally use that if you're doing a SELECT. in this case, you're deleting something, so just remove that loop, and echo().

kennypu
  • 5,950
  • 2
  • 22
  • 28
0
  1. Dont use mysql_* functions, use PDO or Mysqli instead.
  2. take care about $id value. more info
  3. we use mysql_fetch_array for fetching return result of SELECT Queries.

You may want this

if(mysql_affected_rows()  > 0)
{
   echo "Deleted";
}
else
{
  echo "An error occured.";
}
Community
  • 1
  • 1
Shahrokhian
  • 1,100
  • 13
  • 28
0
    <html>
   <body>
    <form method="post">
      Id : <input type="text" name="id">
       Name : <input type="text" name="name">
   Description  : <input type="text" name="des">
     <input type="submit" value="delete" name="delete">
   </form>
   <?php 
    include("connect.php");
     $id = $_POST['id'];
     $name = $_POST['name'];
     $des = $_POST['des'];

   $result = mysql_query("DELETE FROM fact WHERE id='$id'") or die(mysql_error());

    if($result) //if(mysql_affected_rows($result) > 0)
    {

       echo "Deleted";
    }
    else
    {
        echo mysql_error();
    }

    mysql_close($con);  ?>
   </body>
    </html>
Premkumar S
  • 263
  • 3
  • 8
0

Don't use mysql_* function, Try PDO example

<?php
$dbh = new PDO('mysql:host=localhost;dbname=test', 'user', 'password');
$count = $dbh->exec("DELETE FROM fact WHERE id='$id'");
if($count > 0){
    echo 'Deleted';
}else{
    echo 'Not Deleted';
}
?>

$count will return no. of rows deleted

Kasun
  • 679
  • 1
  • 7
  • 21
0
$result = mysql_query("DELETE FROM fact WHERE id='$id'") or die(mysql_error());

if($result)
{
    echo "Deleted";
}
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
-1

in some time die create problem so you use it like this $result = mysql_query("DELETE FROM fact WHERE id='$id'");