I'm trying to pull member info from a database and get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' FROM users WHERE user_id = 0' at line 8 That user does not exist.

I am not sure what to change as I am new to PHP.

My code with the error is:

<?php

function fetch_users() {

    $result = mysql_query('SELECT `user_id` AS `id`, `user_username` AS `username` FROM `users`');

    $users = array();

    while (($row = mysql_fetch_assoc($result)) !== false) {
        $users[] = $row;
    }

    return $users;
}

function fetch_user_info($uid) {
    $uid = (int)$uid;

    $sql = "SELECT 
                `user_username` AS `username`,
                `user_firstname` AS `firstname`,
                `user_lastname` AS `lastname`,
                `user_email` AS `email`,
                `user_about` AS `about`,
                `user_location` AS `location`,
                `user_gender` AS `gender`'
            FROM `users`
            WHERE `user_id` = {$uid}";

    $result = mysql_query($sql);            

    /*Echo mysql_error();*/


    return mysql_fetch_assoc($result);          
} ?>
share|improve this question

67% accept rate
feedback

2 Answers

In the second query, at the end of the last line before the FROM clause, there is a single quote (') that is not supposed to be there, try removing it:

`user_gender` AS `gender`'
FROM `users`

Also, that is exactly what MySQL is telling you in its error message: ''FROM users WHERE user_id = 0' at line 8

Note the double single quote (what a term) at the beginning of the query sample in the message: the first one marks the start of the sample (it is always enclosed by two single quotes) and the second one is already the token causing trouble.

share|improve this answer
feedback

As far as I can tell, it looks like you have a single quote immediately following the backtick after gender. Other than that, I think it would work.

share|improve this answer
sorry about that here is my new error message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM users WHERE user_id = '0'' at line 9 – user1155141 Feb 11 at 21:01
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.