0

I'm working on a project where I store an id in a table, which is linked with a filename. Upon requesting the id, SQL returns the filename. Yeah, Redis would be best for this sort of thing, but I don't know how to get that working with PHP.

Anyhow, the following code returns an empty result:

<?php
    mysql_connect("localhost","USER","PASSWORD") or die("Unable to connect to SQL server");                                                                                                              
    mysql_select_db("DATABASE") or die("Unable to select the database");
    $scaped = mysql_real_escape_string($_GET["id"]);
    $res = mysql_query("SELECT fn from links WHERE id=$scaped");
    mysql_close();
    echo $res;

?>

I know the key I'm using when requesting the page exists, but I get an empty result and no error. What's going on?

EDIT: I get:

Unknown column 'rytughguyig78iu786546789' in 'where clause'

Where the long string is my id. My where clause is as is. What's wrong there?

4
  • add echo mysql_error(); before the mysql_close(); line. what happens?
    – Taha Paksu
    Mar 25, 2012 at 2:18
  • @tpaksu, still a whole lotta nothing
    – tekknolagi
    Mar 25, 2012 at 2:20
  • Wait. There is something. May just be stupidity. Hang on a sec.
    – tekknolagi
    Mar 25, 2012 at 2:20
  • type '$scaped' instead of $scaped Mar 25, 2012 at 2:24

3 Answers 3

3

change your line

$res = mysql_query("SELECT fn from links WHERE id=$scaped");

to

$res = mysql_query("SELECT fn from links WHERE id='".$scaped."' limit 0,1");

and by printing out the record:

$result = mysql_fetch_row($res);
print_r($result);
1
  • If you include the fetch_assoc bit, I'll accept, but it was a combination of two answers that fixed it.
    – tekknolagi
    Mar 25, 2012 at 2:25
1

Change

"SELECT fn from links WHERE id=$scaped"

to

"SELECT fn from links WHERE id='$scaped'"

1

mysql_result will return some sort of resultset object. so you need to: 1.) Check the resultset object has record 2.) fetch the result record from resultset 3.) print it.

sample like:

$res = mysql_query("SELECT fn from links WHERE id='$scaped'");
if( mysql_num_rows( $res ) >0){
    $record = mysql_fetch_assoc($res); //other options: mysql_fetch_object, mysql_fetch_row
    print $record['fn'];
}

links: mysql_num_rows, mysql_fetch_assoc, mysql_fetch_object, mysql_fetch_row

Answer updated after your error message: change $scaped to '$scaped' for string comparision

2
  • It would still be wrong this way because he needs id='$scaped' otherwise its still trying to compare id to another column
    – csteifel
    Mar 25, 2012 at 2:28
  • @legion: i admit $scaped should be encoded with single quote its data type of id is string, but since op didn't state about data of id, i assumed it as integer.
    – KoolKabin
    Mar 25, 2012 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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