有没有办法在一个一维php数组函数中使用从mysql数据库中获取的所有行的数据

fdbelqdn  于 2021-06-21  发布在  Mysql
关注(0)|答案(4)|浏览(232)

这是一种传统的方法,我可以用mysqli\u fetch\u array()函数获取所有带有数据的行并运行while循环来逐个获取它们。

$con = mysqli_connect(YOURLS_DB_HOST,YOURLS_DB_USER,YOURLS_DB_PASS);
     mysqli_select_db($con,YOURLS_DB_NAME);
                              $users=mysqli_query($con,"SELECT * FROM users"));
                                    while($user=mysqli_fetch_array($users))     
                                         {  
                                             $user['email']; $user['pass'];
                                          }

现在的问题是,我必须将包含相应数据的所有行插入到下面的数组中。这怎么可能。行将一个接一个地插入,可能就像单行注解的行一样。和非评论的是我想要的实际数据。

$user_password = array(

    'username2' => 'password2',  
    /* You can have one or more*/ 'login'=>'password',

              // $user['email']  => $user['pass'],                  

    )

我做了一些事情,目前的状态是

$con = mysqli_connect(YOURLS_DB_HOST,YOURLS_DB_USER,YOURLS_DB_PASS);
     mysqli_select_db($con,YOURLS_DB_NAME);
    $users=mysqli_fetch_assoc(mysqli_query($con,"SELECT email, pass FROM users"));
    while ($row = mysqli_fetch_assoc($users)) {
    $email = $row['email'];
    $pass = $row['pass'];
    $user_pass3[] = array(
        $email => $pass,
        'hguhfg'=> 'nhgudfhg',
    );

}
$yourls_user_passwords = $user_pass3;
//print_r($user_pass4);
/*output Array ( [0] => Array ( [test@example.com] => test [hguhfg] => nhgudfhg ) [1] => Array ( [demo@example.com] => demo [hguhfg] => nhgudfhg ) ) but disired output is Array ( [test@example.com] => test [hguhfg] => nhgudfhg [demo@example.com] => demo); */
jvlzgdj9

jvlzgdj91#

听了你的建议,我已经完成了计划。谢谢大家。给我们完美的密码。

$users = mysqli_query($con, "SELECT email, pass FROM users");
while ($row = mysqli_fetch_assoc($users)) {
    $email = $row['email'];
    $pass = $row['pass'];
    $user_pass3[] = array(
        $email => $pass,
    );

}
foreach($user_pass3 as $arr) {
            foreach($arr as $key => $data) {
                    $user_pass[$key]  =   $data;
                }
        }

   // print_r($user_pass);
$user_pass_final = $user_pass;
hpxqektj

hpxqektj2#

<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "test";
$conn = mysqli_connect($servername, $username, $password, $database); if (!($conn)) 
{
die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT email,pass FROM users";
$result = mysqli_query($conn, $sql);

$newArray = array();

if (mysqli_num_rows($result) > 0) 
{
    /* loop for return all the fetched data one by one */
while($row = mysqli_fetch_assoc($result)) 
{
    /* store the data from mysqli array in variable use same code for for as much as type of data you want*/
$email=$row["email"]; 
$password=$row["pass"];
    /* you can also add some value manually into the same array like this */
$newArray['admin']= 'adminpass';
$newArray['test']= 'testpass';
    /* You have to build one array variable which includes 2 type of data 1st one as array index and 2nd one as value */
$newArray[$email]= $password;
}
} 
else 
{
echo "0 results";
}
$conn->close();
    /* print the array or store in another variable to use elsewhere. */
print_r($newArray);
/* or print indiviual data one by one like data1, data2 */
echo "<br>";
foreach($newArray as $x => $x_value)
{
echo "Email=" . $x . ", Password=" . $x_value;
echo "<br>";
}

/* this example shows email and password in plain text just for the special requirement of the project. It's recomended to not to use passwords in plain text and inside the code. */

    //Credits gose to https://www.facebook.com/ansarali.molla.9
?>
lzfw57am

lzfw57am3#

如果您只需要登录名和密码:

$users = mysqli_query($con,"SELECT login, password FROM sughi_users"));

$user = array();

foreach ($users as $aUser) {
    $user[$aUser['login']] = $aUser['password'];
}
neekobn8

neekobn84#

这个 [] 变量上的符号使其成为动态数组。所以不要使用 array() 之后。如果愿意,也可以在括号内传递唯一的键。

$users = mysqli_query($con, "SELECT email, pass FROM users");
while ($row = mysqli_fetch_assoc($users)) {
    $email = $row['email'];
    $pass = $row['pass'];
    $user_pass_final[$email] = $pass;
}
print_r($user_pass_final);

此外,您还将执行双重获取,并在数组上获取数据,该数组应失败,而不是:

$users=mysqli_fetch_assoc(mysqli_query($con,"SELECT email, pass FROM users"));
while ($row = mysqli_fetch_assoc($users)) {

你应该分配 query 返回 $users :

$users=mysqli_query($con,"SELECT email, pass FROM users");
while ($row = mysqli_fetch_assoc($users)) {

最后一点注意,密码不应以纯文本形式存储。看到了吗https://security.stackexchange.com/questions/120540/why-shouldnt-i-store-passwords-in-plaintext

相关问题