Solved

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

Posted on 2009-04-04
8
551 Views
Last Modified: 2013-12-12
for some reason i get this error Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource for here $lastMessageTime = mysql_num_rows($res) == 1 ? array_shift(mysql_fetch_array($res)) :



i dont understand as all it should be doing is finding if user is flooding :S

here is full code

<?php

session_start();

require_once '../../settings.php';

include "../../info.php"; // sets username/id ect

$reciever = mysql_real_escape_string($_POST['username']);

$subject = mysql_real_escape_string($_POST['subject']);

$message = mysql_real_escape_string($_POST['message']);

$timestamp = time();
 

if (!isset($_REQUEST['Submit'])) {

//stop flooding

$query = "SELECT max(`time`) as t FROM messages WHERE receiver='$receiver' AND username='$username'";

$res = mysql_query($query);

$lastMessageTime = mysql_num_rows($res) == 1 ? array_shift(mysql_fetch_array($res)) :

    0;

if ($lastMessageTime > 0 && $lastMessageTime < strtotime('+2 Minutes', $timestamp)) {

    $errors[] = 'Please wait 2 minutes between each message';

}

    //Get their private message count

    $sql = mysql_query("SELECT pm_count FROM users WHERE Username='$reciever'");

    $row = mysql_fetch_array($sql);

    $pm_count = $row['pm_count'];

    //You cant have more than 50 private messages, if they try sending a message to a user with a full inbox return an error message

    if (pm_count >= '1') {

        $errors[] = 'The user you are trying to send a message to has 50 private messages, sorry but we cant send your message untill that user deletes some of their messages.';

    }

   //find errors in form

    if ($_POST['subject'] == '' || strlen($_POST['subject']) < 3) {

        $errors[] = 'A subject is required and must contain 3 characters or more';

    }

    if ($_POST['message'] == '' || strlen($_POST['message']) < 3) {

        $errors[] = 'A message is required and must contain 3 characters or more';

    }

    if (is_array($errors)) {

        echo '<p class="error"><b>The following errors occured</b></p>';

        while (list($key, $value) = each($errors)) {

            echo '<span class="error">' . $value . '</span><br />';

        }

    } else {

        //Are the trying to send a message to a real user or to something they just made up?

        $user_check = mysql_query("SELECT * FROM `friends` WHERE `username`= '$username' and friendname = '$reciever' ");

        $user_check = mysql_num_rows($user_check);

        //The user is real and not made up if this is true

        if ($user_check > 0) {

            $pm_count = $pm_count + '1';

            //Update Them

            $userpmupdate = mysql_query("UPDATE users SET pm_count='$pm_count' WHERE username='$reciever'") or

                die(mysql_error());

            ;

            $update = mysql_query("INSERT INTO messages (time,reciever, sender, subject, message) VALUES('$timestamp','$reciever', '$username', '$subject', '$message')") or

                die(mysql_error());

            echo 'Your profile has been update!';

        } else {

            echo 'You may only message friends!';

        }

    }

}

?>

Open in new window

0
Comment
Question by:runnerjp
8 Comments
 
LVL 28

Accepted Solution

by:
gamebits earned 168 total points
ID: 24066995
The message actually just mean the query didn't work cause could be

- You are not connected to the db
- You mispelled a field name
- You are querying the wrong table
etc.
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 168 total points
ID: 24066997
After line 13, add this:

if(!$res) die("Query: $query\nError: ".mysql_error());
0
 
LVL 10

Assisted Solution

by:cyberstalker
cyberstalker earned 168 total points
ID: 24067020
There is a misspelling in your code. At line 5 you put data in a variable called $reciever and in line 12 you read from $receiver.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24067049
This is not related to your current problem, but you probably need to convert your `time`column to a unix timestamp. you can do that in the SQL like this:
SELECT max(unix_timestamp(`time`)) as t ...

Open in new window

0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24067064
Hello,

Use this query:

$query = "SELECT max('time') as t FROM messages WHERE receiver='$receiver' AND username='$username'";


 instead of

 $query = "SELECT max(`time`) as t FROM messages WHERE receiver='$receiver' AND username='$username'";

The difference is ('time') instead of (`time`).

I think the error because of the single quotation mark mismatch.

Hope this will Help you.

Regards.
0
 
LVL 14

Expert Comment

by:shobinsun
ID: 24067077
Hello,

If that mismatch is not rectified your problem then check that you are using correct table and correct fields.

" mysql_num_rows(): supplied argument is not a valid MySQL result resource "   means that there is no result to be fetched when executing your query.

Regards.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 24068029
We see a lot of "unknown conditions" here at EE.  Many of these result from using MySQL without knowing how to test for success or failure and/or where to look for the error messages.  Please read over the code snippet.  It teaches how to deal with the construction and execution of basic queries.  These coding constructs will help you find and report any errors.

Hopefully you can apply this to your own code and the issues will be easy to resolve.

HTH, ~Ray
<?php // RAY_mysql_example.php

error_reporting(E_ALL);
 
 

// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE

$db_host = "localhost"; // PROBABLY THIS IS OK

$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY

$db_user = "??";

$db_word = "??";
 

// CONNECT TO THE DATA BASE SERVER

if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))

{

   $errmsg = mysql_errno() . ' ' . mysql_error();

   echo "<br/>NO DB CONNECTION: ";

   echo "<br/> $errmsg <br/>";

}
 

// SELECT THE DATA BASE

if (!$db_sel = mysql_select_db($db_name, $db_connection))

{

   $errmsg = mysql_errno() . ' ' . mysql_error();

   echo "<br/>NO DB SELECTION: ";

   echo "<br/> $errmsg <br/>";

   die('NO DATA BASE');

}
 
 
 

// MAKING A QUERY AND TESTING THE RESULTS

$sql = "SELECT id FROM my_table WHERE username='$username'";

$res = mysql_query($sql);
 

// IF mysql_query() RETURNS FALSE, THERE IS DIAGNOSTIC INFORMATION AVAILBALE

if (!$res)

{

   $errmsg = mysql_errno() . ' ' . mysql_error();

   echo "<br/>QUERY FAIL: ";

   echo "<br/>$sql <br/>";

   die($errmsg);

}

// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN USE $res IN OTHER MYSQL FUNCTIONS
 
 
 

// DETERMINE HOW MANY ROWS OF RESULTS WE GOT

$num = mysql_num_rows($res);

if (!$num)

{

   echo "<br/>QUERY FOUND NO DATA: ";

   echo "<br/>$sql <br/>";

}

else

{

   echo "<br/>QUERY FOUND $num ROWS OF DATA ";

   echo "<br/>$sql <br/>";

}
 
 
 

// ITERATE OVER THE RESULTS SET TO SHOW WHAT WE FOUND

echo "<pre>\n"; // MAKE IT EASY TO READ

while ($row = mysql_fetch_assoc($res))

{

   var_dump($row);

}

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now