简体   繁体   English

PHP:使用变量时不会执行 Where 子句

[英]PHP: Where clause will not execute when using a variable

For the user I am testing with, their org_id column value is "student_life"对于我正在测试的用户,他们的 org_id 列值为“student_life”

I am trying to have this function display whatever rows have the student_life column = 1. (so yes there is a column student_life which is a boolean, and then I also have a separate column named org_id and in this case has the value student_life)我试图让这个函数显示任何具有 student_life 列 = 1 的行。(所以是的,有一列 student_life 是一个布尔值,然后我还有一个名为 org_id 的单独列,在这种情况下具有值 student_life)

I am pretty sure there is a syntax error but I cannot figure it out.我很确定存在语法错误,但我无法弄清楚。

function org_id_users_table() 
{
$org_id = mysql_real_escape_string($_POST["org_id"]);

$sql = $this->query("SELECT * FROM ".DBTBLE." WHERE '$org_id' = '1'");
$result = $sql['sql'];
$num_rows = $sql['num_rows'];
$this->create_table($result, $num_rows);
}

(when I replace $org_id in the "$sql=..." line with student_life the code works. (当我用 student_life 替换“$sql=...”行中的 $org_id 时,代码有效。

You're quoting the column name, which makes MySQL think it's a string.您引用了列名,这使 MySQL 认为它是一个字符串。

$sql = $this->query("SELECT * FROM ".DBTBLE." WHERE $org_id = '1'");

Edit:编辑:

Based on your comments, I think what you actually want is this:根据您的评论,我认为您真正想要的是:

$sql = $this->query("SELECT * FROM ".DBTBLE." WHERE org_id = '$org_id'");

Change quotes.更改报价。

$sql = $this->query("SELECT * FROM ".DBTBLE." WHERE `$org_id` = '1'");

PS Why shouldn't I use mysql_* functions in PHP? PS 为什么我不应该在 PHP 中使用 mysql_* 函数?

Where is this coming from?这是从哪里来的? $_POST["org_id"] $_POST["org_id"]

Do you have a form on the page posting that?你在页面上有张贴的表格吗? Or are you just trying to get that from the database?或者你只是想从数据库中获取它? If so, wouldn't you need another query to obtain that first?如果是这样,您是否不需要另一个查询来首先获得它?

$row_MyFirstQuery['org_id'] $row_MyFirstQuery['org_id']

Otherwise if it is $_POST["org_id"], wouldn't it be single quotes not double?否则如果是$_POST["org_id"],岂不是单引号不是双引号? $_POST['org_id'] $_POST['org_id']

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

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