Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    not a valid MySQL result resource [was: I'm new and need quick help]

    Hiya, I was wondering if anybody can help.

    I have a website which i have a login script for however I want to users to be able to have an account balance. I have set up a field for balance in the users table of my database however I am having problems fetching the correct row for each user.


    This is my code:

    <?php

    $mysqlLink = mysql_connect( 'mysql2.freehostia.com' , 'My Username', 'My Password' );


    mysql_select_db( 'joesha1_members' , $mysqlLink );




    $result = mysql_query("SELECT * FROM `users` WHERE `username`= ".$_SESSION['username']." AND `password`=".$_SESSION['password']." ");



    while( $row = mysql_fetch_array($result) )
    {
    echo $row[ 'username' ] . $row[ 'balance' ];
    }


    mysql_close( $mysqlLink );

    ?>

    It keeps on displaying this error

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/joesha1.freehostia.com/account.php on line 19

    Thanks and hope i made any sence, Joe
    Last edited by guelphdad; 11-08-2006 at 11:17 PM. Reason: provide decent thread title

  • #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,633
    Thanks
    4
    Thanked 148 Times in 139 Posts
    look in the PHP forum for your specific error message. I'll move your thread there as PHP is the cause of the problem and not the query per se.

  • #3
    $object->toCD-R(LP); vinyl-junkie's Avatar
    Join Date
    Jun 2003
    Posts
    3,087
    Thanks
    2
    Thanked 23 Times in 23 Posts
    Something is probably wrong with your query. I would change around the script just a little to try and diagnose the problem. Take this statement:

    Code:
    $result = mysql_query("SELECT * FROM `users`
    WHERE `username`= ".$_SESSION['username'].
    " AND `password`=".$_SESSION['password']." ");
    and replace it with this:

    Code:
    $query = "SELECT * FROM `users` WHERE `username`= ".$_SESSION['username'].
    " AND `password`=".$_SESSION['password'];
    echo "query = ". $query . "<br>";
    $result = mysql_query($query) or die(mysql_error);
    This will hopefully show you if something is wrong in the way the query is setup and will show you the actual error.
    Music Around The World - Collecting tips, trade
    and want lists, album reviews, & more
    SNAP to it!

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    if your username and password are strings, then they need to be in quotes inside the query.
    i.e.
    PHP Code:
    $query="SELECT blah FROM table WHERE username='".mysql_real_escape_string($_SESSION['username'])."'"

  • #5
    fan
    fan is offline
    New Coder
    Join Date
    Jan 2003
    Location
    Romania
    Posts
    72
    Thanks
    1
    Thanked 0 Times in 0 Posts
    As a good practice it is good to print the query and see if it is what you expected (errors may occur when appending a variable contents which is actually empty).

    Therefore, I recommend:

    PHP Code:
    $query "SELECT * FROM `users` WHERE username = `".$_SESSION['username']."` AND password =`".$_SESSION['password']."` ";
    echo 
    $query//check if it's the expected query
    $result mysql_query($query); 
    P.S.: there's no need to quote the name of the fields (username, password), but the value you check it against unless it's integer
    Last edited by fan; 11-09-2006 at 08:17 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •