MySQL Lookup

Hi all, I am going crazy with this issue. When I want to lookup a row in a table on a MySQL databse like so:

[php]$search=“MadLife”;

$res = mysqli_query($mysqli, “SELECT * FROM Players WHERE player = $search”);
$row = mysqli_fetch_assoc($res);
echo $row[‘player_portrait’];[/php]

I get the following error:

However when I alter it to this:

[php]$search=“MadLife”;

$res = mysqli_query($mysqli, “SELECT * FROM Players WHERE player = ‘MadLife’”);
$row = mysqli_fetch_assoc($res);
echo $row[‘player_portrait’];[/php]

it works! >:(

Any ideas why I am having issues please? The column on the table is set to varchar(30).

I have used the following before and it worked fine!:

[php]$result = mysql_query(“SELECT * FROM item_table WHERE item_number = $search_query”);[/php]

Thanks

[php]$res = mysqli_query($mysqli, “SELECT * FROM Players WHERE player = ‘{$search}’”);[/php]

let me add that if you’re doing a Mysql search feature, you’d be better off using LIKE:
http://www.tutorialspoint.com/mysql/mysql-like-clause.htm

You need to use apostrophes, since player is a text field.
Also escape special chars from $search variable, like this:
[php]$res = mysqli_query($mysqli, “SELECT * FROM Players WHERE player = '”.mysqli_real_escape_string($search)."’");
[/php]
The last query in your post worked probably because item_number field was numeric, and $search_query was a number.


good job, spring! now you was faster than me :slight_smile:

Thanks to both of you, I managed to sort it now. Much appreciated.

Sponsor our Newsletter | Privacy Policy | Terms of Service