What's wrong with this code?

PythonPython Forum LeaderThe Royal RAM
I get an error with this bit of code but I cant see anything wrong with it:

[PHP]$regnum = "SELECT * FROM $dbprefix.`users`";
$regnum2 = mysql_query($regnum);
$regnum3 = mysql_num_rows($regnum2);
$regtotal = $regnum3;[/PHP]

It just counts the number of users in a database... ive tried changing the prefix so its not using a variable, ive tried rearranging it...

This is probably a very simple mistake that Im just not picking up on...

The error is a mysql error:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home2/pythonsb/public_html/forum/index.php on line 19


Any ideas?
thanks

The Royal Ram

Comments

  • martian2k4martian2k4 Llama Hunter Moderator
    Normally with a error like that its because it aint managing to execute the first query so you got sommat wrong with the first query i am guessing $db_prefix.
    Free MySpace Layouts- Coming soon
    Photoshop Tutorials- Coming soon
    Premium PHP Scripts- Coming soon

    Haha i should really do some work so i can remove all the coming soon's
  • martian2k4martian2k4 Llama Hunter Moderator
    i aint sure but maybe

    $regnum = "SELECT * FROM `$dbprefix.users`";
    $regnum2 = mysql_query($regnum);
    $regnum3 = mysql_num_rows($regnum2);
    $regtotal = $regnum3;
    Free MySpace Layouts- Coming soon
    Photoshop Tutorials- Coming soon
    Premium PHP Scripts- Coming soon

    Haha i should really do some work so i can remove all the coming soon's
  • PythonPython Forum Leader The Royal RAM
    no the line which is causing the problem is $regnum3 = mysql_num_rows($regnum2);

    What i dont understand is that taht exact piece of code i had working in a different file... perhaps ill try changing some of the db stuff...it shouldnt make a difference tho

    The Royal Ram

  • martian2k4martian2k4 Llama Hunter Moderator
    Ye i know it is the mysql_num_rows causing the problem but if the first querty dont work then mysql_num_rows wont b able to either :D
    Free MySpace Layouts- Coming soon
    Photoshop Tutorials- Coming soon
    Premium PHP Scripts- Coming soon

    Haha i should really do some work so i can remove all the coming soon's
  • PythonPython Forum Leader The Royal RAM
    lol. yeah i know... but what im saying is if the first query didnt work then there would be an error about that line too :)

    The Royal Ram

  • ChroderChroder Senior Member The Royal RAM
    Not necessarily. An error with your SQL would not generate a PHP error, it would simply return false. Try adding some error printing to your code.

    [php]$regnum2 = mysql_query($regnum) or die(mysql_error());[/php]

    Also, you should always check the validity of a mysql result before using it for anything. In this case, the 'or die' will stop the script on a query error, but often times you don't want such intrusive error messages popping up. So it's best to test the result (mysql_query will return false on error) before using it in other functions like mysql_num_rows.
  • martian2k4martian2k4 Llama Hunter Moderator
    Yes thats what i was gonna say next :p
    Free MySpace Layouts- Coming soon
    Photoshop Tutorials- Coming soon
    Premium PHP Scripts- Coming soon

    Haha i should really do some work so i can remove all the coming soon's
Sign In or Register to comment.