fredkid
fredkid

Reputation: 71

MySql Unknown column in where

I am a beginner and try to have a very simple messaging system. every thing is managed via the ID's of the users..

So if someone types the username of a person i have to convert it into the person's id:

$res_name = $_POST["res_name"];
    $res_id = userToId($res_name);

This function looks like this:

function userToId($username) {
    $data = array();
    $func_get_args = func_get_args();

    if($func_num_args = 1)
    {
    $res = mysql_query("SELECT id FROM `z_network_users` WHERE `username` = `$username`") or die(mysql_error());
    $data = mysql_fetch_assoc($res);
    }
    return $data['id'];
}

it doesn't work, in fact it can't find a username even if it's existing..

the error is for example:

Unknown column 'testperson' in 'where clause'

the user exists, hope you can help me :) ps: I know I shouldn't use mysql_* but i haven' learned the better version.. ;)

Upvotes: 0

Views: 169

Answers (7)

echo_Me
echo_Me

Reputation: 37243

When using backticks around your variable then it will be considered as column

thats why you got that error Unknown column 'testperson' in 'where clause'

you should use this (you should escape your variable to prevent sql injection)

 $username = mysql_real_escape_string($username);
 $res = mysql_query("SELECT id FROM `z_network_users` 
        WHERE `username` = '$username' ") or die(mysql_error());

or this

 $username = mysql_real_escape_string($username);
 $res = mysql_query("SELECT id FROM `z_network_users` 
        WHERE `username` = '".$username."' ") or die(mysql_error());

try that function:

 function userToId($username) {

   $func_get_args = func_get_args();
    $username = mysql_real_escape_string($username);
   if($func_num_args == 1)
{
   $res = mysql_query("SELECT id FROM `z_network_users` WHERE `username` = '$username' ") or die(mysql_error());
   $row = mysql_fetch_assoc($res);

}
  return $row['id'];
}

Upvotes: 0

Nurdin
Nurdin

Reputation: 23891

I think your query return more then 1. you need to use mysql_num_rows() to get one record. Thats why it doesn't work.

Upvotes: 0

Rakesh Sharma
Rakesh Sharma

Reputation: 13738

you have not any column but

WHERE `username` = `$username`"

but $username understanding as a column so remove `` from $username and put single quote to it like '$username'

Upvotes: 0

deceze
deceze

Reputation: 522577

You only quote column names with backticks.

WHERE `username` = `$username`

This makes $username into a column name. You want a string instead:

WHERE `username` = '$username'

Also definitely see The Great Escapism (Or: What You Need To Know To Work With Text Within Text).

Upvotes: 0

Nambi
Nambi

Reputation: 12042

You should not be backticking the entries

$res = mysql_query("SELECT id FROM `z_network_users` WHERE `username` = '$username'") or die(mysql_error());

Upvotes: 0

MSadura
MSadura

Reputation: 1042

"SELECT id FROM `z_network_users` WHERE `username` = '$username'"

this will fix your query BUT mysql_* functions are deprecated, so you should avoid using them. Try mysqli or PDO instead.

Upvotes: 0

Mostafa Talebi
Mostafa Talebi

Reputation: 9183

You should not use around yourvalue` I think.

Upvotes: 1

Related Questions