简体   繁体   English

从mysql查询php创建数组

[英]create array from mysql query php

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. 结果应该是数组中的19,19,18,17,16。 Thats all the types that has me as set as owner. 这就是我设置为所有者的所有类型。

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. 我虽然可以使用array_push,但大多数都有更好的方法。 I thought I would get all the type values with a simple mysql query. 我以为我会通过简单的mysql查询获得所有类型值。

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 next element 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. 这就是while循环工作的原因。 If there aren't any rows anymore $row will be false and the while loop exists. 如果没有任何行, $row将为false并且while循环存在。

It only seems that mysql_fetch_array gets more than one row, because by default it gets the result as normal and as 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']访问它。

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

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'];
}

You may want to go look at the SQL Injection article on Wikipedia. 您可能想查看Wikipedia上的SQL注入文章。 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. 查看“十六进制转换”部分,找到一个小函数来执行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. 我编写了dosql()函数,因为我厌倦了让我的SQL命令在所有地方执行,忘记检查错误,并能够将我的所有命令记录到日志文件中以供以后查看(如果需要)。 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. 我实际上已经扩展了这个功能,因为我希望它能做得更多但是这个基本功能是从SQL调用中获取输出的一个很好的起点。

$type_array = array();    
while($row = mysql_fetch_assoc($result)) {
    $type_array[] = $row['type'];
}
while($row = mysql_fetch_assoc($result)) {
  echo $row['type'];
}

You could also make life easier using a wrapper, eg 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. 一个好的包装器也会为你做所有的逃避,让事情更容易阅读。

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

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