多个附加在数组中 - PHP

时间:2015-02-09 18:27:52

标签: php mysql arrays

哎呀,让我直截了当地说。 以下是我的表结构:

  

表名:user_password; sno int(11)AUTO_INCREMENT,密码   VARCHAR(20)

test.php的

<?php
include "config.php"; // database connection details stored here
$mod3 = rand(0,20);
$max_passno=$dbo->prepare("select count(*) from user_password"); //find the max. no of entries in user_password table
$max_passno->execute();
$count = $max_passno->fetchColumn();
echo "</br>total count : " . $count . "</br>";
$temp_array = array(); //initialise an empty array
for($j=1; $j<=$mod3; $j++){
    $no2 = rand(1, $count + 1); //select a random number
    echo "</br>Random no. : " . $no2 . "</br>";
    $my_task = "SELECT password FROM user_password WHERE sno=$no2";
    print_r($dbo->query($my_task)->fetchAll());
    echo "</br>";
    array_push($temp_array, $dbo->query($my_task)->fetchAll()); //append the random password to $temp_array
}
print_r ($temp_array);
?>

预期输出

包含循环期间选择的随机密码的数组。

实际输出:

  

总数:29

     

随机没有。 :3数组([0] =&gt;数组([密码] =&gt; qwerty [0] =&gt;   qwerty))

     

随机没有。 :26 Array([0] =&gt; Array([password] =&gt; trustno1 [0] =&gt;   trustno1))

     

数组([0] =&gt;数组([0] =&gt;数组([密码] =&gt;   qwerty [0] =&gt; qwerty))[1] =&gt;数组([0] =&gt;数组([密码] =&gt;   trustno1 [0] =&gt; trustno1)))

我该怎么做?任何帮助请...

1 个答案:

答案 0 :(得分:0)

<?php
include "config.php"; // database connection details stored here
$mod3 = rand(0,20);
$max_passno = $conn->query("SELECT * FROM user_password");
$count = $max_passno->num_rows;
echo "</br>total count : " . $count . "</br>";
$temp_array = array(); //initialise an empty array
for($j=1; $j<=$mod3; $j++){
    $no2 = rand(1, $count + 1); //select a random number
    echo "</br>Random no. : " . $no2 . "</br>";
    $my_task = "SELECT password FROM user_password WHERE sno=$no2";
    $result_new = $conn->query($my_task);
    $row = $result_new->fetch_assoc();
    $element = $row['password'];
    echo $element;
    array_push($temp_array, $element);
    echo "</br>";
}
echo "</br>";
print_r($temp_array);
?>

这将有效!