简体   繁体   中英

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"

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)

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.

You're quoting the column name, which makes MySQL think it's a string.

$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?

Where is this coming from? $_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']

Otherwise if it is $_POST["org_id"], wouldn't it be single quotes not double? $_POST['org_id']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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