2015-02-09 52 views
1

Okey,讓我把這個直接。 以下是我的表結構:在數組中添加多個 - PHP

表名: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 Array([0] => Array([password] => qwerty [0] => qwerty))

Random no。數組([0] =>數組([password] => trustno1 [0] => trustno1))

Array([0] => Array([0] => Array([password] = > QWERTY [0] => QWERTY))[1] =>數組([0] =>數組([口令] => trustno1 [0] => trustno1)))

我如何把這個權利?任何幫助請...

回答

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); 
?> 

這將工作!

+0

非常感謝LOTTT !!!!! :) – Jess 2015-02-10 10:24:35