从PHP中的表中获取主题

OK So I'm trying to access a table called emg_quote I have the Quote ID so Im trying to get the Column Subject from the same row as this ID but for some reason All I'm getting is the first row in the entire table? Can any one figure out what I'm doing wrong? Here is my coding:

$row['quote_id'] = quoteTitle($row['quote_id']);

function quoteTitle($quoteid){
    global $db;

    $sql = "SELECT subject FROM emg_quote WHERE ".$quoteid."";
    $res = $db->query($sql);
    $row = $db->fetch_row();

    $output = $row['subject'];

    return $output;
}

You might be missing the where column.

$sql = "SELECT subject FROM emg_quote WHERE quote_id=".$quoteid."";
                                            ^^^^^^^^

We also do not see weather something with your Db class is wrong.

You should in any case not directly put request variables into a database query.

Are you using a custom object to wrap the native API's?

Either way it doesn't look right to me. You don't seem to be using the result of the query.

i.e.

$result = $mysqli->query($query);
$row = $result->fetch_row();

You have few bad practices in your code.

A. You lie on $quoteid to give you the correct where syntax. ie: ID=123 This is an highly unsafe method, because the user can change the it to Some-Important-Details='bla' To extract more details from this table or others.

B. You should ALWAYS escape characters when receiving data from user, otherwise you easily subjected to SQL-Injections. And believe me you don't want it.

you have to use the checking after where. use you column name before your $quoteid variable

$row['quote_id'] = quoteTitle($row['quote_id']);

function quoteTitle($quoteid){
global $db;

$sql = "SELECT subject FROM emg_quote WHERE quoteid=".$quoteid." LIMIT 1 ";
$res = $db->query($sql);
$row = $db->fetch_row();

$output = $row['subject'];

return $output;
}

Remember : USE limit 1 when you search with primary key and you know that only 1 record will be searched. it reduce your processing time.

$sql = "SELECT subject FROM emg_quote WHERE ID='".$quoteid."'";

You had not wrote your db fieldname in where condition