#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep Power
    0

    Having trouble fetching data using mysql_fetch_assoc


    Hello,
    I writing a script that is supposed to fetch specific user data and display it in an array. I don't know if I am writing the query wrong. I've tried using ' and just leaving the fields blank but either way I get a Boolean message.

    PHP Code:
    $fields ' `' implode(' `'$func_get_args) . ' `';
    $data mysql_fetch_assoc(mysql_query("SELECT $fields FROM users WERE user_id = $user_id"));

    print_r($data);
    die(); 
    here is the error...

    Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\core\functions\users.php on line 13

    I don't see the Boolean, any suggestions?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    mysql_query() returns a boolean "false" in case the query is incorrect. So obviously there's something wrong with the SQL. Output it and check it. You can also test it in phpmyadmin.

    Shouldn't the "WERE" be a "WHERE"? And what's with the strange $func_get_args? Are you sure it's not func_get_args() as a function call? And shouldn't you put commas between the fields?
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2002
    Location
    Seattle, U.S.A.
    Posts
    712
    Rep Power
    12
    If you check for errors in your code and output the mysql_error report, then you'll get some more information.

    For example (from the php.net/mysql_query page )

    PHP Code:
    $result mysql_query($query);

    if (!
    $result) {
        
    $message  'Invalid query: ' mysql_error() . "\n";
        
    $message .= 'Whole query: ' $query;
        die(
    $message);

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep Power
    0
    Ah, well the typo might be part of the problem. well, I can echo the SQL query and it will display the results in an array...the problem is when I try to enclose the query in the mysql_fetch_assoc function.

    PHP Code:
    echo "SELECT $fields FROM users WHERE user_id = $user_id"
    PHP Code:
    <?php 
    function user_data($user_id) {
        
    $data = array();
        
    $user_id = (int)$user_id;
        
        
    $func_num_args func_num_args();
        
    $func_get_args func_get_args();
        
        if (
    $func_num_args 1) {
            unset(
    $func_get_args[0]);
            
            
    $fields ' `' implode(' `'$func_get_args) . ' `';
            
    $data mysql_fetch_assoc(mysql_query("SELECT $fields FROM users WHERE user_id = $user_id"));
            
            
    print_r($data);
            die();
            
            return 
    $data;
        }
    }

    function 
    logged_in() {
        return (isset(
    $_SESSION['user_id'])) ? true false;
    }

    function 
    user_exists($username) {
        
    $username sanitize($username);
        return (
    mysql_result(mysql_query("SELECT COUNT(user_id) FROM users WHERE username ='$username'"), 0) == 1) ? true false;
    }
    function 
    user_active($username) {
        
    $username sanitize($username);
        return (
    mysql_result(mysql_query("SELECT COUNT(user_id) FROM users WHERE username ='$username' AND active ='1'"), 0) == 1) ? true false;

    }

    function 
    user_id_from_username($username){
        
    $username sanitize($username);
        return 
    mysql_result(mysql_query("SELECT user_id FROM users WHERE username ='$username'"), 0'user_id');
    }

    function 
    login($username$password) {
        
    $user_id user_id_from_username($username);
        
        
    $username sanitize($username);
        
    $password md5($password);
        
        return (
    mysql_result(mysql_query("SELECT COUNT(user_id) FROM users WHERE username ='$username' AND password = '$password'"), 0) == 1) ? $user_id false;
    }

    ?>
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep Power
    0
    Ill try the error reporting in a bit after some tasty bbq.
    I'm sure its something simple. By the way, this code is from a login script from alex at php tutorials. Found it on youtube. There are several parts to it. It has a forum but I can't find anyone who has had a problem on this line.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by codeman061988
    PHP Code:
    echo "SELECT $fields FROM users WHERE user_id = $user_id"
    And what does that say? What happens when you execute the query in phpmyadmin?

    And to ask once again: Should there not be commas between the fields? If I call your method with multiple args, I get this:
    Code:
    SELECT `field1 `field2 `field3` ...
    That's obviously not right. The implode() separator must be "`,`" with two backticks and a comma in between.

    Like I already said: A boolean "false" means that your query is wrong. It is not a problem of mysql_fetch_assoc. That's just were the error will take effect.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep Power
    0
    Well,
    I ran the query in phpmyadmin and it resulted in this error...

    #1054 - Unknown column '$fields' in 'field list'

    I fixed the separator problem in the implode function.
    In the tutorial, the guy echos the query and get the fields in an array which is good but placing the query in mysql_fetch_assoc is supposed to grab the field values to be displayed later.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep Power
    0
    I guess I need to format the implode correctly when I assign it to to $fields.

    PHP Code:
    $fields "`,`" implode("`,`" $func_get_args) . "`,`"
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by codeman061988
    Well,
    I ran the query in phpmyadmin and it resulted in this error...

    #1054 - Unknown column '$fields' in 'field list'
    Now you've taken the PHP string expression from the code. You need to actually output it with "echo" and use the evaluated result. Raw PHP code with variables in it won't work as an SQL query.

    Echo the query and copy it here into the forum. Then it shouldn't be too hard to find out why it doesn't work.



    [QUOTE=codeman061988]
    Originally Posted by codeman061988
    I guess I need to format the implode correctly when I assign it to to $fields.

    PHP Code:
    $fields "`,`" implode("`,`" $func_get_args) . "`,`"
    Now your query looks like this:
    Code:
    SELECT `,`col1`,`col1`,` ...
    This cannot possibly work.

    You need commas between the fields, but not before and after the fields.
    Last edited by Jacques1; November 25th, 2012 at 06:16 PM.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep Power
    0
    And the winner is....

    PHP Code:
    $fields '`' implode('`, `'$func_get_args) . '`'
    my punctuation was wrong for this array.

    Here is the result I was looking for...

    Array ( [user_id] => 1 [username] => codeman061988 [password] => 5f4dcc3b5aa765d61d8327deb882cf99 [first_name] => Cody [last_name] => Hicks [email] => codyhicks57@hotmail.com )


    This is actually a cool script for login. It's recent too. It teaches you how to separate header/footer and several logic components....

    http://www.youtube.com/watch?v=9kyQGBABA38&feature=BFa&list=ECE134D877783367C7

    I appreciate your help!

IMN logo majestic logo threadwatch logo seochat tools logo