简体   繁体   English

将mysql结果推入php数组并打印该数组

[英]Pushing mysql results into php array and printing this array

So I am trying to make a code that gets an input and then starts comparing all values of this input with every other value of the other values in the database. 因此,我试图编写一个获取输入的代码,然后开始将该输入的所有值与数据库中其他值的所有其他值进行比较。 To do this i want to put every row in an array an then compare this array to the value of the input. 为此,我想将每一行放入一个数组中,然后将该数组与输入值进行比较。 Now my problem is that i cannot get an entire row into an array the code that has the problem is. 现在我的问题是我无法将整个行放入具有问题的代码的数组中。

 $row = array();
 for ($i = 1; $i <= 10; $i++) {
$autorange2="SELECT AutoRange  FROM $tbl_name WHERE Nr='$i'";
$auto2=mysql_query($autorange, 0);
array_push($row, "$auto2");
}
print_r($row);

and my result is 我的结果是

Array ( [0] => Resource id #6 [1] => Resource id #7 [2] => Resource id #8 [3] =>  
Resource id #9 [4] => Resource id #10 [5] => Resource id #11 [6] 
=> Resource id #12 [7] => Resource id #13 [8] => Resource id #14 [9] => 
Resource id #15 )

(the print is for testing purposes) (打印用于测试目的)

Use mysql_fetch_array . 使用mysql_fetch_array Try this- 尝试这个-

for ($i = 1; $i <= 10; $i++) {
   $autorange2="SELECT AutoRange  FROM $tbl_name WHERE Nr='$i'";
   $auto2=mysql_query($autorange, 0);
   while($row1 = mysql_fetch_array($auto2))
   {
       array_push($row, $row1);
   }
}

And if you are sure that there will only be 1 row in the query result- 而且,如果您确定查询结果中只有1行,

for ($i = 1; $i <= 10; $i++) {
   $autorange2="SELECT AutoRange  FROM $tbl_name WHERE Nr='$i'";
   $auto2=mysql_query($autorange, 0);
   array_push($row, mysql_fetch_assoc($auto2));
}

You tried to outut $row — result set. 您试图输出$row —结果集。 You can't output result set, you have to fetch it firstly. 您无法输出结果集,必须先获取它。

Use mysql_fetch_assoc function for it. 使用mysql_fetch_assoc函数。

$row = array();
for ($i = 1; $i <= 10; $i++) {
 $autorange2="SELECT AutoRange  FROM $tbl_name WHERE Nr='$i'";
 $auto2=mysql_query($autorange); // <-- I removed 2nd parameter, you don't need it there
 while($data = mysql_fetch_assoc($auto2)) {
  $row[] = $data; // same as array_push($row, $data);
  // $data['AutoRange']; <-- get access to data on this row
 }
 print_r($row);
}

If you want to directly access to some entry in set, use $data['fieldName'] where fieldName is name of field you want to acess. 如果要直接访问set中的某个条目,请使用$data['fieldName'] ,其中fieldName是要访问的字段名称。

mysql_fetch_assoc return associated array. mysql_fetch_assoc返回关联的数组。

Note : if you will use method from this answer you have to know, that mysql_fetch_array returns indexed array, so you have to get access to data with $data[0] , $data[1] etc. I think better to use mysql_fetch_assoc function and work with associative array. 注意 :如果您将使用答案中的方法,则必须知道mysql_fetch_array返回索引数组,因此必须使用$data[0]$data[1]等访问数据。我认为最好使用mysql_fetch_assoc函数并使用关联数组。

Another note : what for you use such approach to work with database? 另一个注意事项 :使用这种方法对数据库有什么作用? You don't need for loop to get data from table. 你不需要for循环从表中获取数据。 Data, returned in resultset, depending on your query. 在结果集中返回的数据,具体取决于您的查询。

This query will return all rows from table $tbl_name : 该查询将返回表$tbl_name所有行:

SELECT AutoRange  FROM $tbl_name

So your code will be: 因此,您的代码将是:

$rows = array();
$autorange2="SELECT AutoRange  FROM $tbl_name WHERE Nr='$i'";
$auto2=mysql_query($autorange); // <-- I removed 2nd parameter, you don't need it there
while($data = mysql_fetch_assoc($auto2)) {
 $rows[] = $data; // same as array_push($row, $data);
 // $data['AutoRange']; <-- get access to data on this row
}
print_r($rows);

Now you have values of autorange field of each row from $tbl_name table. 现在,您有了$tbl_name表中每一行的autorange字段的值。

How you get access to this data: 您如何访问此数据:

foreach($row in $rows) {
 $autoRange = $row['AutoRange'];
 var_dump($autoRange);
}

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

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