如何通过一个查询从 mysql 中选择多行并在 php 中使用它们

发布于 2024-11-17 13:34:14 字数 281 浏览 4 评论 0原文

我目前有一个如下图所示的数据库。

在此处输入图像描述

其中有一个查询选择 number1 等于 1 的行。在

mysql_fetch_assoc()

php 中使用时,我只给出第一个有什么办法得到第二个吗?就像通过维度数组

array['number2'][2] 

或类似的东西

I currently have a database like the picture below.

enter image description here

Where there is a query that selects the rows with number1 equaling 1. When using

mysql_fetch_assoc()

in php I am only given the first is there any way to get the second? Like through a dimesional array like

array['number2'][2] 

or something similar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你是暖光i 2024-11-24 13:34:14

使用重复调用 mysql_fetch_assoc。它记录在 PHP 手册中。

http://php.net/manual/function.mysql-fetch-assoc.php

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

如果需要,您可以使用它来构建一个多维数组,以便在脚本的其他部分中使用。

Use repeated calls to mysql_fetch_assoc. It's documented right in the PHP manual.

http://php.net/manual/function.mysql-fetch-assoc.php

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

If you need to, you can use this to build up a multidimensional array for consumption in other parts of your script.

偏闹i 2024-11-24 13:34:14
$Query="select SubCode,SubLongName from subjects where sem=1";
$Subject=mysqli_query($con,$Query);
$i=-1;

while($row = mysqli_fetch_array($Subject))
{
    $i++;

    $SubjectCode[$i]['SubCode']=$row['SubCode'];
    $SubjectCode[$i]['SubLongName']=$row['SubLongName'];

}

这里 while 循环将获取每一行。该行的所有列将存储在 $row 变量(数组)中,但是当下一次迭代发生时,它将丢失。所以我们复制将 $row 转换为名为 $SubjectCode 的多维数组。每行的内容将存储在该数组的第一个索引中。这可以稍后在我们的脚本中重用。
(我是 PHP 新手,所以如果有人遇到过这个并且知道更好的方法,请在评论中提及我的名字,以便我可以学习新知识。)

$Query="select SubCode,SubLongName from subjects where sem=1";
$Subject=mysqli_query($con,$Query);
$i=-1;

while($row = mysqli_fetch_array($Subject))
{
    $i++;

    $SubjectCode[$i]['SubCode']=$row['SubCode'];
    $SubjectCode[$i]['SubLongName']=$row['SubLongName'];

}

Here the while loop will fetch each row.All the columns of the row will be stored in $row variable(array),but when the next iteration happens it will be lost.So we copy the contents of array $row into a multidimensional array called $SubjectCode.contents of each row will be stored in first index of that array.This can be later reused in our script.
(I 'am new to PHP,so if anybody came across this who knows a better way please mention it along with a comment with my name so that I can learn new.)

谁对谁错谁最难过 2024-11-24 13:34:14

看起来还没有建议完整的解决方案

$Query="select SubCode,SubLongName from subjects where sem=1";
$Subject=mysqli_query($con,$Query);
$Rows = array ();
while($row = mysqli_fetch_array($Subject))
{
    $Rows [] = $row;
}

$Rows [] = $row 将行追加到数组中。结果是所有行的多维数组。

It looks like the complete solution has not been suggested yet

$Query="select SubCode,SubLongName from subjects where sem=1";
$Subject=mysqli_query($con,$Query);
$Rows = array ();
while($row = mysqli_fetch_array($Subject))
{
    $Rows [] = $row;
}

The $Rows [] = $row appends the row to the array. The result is a multidimensional array of all rows.

煮酒 2024-11-24 13:34:14

这是另一种简单的方法

$sql_shakil ="SELECT app_id, doctor_id FROM patients WHERE doctor_id = 201 ORDER BY ABS(app_id) ASC";
if ($result = $con->query($sql_shakil)) {

while ($row = $result->fetch_assoc()) {
    printf ("%s (%s)\n", $row["app_id"], $row["doctor_id"]);
}

演示链接

This is another easy way

$sql_shakil ="SELECT app_id, doctor_id FROM patients WHERE doctor_id = 201 ORDER BY ABS(app_id) ASC";
if ($result = $con->query($sql_shakil)) {

while ($row = $result->fetch_assoc()) {
    printf ("%s (%s)\n", $row["app_id"], $row["doctor_id"]);
}

Demo Link

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文