PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 306,950 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,917 people online right now. Registration is fast and FREE... Join Now!




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

 

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

greenbluekidz

6 Nov, 2009 - 09:38 PM
Post #1

New D.I.C Head
*

Joined: 5 Jan, 2009
Posts: 49


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

CODE


<?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:

CODE

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

User is offlineProfile CardPM
+Quote Post


Martyr2

RE: Warning: Mysql_num_rows() Expects Parameter 1 To Be Resource....

6 Nov, 2009 - 09:59 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 7,307



Thanked: 837 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
CODE

// 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!

smile.gif
User is offlineProfile CardPM
+Quote Post

greenbluekidz

RE: Warning: Mysql_num_rows() Expects Parameter 1 To Be Resource....

7 Nov, 2009 - 08:36 AM
Post #3

New D.I.C Head
*

Joined: 5 Jan, 2009
Posts: 49


My Contributions
Martyr2,

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

Unable to execute the query.

Error code 0:
User is offlineProfile CardPM
+Quote Post

Dannyboy997

RE: Warning: Mysql_num_rows() Expects Parameter 1 To Be Resource....

7 Nov, 2009 - 10:23 AM
Post #4

D.I.C Head
**

Joined: 17 Apr, 2009
Posts: 77



Thanked: 2 times
My Contributions
make sure all of your databases and tables, columns are there, if not then there is your error.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:46AM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month