954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

mysql_numrows(): supplied argument is not a valid MySQL result resource

Hi, I'm quite new to PHP and MYSQL, but I have chosen to do an A-level project in it. This is just a basic registering page, which I've copied and modified from one out of a book. That one works, but this doesn't, saying that "mysql_numrows(): supplied argument is not a valid MySQL result resource". I hope that makes sense to someone.

<?php #Script 8.7 - register.php
//SEND NOTHING TO BROWSER BEFORE HEADER()

//Check if form is sumbitted
if (isset($_POST['submitted'])) {
    
    require_once('../mysql_pp_connect.php');
    
    $errors = array();
    
    //Validate crap
    if (empty($_POST['title'])) {
        $errors[] = 'You forgot to enter your first name.';
    } else {
        $t = escape_data($_POST['title']);
    }
    
    if (empty($_POST['first_name'])) {
        $errors[] = 'You forgot to enter your first name.';
    } else {
        $fn = escape_data($_POST['first_name']);
    }
    
    if (empty($_POST['last_name'])) {
        $errors[] = 'You forgot to enter your last name.';
    } else {
        $ln = escape_data($_POST['last_name']);
    }
    
    if (empty($_POST['addressline1'])) {
        $errors[] = 'You forgot to enter your address.';
    } else {
        $al1 = escape_data($_POST['addressline1']);
    }
    
    $al2 = escape_data($_POST['addressline2']);
    $al3 = escape_data($_POST['addressline3']);
    
    if (empty($_POST['postcode'])) {
        $errors[] = 'You forgot to enter your postcode.';
    } else {
        $pc = escape_data($_POST['postcode']);
    }
    
    if (empty($_POST['email'])) {
        $errors[] = 'You forgot to enter your email address.';
    } else {
        $e = escape_data($_POST['email']);
    }
    
    if (empty($_POST['phone_number'])) {
        $errors[] = 'You forgot to enter your phone number.';
    } else {
        $pn = escape_data($_POST['phone_number']);
    }
    
    if(!empty($_POST['password1'])) {
        if($_POST['password1'] != $_POST['password2']) {
            $errors[] = "Your passwords didn't match";
        } else {
            $p = escape_data($_POST['password1']);
        }
    } else {
        $errors[] = "You didn't enter a password!";
    }
    
    if (empty($errors)) {
        
        $query = "SELECT user_id FROM users WHERE email='$e'";
        $result = mysql_query ($query);
        if (mysql_num_rows($result) == 0) {        
        
             //Make query
             $query = "INSERT INTO customers (Title, FirstName, LastName, AddressLine1, AddressLine2, Addressline3, Postcode, EmailAddress, PhoneNumber, Password) VALUES ('$t', '$fn', '$ln', '$al1', '$al2', '$al3', '$pc', '$e', '$pn', SHA('$p'))";
             $result = @mysql_query ($query); //Run query
        
             if ($result) {
        
                  //send email blah blah
            
                    //redirect to thanks.php page
                    //start defining url
                    $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
            
                    if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\')) {
                        $url = substr($url, 0, -1); //chop off slashah.
                    }
            
                    $url .= '/thanks.php';
            
                    header("Location: $url");
                    exit();
        
            } else {
                $errors[] = 'You could not be registered due to a system error. oops. sorry!';
                $errors[] = mysql_error() . '<p>Query ' . $query;
            }
        
    } else {
        $errors = 'The email address has already been registered!';
    }
        
}
mysql_close();

}else{
    $errors = NULL;
} //end of submit conditional
$page_title = 'Register';
include ('./header.html');

if(!empty($errors)){
    echo '<h1 id="mainhead">Error!</h1><p class="error">The following errors occurred:<ul>';
    foreach ($errors as $msg) {
        echo "<li>$msg</li>";
    }
    echo '</ul><p>Please try again';
} //end of if empty errrors if
 ?>


Cheers

mickeymoose
Newbie Poster
2 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

Your query probably fails, try debugging:
[php]
$result = mysql_query ($query) or die(mysql_error());
[/php]

php_daemon
Junior Poster
140 posts since Aug 2006
Reputation Points: 13
Solved Threads: 2
 

Yay, thanks. That was it, I'd forgotten my table was called something different. Thanks again.

mickeymoose
Newbie Poster
2 posts since Jan 2007
Reputation Points: 10
Solved Threads: 0
 
Your query probably fails, try debugging: [php] $result = mysql_query ($query) or die(mysql_error()); [/php]


I have the same problem and I check all the things and precautions. My code is ok. But I dont know what is the problem. My battery is gone down and I just want to post it to get answers when I came again. So please help I also debug it, and that's ok. Help it mess up my time.

Thanks

chand.
Junior Poster in Training
57 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
I have the same problem and I check all the things and precautions. My code is ok. But I dont know what is the problem.

Chand, your code is obviously NOT ok. :-) Please paste your code in this or another thread for us to review.

spthorn
Newbie Poster
21 posts since Sep 2007
Reputation Points: 10
Solved Threads: 5
 

I have the same problem and I check all the things and precautions. My code is ok. But I dont know what is the problem. My battery is gone down and I just want to post it to get answers when I came again. So please help I also debug it, and that's ok. Help it mess up my time.

Thanks


Read the FAQ dealing with this problem instead of reviving 2 year old threads.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 
Chand, your code is obviously NOT ok. :-) Please paste your code in this or another thread for us to review.

//index.php

<! DOCTYPE HTML PUBLIC "W3C//DTD// HTML 4.01//TRANSITIONAL//EN">

<HTML>
<HEAD>
<TITLE>Wish to visualize!</TITLE>
</HEAD>

<BODY>

<form action="wishlist.php" method="GET" name="wishList">
      Show wish list of: <input type="text" name="user"/>
		<input type="submit" value="Go" />
</form>

<?php


?>
</BODY>

</HTML>


//wishlist.php

<! DOCTYPE HTML PUBLIC "W3C//DTD// HTML 4.01//TRANSITIONAL//EN">


<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
    <body>
        Wish List of <?php echo  $_GET["user"]."<br/>";?>
        
		<?php

		$con = mysql_connect("localhost", "root", "");
		if(!$con){
			die('Could not connect!' .mysql_error());
		}else{
			echo("connected!");
		}
		mysql_query("SET NAMES 'utf8'");   
		
		if(mysql_select_db("wishlist", $con)){
			echo("DB is selected.<del>");
		}
		
		$wisher = mysql_query("SELECT id FROM wishes WHERE description = '".mysql_real_escape_string($_GET["user"])."'");
		
		if(!($wisher)){
			die(mysql_error());
		}
		
		if(mysql_num_rows(($wisher)<1)){
			die("The specified person".$_GET["user"]. "is not exist here");
		}
		
		$wisherId = mysql_result($wisher, 0);
	

		
		       
        
        ?>
        
    </body>
    </html>
chand.
Junior Poster in Training
57 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

The following line has parenthesis issues:

if(mysql_num_rows(($wisher)<1)){

It should be: if(mysql_num_rows($wisher)<1){

spthorn
Newbie Poster
21 posts since Sep 2007
Reputation Points: 10
Solved Threads: 5
 

Thank you so much for your help. I'm very grateful to you. I'm a novice could you tell me about good learning resources for php.

Thanks

chand.
Junior Poster in Training
57 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Since your bug was a syntax one, you might consider an IDE. See http://www.ibm.com/developerworks/library/os-php-ide/index.html for a list of suggestions. However, there can be a tendency to rely on the IDE to fix your mistakes. I personally use a simple text editor to write code, which forces me to learn and memorize syntax and base library calls, which I think is a good thing. I can focus more on writing good code than on language technicalities

There are many ways to learn. Online I often recommend http://www.w3schools.com/PHP/DEfaULT.asP as a starter, but a good book would work as well. code, code, code.

spthorn
Newbie Poster
21 posts since Sep 2007
Reputation Points: 10
Solved Threads: 5
 


Select Expansion !


Select your WoW Expansion :


World of Warcraft
The Burning Crusade
Wrath of the Lich King (Includes TBC)


Username


Password


E-Mail


World of WarcraftThe Burning CrusadeWrath of the Lich King


YourServer © 2008. All rights

reserved.

looxlmc
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 


Select Expansion !


Select your WoW Expansion :


World of Warcraft
The Burning Crusade
Wrath of the Lich King (Includes TBC)


Username


Password


E-Mail


World of WarcraftThe Burning CrusadeWrath of the Lich King


YourServer © 2008. All rights reserved.

looxlmc
Newbie Poster
2 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You