3 Replies - 73408 Views - Last Post: 07 November 2009 - 11:23 AM Rate Topic: -----

#1 greenbluekidz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

Warning: mysql_num_rows() expects parameter 1 to be resource....

Posted 06 November 2009 - 10:38 PM

I am creating a simple login page using php and MySQL. I am new at this and cannot figure out why I am getting this error:

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in RegisterUser.php on line 53

Unable to execute the query.

Error code 0:

I am using xampp by apache friends 1.7.2. I have dumped my database and table several times. Everything appears to be as it should but I don't know where I am going wrong.


<?php
session_start();
if (empty($_GET['email']) || empty($_GET['email_confirm'])
	|| empty($_GET['password']) 
	|| empty($_GET['password_confirm']))
	exit("<p>You must enter values in all fields of the Registration form! Click your browser's Back button to return to the previous page.</p>");

else if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_GET['email']))
	exit("<p>You must enter a valid e-mail address! Click your browser's Back button to return to the previous page.</p>");

else if ($_GET['email'] != $_GET['email_confirm'])
	exit("<p>You did not enter the same e-mail address! Click your browser's Back button to return to the previous page.</p>");

else if ($_GET['password'] != $_GET['password_confirm'])
	exit("<p>You did not enter the same password! Click your browser's Back button to return to the previous page.</p>");

else if (strlen($_GET['password']) < 5 || strlen($_GET['password']) > 10)
	exit("<p>Your password must be between 5 and 10 characters! Click your browser's Back button to return to the previous page.</p>");

$DBConnect = @mysql_connect("localhost", "root")
	Or die("<p>Unable to connect to the database server.</p>"
	. "<p>Error code " . mysql_connect_errno()
	. ": " . mysql_connect_error()) . "</p>";

// select and create database
$DBName = "songorganizer";
if (!@mysql_select_db($DBName)) {
	$SQLstring = "CREATE DATABASE $DBName";
	$QueryResult = @mysql_query($SQLstring)
		Or die("<p>Unable to execute the query.</p>"
		. "<p>Error code " . mysql_errno($DBConnect)
		. ": " . mysql_error($DBConnect)) . "</p>";
	echo "<p>You entered your first song!</p>";
	mysql_select_db($DBName);
}

$TableName = "registered_users";
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysql_query($SQLstring);
if (!$QueryResult) {
 	$SQLstring = "CREATE TABLE $TableName (ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, email VARCHAR(40), password VARCHAR(10))";
 	$QueryResult = @mysql_query($SQLstring)
 		Or die("<p>Unable to create the table.</p>"
 		. "<p>Error code " . mysql_errno($DBConnect)
 		. ": " . mysql_error($DBConnect)) . "</p>";
}
$TableName = "registered_users";
$Email = addslashes($_GET['email']);
$Password = addslashes($_GET['password']);
$SQLstring = "SELECT * FROM $TableName";
$QueryResult = @mysql_query($DBConnect, $SQLstring);

if (mysql_num_rows($QueryResult) > 0) {
	$Row = mysql_fetch_row($QueryResult);
	do {
		if (in_array($Email, $Row))
			exit("<p>Hi, the e-mail address you entered is already
registered! Click your browser's Back button to
return to the previous page.</p>");
		$Row = mysql_fetch_row($QueryResult);
	} while ($Row);
	mysql_free_result($QueryResult);
}

$SQLstring = "INSERT INTO $TableName VALUES(NULL, '$Email',
'$Password')";
$QueryResult = @mysql_query($DBConnect, $SQLstring)
	Or die("<p>Unable to execute the query.</p>"
 		. "<p>Error code " . mysql_errno($DBConnect)
 		. ": " . mysql_error($DBConnect)) . "</p>";
$_SESSION['ID'] = mysql_insert_id($DBConnect);
mysql_close($DBConnect);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Song Organizer Site</title><img src="Butterfly/images/header.gif" width="875" height="50" alt="" title="" border="0" align="right" />
<link rel="stylesheet" href="php_styles.css" type="text/css" />
<meta http-equiv="content-type"
	  content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>Song Organizer</h1>
<h2>User Registration</h2>
<p>Your new user ID is <strong>
<?= $_SESSION['ID'] ?></strong>.</p>
<p><a href="UpdateContactInfo.php">Enter Contact Information</a></p>
</body>
</html>




This is actually the code for line 53:

if (mysql_num_rows($QueryResult) > 0) {



My table info is:

SQL result

Host: localhost
Database: songorganizer
Generation Time: Nov 07, 2009 at 01:35 AM
Generated by: phpMyAdmin 3.2.0.1 / MySQL 5.1.37
SQL query: show create table registered_users;
Rows: 1
Table registered_users
Create Table
CREATE TABLE `registered_users` (
`ID` smallint(6) NOT NULL AUTO_INCREMENT,
`email` varchar(40) DEFAULT NULL,
`password` varchar(10) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

Any ideas where it is that I am going wrong??

Is This A Good Question/Topic? 0
  • +

Replies To: Warning: mysql_num_rows() expects parameter 1 to be resource....

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: Warning: mysql_num_rows() expects parameter 1 to be resource....

Posted 06 November 2009 - 10:59 PM

// It should be $SQLstring first, $DBConnect second. Because they are wrong order
// This query is failing and thus returning "false" which is then causing the error
// in mysql
$QueryResult = @mysql_query($DBConnect, $SQLstring);

if (mysql_num_rows($QueryResult) > 0) {



As I already mention in the comments, you have your parameters in the mysql_query above it in the wrong order. The query is first, then the connection identifier. Since these are in the wrong order, your mysql_query is failing and thus returning false. This false is being passed to mysql_num_rows and causing the error.

Hope this helps!


Btw, this is a PHP question, so I will move it to the PHP forum. Thanks!

:)
Was This Post Helpful? 0
  • +
  • -

#3 greenbluekidz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 59
  • Joined: 05-January 09

Re: Warning: mysql_num_rows() expects parameter 1 to be resource....

Posted 07 November 2009 - 09:36 AM

Martyr2,

By making the change that you suggested, it is returning an error:

Unable to execute the query.

Error code 0:
Was This Post Helpful? 0
  • +
  • -

#4 Dannyboy997  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 64
  • View blog
  • Posts: 258
  • Joined: 17-April 09

Re: Warning: mysql_num_rows() expects parameter 1 to be resource....

Posted 07 November 2009 - 11:23 AM

make sure all of your databases and tables, columns are there, if not then there is your error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1