php 从mysql查询php创建数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3415296/
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
create array from mysql query php
提问by ganjan
I have a little problem that I don't understand. I have a db that has an owner and type (and more off course). I want to get a list of all the type values that has owner equal to the current user, but I get only two result
我有一个小问题,我不明白。我有一个拥有所有者和类型的数据库(当然还有更多)。我想获取所有者等于当前用户的所有类型值的列表,但我只得到两个结果
$sql = "SELECT type FROM cars WHERE owner='".mysql_real_escape_string($_SESSION['username'])."' AND selling='0' ORDER BY id DESC ";
$result = mysql_query($sql,$con);
print_r(mysql_fetch_array($result));
prints out:
打印出来:
Array ( [0] => 18 [type] => 18 )
and
和
$sql = "SELECT type FROM cars WHERE owner='".mysql_real_escape_string($_SESSION['username'])."' AND selling='0' ";
prints out:
打印出来:
Array ( [0] => 16 [type] => 16 )
And the result should be something like 19, 19, 18, 17, 16 in an array. Thats all the types that has me as set as owner.
结果应该类似于数组中的 19, 19, 18, 17, 16 。这就是将我设置为所有者的所有类型。
I have got this working now:
我现在已经开始工作了:
for ($x = 0; $x < mysql_num_rows($result); $x++){
$row = mysql_fetch_assoc($result);
echo $row['type'];
}
Here I print out all the values correctly, but I need to create an array with all the values. I though I could use array_push, but there most be a better way of doing it. I thought I would get all the type values with a simple mysql query.
在这里,我正确打印了所有值,但我需要创建一个包含所有值的数组。我虽然可以使用array_push,但大多数都有更好的方法。我以为我会通过一个简单的 mysql 查询获得所有类型值。
回答by Felix Kling
Very often this is done in a while
loop:
通常这是在while
循环中完成的:
$types = array();
while(($row = mysql_fetch_assoc($result))) {
$types[] = $row['type'];
}
Have a look at the examples in the documentation.
查看文档中的示例。
The mysql_fetch_*
methods will always get the nextelement of the result set:
这些mysql_fetch_*
方法将始终获取结果集的下一个元素:
Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows.
返回与获取的行相对应的字符串数组,如果没有更多行,则返回 FALSE。
That is why the while
loops works. If there aren't any rows anymore $row
will be false
and the while
loop exists.
这就是while
循环起作用的原因。如果没有任何行了$row
会false
和while
循环存在。
It only seems that mysql_fetch_array
gets more than one row, because by default it gets the result as normal andas associative value:
似乎只mysql_fetch_array
得到不止一行,因为默认情况下它以正常和关联值的形式获取结果:
By using MYSQL_BOTH (default), you'll get an array with both associative and number indices.
通过使用 MYSQL_BOTH(默认),您将获得一个具有关联索引和数字索引的数组。
Your example shows it best, you get the same value 18
and you can access it via $v[0]
or $v['type']
.
您的示例最好地说明了这一点,您获得了相同的值,18
并且可以通过$v[0]
或访问它$v['type']
。
回答by Master Coder
THE CORRECT WAY ************************ THE CORRECT WAY
while($rows[] = mysqli_fetch_assoc($result));
array_pop($rows); // pop the last row off, which is an empty row
回答by Fosco
You do need to iterate through...
您确实需要遍历...
$typeArray = array();
$query = "select * from whatever";
$result = mysql_query($query);
if ($result) {
while ($record = mysql_fetch_array($results)) $typeArray[] = $record['type'];
}
回答by Treffynnon
$type_array = array();
while($row = mysql_fetch_assoc($result)) {
$type_array[] = $row['type'];
}
回答by Codler
while($row = mysql_fetch_assoc($result)) {
echo $row['type'];
}
回答by Mark Manning
You may want to go look at the SQL Injection article on Wikipedia. Look under the "Hexadecimal Conversion" part to find a small function to do your SQL commands and return an array with the information in it.
您可能需要查看 Wikipedia 上的 SQL 注入文章。在“十六进制转换”部分下查找一个小函数来执行您的 SQL 命令并返回一个包含信息的数组。
https://en.wikipedia.org/wiki/SQL_injection
https://en.wikipedia.org/wiki/SQL_injection
I wrote the dosql() function because I got tired of having my SQL commands executing all over the place, forgetting to check for errors, and being able to log all of my commands to a log file for later viewing if need be. The routine is free for whoever wants to use it for whatever purpose. I actually have expanded on the function a bit because I wanted it to do more but this basic function is a good starting point for getting the output back from an SQL call.
我编写了 dosql() 函数,因为我厌倦了到处执行 SQL 命令、忘记检查错误以及能够将我的所有命令记录到日志文件中以便以后在需要时查看。该例程对于任何想要出于任何目的使用它的人都是免费的。我实际上对这个函数做了一些扩展,因为我希望它做更多的事情,但是这个基本函数是从 SQL 调用中获取输出的一个很好的起点。
回答by Paul Dixon
You could also make life easier using a wrapper, e.g. with ADODb:
您还可以使用包装器让生活更轻松,例如使用 ADODb:
$myarray=$db->GetCol("SELECT type FROM cars ".
"WHERE owner=? and selling=0",
array($_SESSION['username']));
A good wrapper will do all your escaping for you too, making things easier to read.
一个好的包装器也会为你完成所有的转义,使事情更容易阅读。