简体   繁体   English

如何将SELECT查询的结果分配给变量?

[英]How to assign the result of a SELECT query to a variable?

Can't extract the value of the $row['PID'] from the query... 无法从查询中提取$ row ['PID']的值...

$result = mysqli_query($con,"SELECT * FROM posts WHERE PID=(SELECT max(PID) FROM posts)");

$row = mysql_fetch_array($result);

$id = $row['PID'];

I know how to print it out: 我知道如何打印出来:

while($row = mysqli_fetch_array($result))
      {
      echo $row['PID'];
      }

But no idea how to assign it to a variable without having to print it out... 但是不知道如何在不必打印出来的情况下将其分配给变量...

In the second line, you are using mysql_fetch_array instead of mysqli_fetch_array . 在第二行中,您正在使用mysql_fetch_array而不是mysqli_fetch_array I'm not sure if that has anything to do with the problem (not used to sqli). 我不确定这是否与问题有关(不习惯于sqli)。

Can you do a print_r of the $row variable? 您可以执行$row变量的print_r吗?

Try changing 尝试改变

$row = mysql_fetch_array($result);

to

$row = mysqli_fetch_array($result);

Change this line: 更改此行:

$row = mysql_fetch_array($result);

to this: 对此:

$row = mysqli_fetch_row($result);

Fetching an array will grab a multi dimensional array, so you'd have to use $row[0]['pid'] I believe. 提取数组将获取多维数组,因此我相信您必须使用$ row [0] ['pid']。

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

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