11 Replies - 1279 Views - Last Post: 05 August 2013 - 10:56 AM Rate Topic: -----

#1 sabasi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 04-August 13

mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 04 August 2013 - 03:48 PM

everything is okay but when i submit the selected item for delete,this error show "Warning:mysql_num_rows()expects parameter 1 to be resource,boolean given in C:\www\login\delete.php on line 15" which is this line in my code "if(mysql_num_rows($result)) {"
how do i make it right ?
 <?php

    // Database Constants
    define("DB_SERVER", "localhost");
    define("DB_NAME", "audi");
    define("DB_USER", "root");
    define("DB_PASS", "123456");

    // Create a database connection
    $connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
    if (!$connection) {
    die("Database connection failed: " . mysql_error());
    }

    // Select a database to use 
    mysql_select_db(DB_NAME,$connection);
?>
<html>
<head>
<title>Delete Users</title>
</head>
<body>
<?php 
$username = isset($_POST['react']) ? $_POST['react'] : NULL;

if(!empty($username)) {
$query= "DELETE FROM users WHERE username = '$username'";
$result = mysql_query($query,$connection);
if(mysql_num_rows($result)) {
echo("<strong>$username</strong>Successfully Deleted<p>");
                            }
else {
     echo("<strong>no users are available to delete yet, sorry. </strong><p>");
	 }
	 }
?>

<form method="post" action="Delete_user.php"><div align="center"><center><p>Delete users
<input type="hidden" name="react" value="delete_user" />
<select name="user" size="1">
<?php 
$query = "SELECT username FROM users ORDER BY username";
$result = mysql_query($query,$connection);
if(mysql_num_rows($result)){
//we have atleast one user,so show all users as options in select
while ($rows = mysql_fetch_row($result))
   {
echo("<option value=\"$rows[0]\">$rows[0]</option>");
   }
} 
   else {
         echo("<option value=\"\">Please Select User</option>");
        }



?>
</select><input type="submit" value="submit"></p></center></div>
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2884
  • View blog
  • Posts: 9,566
  • Joined: 12-December 12

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 04 August 2013 - 04:48 PM

Reading the docs, the return result from mysql_query is:

Quote

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

So your DELETE query is failing. Print out or echo the $username value. If the username seems to be correct then print out $query and try to run it in phpMyAdmin.

..and the mysql library is deprecated. See my signature and links about this and prepared statements.

BTW DIVs and most other elements do not have an align attribute, and there is no longer a center tag: use css instead.

This post has been edited by andrewsw: 04 August 2013 - 05:01 PM

Was This Post Helpful? 1
  • +
  • -

#3 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2884
  • View blog
  • Posts: 9,566
  • Joined: 12-December 12

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 04 August 2013 - 04:54 PM

Added: Looking at your code again, you have:

<input type="hidden" name="react" value="delete_user" />

so the POST-variable react contains the value "delete_user", not the username. You should be looking at $_POST['user'].

This post has been edited by andrewsw: 04 August 2013 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2884
  • View blog
  • Posts: 9,566
  • Joined: 12-December 12

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 04 August 2013 - 05:04 PM

BTWW You haven't closed your form-tag.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2834
  • View blog
  • Posts: 9,740
  • Joined: 08-August 08

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 04 August 2013 - 05:09 PM

...and of course you should not be using deprecated mysql functions.
Was This Post Helpful? 0
  • +
  • -

#6 sabasi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 04-August 13

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 04 August 2013 - 09:22 PM

View Postandrewsw, on 04 August 2013 - 04:54 PM, said:

Added: Looking at your code again, you have:

<input type="hidden" name="react" value="delete_user" />

so the POST-variable react contains the value "delete_user", not the username. You should be looking at $_POST['user'].

I have change it to $_POST['user'] and though the item gets deleted from the database but the error "Warning:mysql_num_rows()expects parameter 1 to be resource,boolean given in C:\www\login\delete.php on line 15" shows and it also echos "no users are available to delete yet, sorry.",what should i do o get rid of this
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2884
  • View blog
  • Posts: 9,566
  • Joined: 12-December 12

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 04 August 2013 - 09:35 PM

I'll repeat the quote for mysql_query:

Quote

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

So if the DELETE is successful it returns TRUE. Check for this TRUE value and remove the use of mysql_num_rows.

This post has been edited by andrewsw: 04 August 2013 - 09:35 PM

Was This Post Helpful? 1
  • +
  • -

#8 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 04 August 2013 - 10:44 PM

*num_rows* is for SELECTS, for all others itís *affected_rows*. though if you only want to know about the query success, neither of them is necessary (cf. post above).
Was This Post Helpful? 1
  • +
  • -

#9 sabasi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 04-August 13

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 05 August 2013 - 02:02 AM

View Postandrewsw, on 04 August 2013 - 09:35 PM, said:

I'll repeat the quote for mysql_query:

Quote

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

So if the DELETE is successful it returns TRUE. Check for this TRUE value and remove the use of mysql_num_rows.

Ok I have removed the mysql_num_rows and made it like it
$query= "DELETE FROM users WHERE username = '".$username."'";
mysql_query($query,$connection);
echo("<strong>$username</strong>Successfully Deleted<p>");
	 }


now it works fine echos of successful delete,then what is the prob with mysql_num_rows ????

View PostDormilich, on 04 August 2013 - 10:44 PM, said:

*num_rows* is for SELECTS, for all others itís *affected_rows*. though if you only want to know about the query success, neither of them is necessary (cf. post above).


So you are saying that i don't have to use any of them,I could just echo after mysql_query() ?
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 05 August 2013 - 02:50 AM

View Postsabasi, on 05 August 2013 - 11:02 AM, said:

So you are saying that i don't have to use any of them,I could just echo after mysql_query() ?

and what if mysql_query() returns false?
Was This Post Helpful? 0
  • +
  • -

#11 sabasi  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 04-August 13

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 05 August 2013 - 10:52 AM

$query= "DELETE FROM users WHERE username = '".$username."'";
$qry = mysql_query($query,$connection);
if ($qry == false){
echo("there was a problem in deleting the user");
}
else{
echo("<strong>$username</strong>Successfully Deleted<p>");
	}
 }


Is it okay now ?

This post has been edited by andrewsw: 05 August 2013 - 10:55 AM
Reason for edit:: CODE tags corrected

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2884
  • View blog
  • Posts: 9,566
  • Joined: 12-December 12

Re: mysql_num_rows()expects parameter 1 to be resource,boolean given ?

Posted 05 August 2013 - 10:56 AM

@sabasi CODE tags are surrounded with square brackets - you can see this directly in the area where you are typing your post. You can also select your code and click the CODE button that appears in the panel above where you are typing.

Did you test it? But, yes, that is correct now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1