简体   繁体   English

在PHP中从Table获取主题

[英]Get subject from Table in 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? 确定,所以我试图访问一个名为emg_quote的表,我具有报价ID,所以我试图从与此ID相同的行中获取“列主题”,但由于某种原因,我得到的只是整个表的第一行吗? 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;
}

Are you using a custom object to wrap the native API's? 您是否正在使用自定义对象包装本地API?

Either way it doesn't look right to me. 无论哪种方式对我来说都不对。 You don't seem to be using the result of the query. 您似乎没有使用查询结果。

ie

$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. A.您躺在$quoteid以提供正确的where语法。 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. 即: ID=123这是一种非常不安全的方法,因为用户可以将其更改为Some-Important-Details='bla'以便从此表或其他表中提取更多详细信息。

B. You should ALWAYS escape characters when receiving data from user, otherwise you easily subjected to SQL-Injections. B.从用户接收数据时,应始终转义字符,否则您很容易受到SQL注入的影响。 And believe me you don't want it. 相信我,你不想要它。

you have to use the checking after where. 您必须在哪里使用检查。 use you column name before your $quoteid variable 在$ quoteid变量之前使用您的列名

$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. 切记:使用主键搜索时,请使用USE限制1,并且您知道将仅搜索1条记录。 it reduce your processing time. 它减少了您的处理时间。

You might be missing the where column. 您可能缺少where列。

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

We also do not see weather something with your Db class is wrong. 我们也看不到您的Db类出现天气问题是错误的。

You should in any case not directly put request variables into a database query. 无论如何,您都不应将请求变量直接放入数据库查询中。

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

您没有在where条件中写入数据库字段名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM