[英]having problems to execute a PHP code with simple mySQL query
I am trying to execute a query with a WHERE clause but it looks like the id I retrieve needs to be perhaps converted from an array into something else. 我正在尝试使用WHERE子句执行查询,但看来我检索到的ID可能需要从数组转换为其他内容。 I am new to PHP so I am struggling a little:
我是PHP的新手,所以我有点挣扎:
...some previous query here
$sharepoint_id = $data[0];
//returns Array([ID] => a5f415a7-3d4f-11e5-b52f-b82a72d52c35)
qry = mysql_query("SELECT HostName FROM MSSWireList WHERE id=".$sharepoint_id);
$data = array();
while($rows = mysql_fetch_array($qry))
{
$data[] = array(
"ID" => $rows['ID'],
"Record" => $rows['Record'],
"HostName" => $rows['HostName']
);
}
return json_encode($data);
also tried $sharepoint_id = $data[0]->ID; 还尝试了$ sharepoint_id = $ data [0]-> ID; Thank you
谢谢
"returns Array([ID] => a5f415a7-3d4f-11e5-b52f-b82a72d52c35)"
“返回数组([ID] => a5f415a7-3d4f-11e5-b52f-b82a72d52c35)”
That's a string and not an integer. 那是一个字符串,而不是整数。 The variable in your
WHERE
clause needs to be quoted. 您的
WHERE
子句中的变量需要加引号。
WHERE id='".$sharepoint_id."' ");
Checking for errors would have signaled the syntax error. 检查错误将表明语法错误。
Add or die(mysql_error())
to mysql_query()
. 添加
or die(mysql_error())
到mysql_query()
。
Your present code is open to SQL injection . 您当前的代码可以进行SQL注入 。 Use
mysqli_*
with prepared statements , or PDO with prepared statements . 将
mysqli_*
与预处理语句一起使用 ,或将PDO与预处理语句一起使用 。
Edit: 编辑:
You only selected the HostName
column from your query and not the other two, ID
and Record
. 您仅从查询中选择了
HostName
列,而没有选择其他两个ID
和Record
。
However, when going over a loop, row names are case-sensitive. 但是,在循环中,行名区分大小写。
So, if your row's case is id
as opposed to ID
, then that will matter in your loop. 因此,如果您的行的大小写是
id
而不是ID
,那么这在循环中就很重要。
$rows['ID']
and $rows['id']
are two different animals. $rows['ID']
和$rows['id']
是两种不同的动物。 Sidenote: 边注:
Pulled from a comment I asked already: 从我已经问过的评论中拉出来:
qry = mysql_query
if that your real code, it's missing a $
for qry
. qry = mysql_query
如果您的真实代码,则缺少qry
的$
。
And if so, error reporting would have thrown you an undefined constant qry notice. 如果是这样,错误报告将向您抛出未定义的恒定qry通知。
I won't take you to task for using the old mysql driver instead of mysqli or PDO, or for not using a prepared statement - I'll leave that for others to do - but 我不会带您去使用旧的mysql驱动程序来代替mysqli或PDO,或者不使用准备好的语句-我将把它留给其他人去做-但是
"...WHERE id = '" . $sharepoint_id['ID'] . "'"
should do the job. 应该做的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.