Domanda

I have a simple script to check how many files a user has uploaded:

$sql = "SELECT * from $username where file IS NOT NULL";
$result = mysql_query($sql);
$count=mysql_num_rows($result);

If the table $username does not have any rows where file is not null it spits out:

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

What can I do to avoid this? Is there an easy way to simply suppress this error message from displaying? or a simple "if" statement that could prevent the SQL query from happening in the first place?

È stato utile?

Soluzione

You could do something like this:

<?php
 $sql = "SELECT * from $username where file IS NOT NULL";
 $result = mysql_query($sql);
 if($result) 
 {
     $count=mysql_num_rows($result);
 }
 else 
 {
     $count = 0;
  }

OR use a shorthand:

<?php 
 $count = ($result) ? mysql_num_rows($result) : 0;

Note:

Like you can see in other comment's, this is just an answer to your question, taking care of one problem at a time. The usage of $username here can be tricky, especially if it's dynamic or user generated content. It's best to look at some long term improvements to the way you interact with the database, but here's the fix for now ;-)

PS. If you're not feeling like learning all the new stuff, perhaps a framework, something like Codeigniter could help you. Using it's active record class is easy and it takes care of a lot of things itself.

Altri suggerimenti

Suppressing the errors is the last thing you want to do. You should be handling them.

$sql = 'SELECT * FROM ' . mysql_real_escape_string($username) . ' WHERE `file` IS NOT NULL';
$result = mysql_query($sql);

if ($result !== false) {
    $count=mysql_num_rows($result);
} else {
    // An error occurred, handle it here.
}

Also, it is terrifying that you are using dynamic table names. Don't do this!

In addition, you are likely wide open to SQL injection. Learn to use prepared queries with PDO to avoid this problem.

In simplest way you can do mysql_query($sql) or die("invalid sql"); or put a try / catch.

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('database_name')) {
    die('Could not select database: ' . mysql_error());
}
$result = mysql_query('SELECT name FROM work.employee');
if (!$result) {
    die('Could not query:' . mysql_error());
}
echo mysql_result($result, 2); // outputs third employee's name

mysql_close($link);
?>

I think above code from http://www.php.net/manual/en/function.mysql-result.php speaks what you need.

Here, if (!$result) {...} is the key.

A simply way to suppress this error is putting a @ before mysql_query and mysql_num_rows, but it is not recommended, you should make a proper error treatment

$result will be false if you had an error running your query so check for that before trying to use it:

if $result === false)
{
     // stuff went boom!
}

You could try the @ symbol. So your call to mysql_query would now look like this: $result = @mysql_query($sql);.

Update

As has been stated elsewhere (specifically as a comment to this answer), you should not suppress errors unless you know what you are doing.

Update 2

Here are the docs for the Error Control Operators

if( !is_resource($result) )

See the manual on is_resource().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top