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

Join 150,183 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 2,130 people online right now. Registration is fast and FREE... Join Now!




PHP warning message

 
Reply to this topicStart new topic

PHP warning message

dontKnowJava
26 Aug, 2008 - 11:03 AM
Post #1

D.I.C Head
**

Joined: 29 Sep, 2007
Posts: 213


My Contributions
im working on a script and get this error message when i run it.
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/s/t/o/stockscout/html/index.php on line 513

here's the code that makes the eror evn though it posts to the db

CODE

$query = 'SELECT email FROM members WHERE email  = '.'\''.$_POST['em'].'\'';
                    $result = mysql_query($query);

                    
                    if(mysql_num_rows($result) == 0)    {
                        $insert ="INSERT INTO `members` (fname, lname, address, city, state, zip, phone, em, pass) VALUES ('".$_POST['fname']."', '".$_POST['lname']."', '".$_POST['address']."', '".$_POST['city']."', '".$_POST['state']."', '".$_POST['zip']."', '".$_POST['phone']."', '".$_POST['em']."', '".$pw."')";

                        $insert2 = mysql_query($insert);

                        if(!$insert2)    {
                            die(mysql_error());
                        }
                        
        

                    }



any ideas?
User is offlineProfile CardPM
+Quote Post

AdaHacker
RE: PHP Warning Message
26 Aug, 2008 - 11:29 AM
Post #2

D.I.C Head
**

Joined: 17 Jun, 2008
Posts: 194



Thanked: 33 times
My Contributions
That message means your select query is failing. Why it's failing, I don't know.

The reason your data gets posted to the database anyway is that you're testing the return value of mysql_num_rows() against zero. If you read the documentation, you'll see that mysql_num_rows() returns false on failure, and false is equivalent to 0 in PHP, which means that your if condition is true when mysql_num_rows() fails.

Incidentally, I didn't see where you're using the results of that first select query other than in the call to mysql_num_rows(). In general, it's considered bad practice to use a select query and mysql_num_rows() just to get the number of rows a query returns. Doing that forces MySQL to fetch the entire data set just to count the rows. It's much more efficient to get the row count with a SELECT COUNT(*) WHERE ... query. Of course, if you're actually using the select results, that's different.
User is offlineProfile CardPM
+Quote Post

JBrace1990
RE: PHP Warning Message
26 Aug, 2008 - 02:45 PM
Post #3

D.I.C Regular
Group Icon

Joined: 9 Mar, 2008
Posts: 479



Thanked: 24 times
Dream Kudos: 350
My Contributions
you have an unexpected T_?WHITESPACE in your query.... you never escaped the single quotes...

php
$query = "SELECT email FROM members WHERE email  = '$_POST['em']'";

User is offlineProfile CardPM
+Quote Post

AdaHacker
RE: PHP Warning Message
26 Aug, 2008 - 05:27 PM
Post #4

D.I.C Head
**

Joined: 17 Jun, 2008
Posts: 194



Thanked: 33 times
My Contributions
QUOTE(JBrace1990 @ 26 Aug, 2008 - 05:45 PM) *

you have an unexpected T_?WHITESPACE in your query.... you never escaped the single quotes...

Huh? Where are you getting that from? The quotes are escaped correctly. The syntax is he's using is rather...unwieldy (your version is much better), but it works. In fact, I tried running the code through php -l and it comes back clean - no syntax errors.
User is offlineProfile CardPM
+Quote Post

Moonbat
RE: PHP Warning Message
26 Aug, 2008 - 06:17 PM
Post #5

D.I.C Regular
Group Icon

Joined: 30 Jun, 2008
Posts: 399



Thanked: 23 times
Dream Kudos: 600
My Contributions
dontKnowJava, I know this doesn't relate to your question, but if I were you I would filter all of the $_POST variables being sent to your query with mysql_real_escape_string() so nobody can do a DROP DATABASE on you smile.gif

This post has been edited by Moonbat: 26 Aug, 2008 - 06:17 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:56AM

Be Social

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

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month