如何获取userid = 8的行数。 我有一个像ID和userid这样的cloumns的表。 所以我试图在php数组中输入userid = 8的行数。
exmple:
我尝试使用这个答案:Get all mysql selected rows into an array但它没有用。
这就是我得到userid = 8的行,但是如何将它们放入数组?
$midSelect[i]="SELECT * FROM times WHERE userid=8;
$midResult[i]=mysqli_query($con,$midSelect[i]);
while($row = mysqli_fetch_array($midResult[i])) {
$mid[i]=$row['id'];
}
答案 0 :(得分:1)
我认为你距离它只有一步之遥
$sql ="SELECT id FROM times WHERE userid=8";
$query =mysqli_query($con,$sql);
$mid = array();
while($row = mysqli_fetch_array($query)) {
$mid[] = $row['id'];
}
print_r($mid);
您所需的值现在位于数组$mid
根据操作请求,我添加了一些代码
现在,如果你需要echo
值,你可以在数组中循环并在迭代时打印
foreach($mid as $val)
{
echo $val . '<br />';
}
//print
// 1
// 4
// 5
// 8
答案 1 :(得分:0)
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "my_database");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "SELECT `id` FROM `times` WHERE `userid` = '8'";
$result = $mysqli->query($query);
$loopcounter = 0;
/* associative array */
while($row = $result->fetch_array(MYSQLI_ASSOC))
{
$loopcounter++;
$counter[$loopcounter] = $row['id'];
}
/* print counter */
print_r($counter);
/* free result set */
$result->free();
/* close connection */
$mysqli->close();
?>