Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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..

share|improve this question
1  
Also: SQL INJECTION VULNERABILITY! –  deceze Dec 14 '12 at 10:21
1  
Also: mysql_* functions are deprecated! –  Petah Dec 14 '12 at 10:22
add comment

marked as duplicate by deceze, DCoder, Rory McCrossan, AlphaMale, Inder Kumar Rathore Dec 14 '12 at 14:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

7 Answers

up vote 1 down vote accepted

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().

share|improve this answer
add comment

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.

share|improve this answer
    
This one worked for me .. thx .. but i have to remove the while loop now –  iOSBee Dec 14 '12 at 10:41
    
@user1877823 Yes, there is no need for a while loop because there are no "results" to loop through :) –  Dale Dec 14 '12 at 10:42
add comment
  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.";
}
share|improve this answer
add comment
    <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>
share|improve this answer
add comment

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

share|improve this answer
add comment
$result = mysql_query("DELETE FROM fact WHERE id='$id'") or die(mysql_error());

if($result)
{
    echo "Deleted";
}
share|improve this answer
add comment

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

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.