?
Solved

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

Posted on 2009-04-04
8
Medium Priority
?
596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 28

Accepted Solution

by:
gamebits earned 672 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 672 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 672 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 111

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

765 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