• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 602
  • Last Modified:

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

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
runnerjp
Asked:
runnerjp
3 Solutions
 
gamebitsCommented:
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
 
Roger BaklundCommented:
After line 13, add this:

if(!$res) die("Query: $query\nError: ".mysql_error());
0
 
cyberstalkerCommented:
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.

 
Roger BaklundCommented:
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
 
shobinsunCommented:
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
 
shobinsunCommented:
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
 
Ray PaseurCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now