php MYSQL - 从获取的数组中选择特定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7821203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:26:10  来源:igfitidea点击:

MYSQL - Select specific value from a fetched array

phpmysqlarrays

提问by r0skar

I have a small problem and since I am very new to all this stuff, I was not successful on googling it, because I dont know the exact definitions for what I am looking for.

我有一个小问题,因为我对所有这些东西都很陌生,所以我在谷歌上搜索它没有成功,因为我不知道我正在寻找的东西的确切定义。

I have got a very simple database and I am getting all rows by this:

我有一个非常简单的数据库,我通过这个获取所有行:

while($row = mysql_fetch_array($result)){
    echo $row['id']. " - ". $row['name'];
    echo "<br />";
}

Now, my question is: how do I filter the 2nd result? I thought something like this could work, but it doesnt:

现在,我的问题是:如何过滤第二个结果?我认为这样的事情可以工作,但它没有:

$name2= $row['name'][2];

Is it even possible? Or do I have to write another mysql query (something like SELECT .. WHERE id = "2") to get the name value in the second row?

甚至有可能吗?或者我是否必须编写另一个 mysql 查询(类似于SELECT .. WHERE id = "2")来获取第二行中的名称值?

What I am trying to is following:

我正在尝试的是以下内容:

-get all data from the database (with the "while loop"), but than individually display certain results on my page. For instance echo("name in second row")and echo("id of first row")and so on.

- 从数据库中获取所有数据(使用“while 循环”),但不是在我的页面上单独显示某些结果。例如echo("name in second row")echo("id of first row")等。

回答by JJJ

If you would rather work with a full set of results instead of looping through them only once, you can put the whole result set to an array:

如果您更愿意使用完整的结果集而不是只循环一次,您可以将整个结果集放入一个数组:

$row = array();

while( $row[] = mysql_fetch_array( $result ) );

Now you can access individual records using the first index, for example the name field of the second row is in $row[ 2 ][ 'name' ].

现在您可以使用第一个索引访问单个记录,例如第二行的 name 字段是 in $row[ 2 ][ 'name' ]

回答by javi_moralesf

$result = mysql_query("SELECT * FROM ... WHERE 1=1");
while($row = mysql_fetch_array($result)){
/*This will loop arround all the Table*/
    if($row['id'] == 2){
    /*You can filtere here*/
    }

    echo $row['id']. " - ". $row['name'];
    echo "<br />";
}

回答by Mick

If you want to be able to use 2 consecutive results in one loop, you can store the results of the first loop, and then loop through.

如果希望能够在一个循环中使用 2 个连续的结果,可以存储第一个循环的结果,然后循环执行。

$initial = true;
$storedId = '';

while($row = mysql_fetch_array($result)) {

  $storedId = $row['id'];

  if($initial) {

    $initial = false;
    continue;
  }

  echo $storedId . $row['name'];
}

This only works for consecutive things though.Please excuse the syntax errors, i haven't programmed in PHP for a very long time...

这仅适用于连续的事情。请原谅语法错误,我很久没有用 PHP 编程了......

回答by Raz

If you always want the second row, no matter how many rows you have in the database you should modify your query thus:

如果你总是想要第二行,无论你在数据库中有多少行,你都应该这样修改你的查询:

SELECT * FROM theTable LIMIT 1, 1;

See: http://dev.mysql.com/doc/refman/5.5/en/select.html

见:http: //dev.mysql.com/doc/refman/5.5/en/select.html

回答by IT-Dan

I used the code from the answer and slightly modified it. Thought I would share.

我使用了答案中的代码并稍微修改了它。以为我会分享。

$result = mysql_query( "SELECT name FROM category;", db_connect() );
$myrow = array();
while ($myrow[]  = mysql_fetch_array( $result, MYSQLI_ASSOC )) {}
$num = mysql_num_rows($result);

Example usage

示例用法

echo "You're viewing " . $myrow[$view_cat]['name'] . "from a total of " . $num;

回答by Riz

$counter = 0;
while($row = mysql_fetch_array($result)){
    $counter++;

    if($counter == 2){
        echo $row['id']. " - ". $row['name'];
        echo "<br />";
    }
}

回答by Frederik.L

Yes, ideally you have to write another sql query to filter your results. If you had :

是的,理想情况下,您必须编写另一个 sql 查询来过滤结果。如果你有 :

SELECT * FROM Employes

then you can filter it with :

然后你可以过滤它:

SELECT * FROM Employes WHERE Name="Paul";

if you want every names that start with a P, you can achieve this with :

如果你想要所有以 P 开头的名字,你可以通过以下方式实现:

SELECT * FROM Employes WHERE Name LIKE "P%";

The main reason to use a sql query to filter your data is that the database manager systems like MySQL/MSSQL/Oracle/etc are highly optimized and they're way faster than a server-side condition block in PHP.

使用 sql 查询来过滤数据的主要原因是 MySQL/MSSQL/Oracle/etc 等数据库管理器系统经过高度优化,它们比 PHP 中的服务器端条件块要快得多。

回答by vikas

This While loop will automatically fetch all the records from the database.If you want to get any other field then you will only need to use for this.

这个 While 循环将自动从数据库中获取所有记录。如果您想获取任何其他字段,那么您只需要为此使用。

回答by Nikoloff

Depends on what you want to do. mysql_fetch_array() fetches the current row to which the resource pointer is pointing right now. This means that you don't have $row['name'][2];at all. On each iteration of the while loop you have all the columns from your query in the $row array, you don't get all rows from the query in the array at once. If you need just this one row, then yes - add a WHEREclause to the query, don't retrieve the other rows if you don't need them. If you need all rows, but you wanna do something special when you get the second row, then you have to add a counter that checks which row you are currently working with. I.e.:

取决于你想做什么。mysql_fetch_array() 获取资源指针现在指向的当前行。这意味着你根本没有$row['name'][2];。在 while 循环的每次迭代中,您的查询中的所有列都位于 $row 数组中,您不会一次从数组中的查询中获取所有行。如果您只需要这一行,那么是的 -WHERE在查询中添加一个子句,如果您不需要它们,请不要检索其他行。如果您需要所有行,但是您想在获得第二行时做一些特别的事情,那么您必须添加一个计数器来检查您当前正在使用的行。IE:

$count = 0;
while($row = mysql_fetch_array($result)){
    if(++$count == 2)
    {
        //do stuff
    }
}