简体   繁体   English

PHP-Mysqli |中文 我正在尝试输出数据,但我一直在获取空数组

[英]PHP - Mysqli | i'm trying to output data but i keep getting empty array

i'm trying to get user information from database, but it keep giving me an empty array. 我试图从数据库中获取用户信息,但它一直给我一个空数组。
code : i.stack.imgur.com/Op4HB.png 代码:i.stack.imgur.com/Op4HB.png

$information = array();
$user = 'admin';
$myuser = new mysqli(HOST, USER, PASSWORD, DATABASE);

if ($info = $myuser->prepare("SELECT id,username,email,bd,firstname,lastname,gender,ppicture,cpicture FROM members WHERE username = ?")) {
    $info->bind_param("s", $user);
    /* execute query */
    $info->execute();
    /* get result */
    $result = $info->get_result();
    /* bind result variables */
    $info->bind_result($information['id'],$information['username'],$information['email'],$information['birthday'],$information['first_name'],$information['last_name'],$information['gender'],$information['profile_picture'],$information['cover_picture']);
    $info->fetch();
    $rows = $result->num_rows;
    $info->close();
}
if (!$rows) { 
    redirect('http://example.com/'); 
}  else { 
    print_r($information); 
}

this is what i get : 这就是我得到的: 结果

can someone help me in this ? 有人可以帮我吗? i use mysqli all the time but i don't know what went wrong this time. 我一直在使用mysqli,但是我不知道这次出了什么问题。

thanks. 谢谢。

Try this 尝试这个

Fetch & display the results inside the while loop 在while循环内获取并显示结果

if ($info = $myuser->prepare("SELECT id,username,email,bd,firstname,lastname,gender,ppicture,cpicture FROM members WHERE username = ?")) {
        $info->bind_param("s", $user);
        /* execute query */
        $info->execute();
        /* get result */
        $result = $info->get_result();
        /* bind result variables */
        $info->bind_result($information['id'],$information['username'],$information['email'],$information['birthday'],$information['first_name'],$information['last_name'],$information['gender'],$information['profile_picture'],$information['cover_picture']);

        $rows = $result->num_rows;

    }
    if (!$rows) { 
        redirect('http://example.com/'); 
    }  else { 
    while ($info->fetch()) {
           print_r($information); 
        }

    }

EDIT 编辑

 if ($info = $myuser->prepare("SELECT email FROM members WHERE username = ?")) {
            $info->bind_param("s", $user);
            /* execute query */
            $info->execute();

            /* bind result variables */
    $info->bind_result($email);

    $info->fetch();

    printf("%s \n", $email);
}

i fixed it myself 我自己修好了

i found the answer at http://php.net 我在http://php.net找到了答案

thanks anyways ! 不管怎么说,多谢拉 !

<?php 

    $mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    $query = "SELECT id,username,email,bd,firstname,lastname,gender,ppicture,cpicture FROM members WHERE username = ?";
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("s", $user);
    $stmt->execute();
    $res = $stmt->get_result();
    $rows = $res->num_rows;
    $information = $res->fetch_assoc();

    if($rows == 0 ) {
        redirect("http://example.com");
    }

    print_r($information);


?>

PS: account.php is being required by index.php so it gets ' $user ' from there PS:index.php要求使用account.php,以便从那里获取' $ user '

output 产量

Array ( [id] => 15 [username] => admin [email] => admin@theboat.tn [bd] => 2017-02-07 [firstname] => Saif [lastname] => Eddin [gender] => male [ppicture] => 1f4c1b47a3910039e60851c453ae4d80_a.jpg [cpicture] => default_c.jpg ) 数组([id] => 15 [username] => admin [email] => admin@theboat.tn [bd] => 2017-02-07 [firstname] => Saif [lastname] => Eddin [gender] = >男性[ppicture] => 1f4c1b47a3910039e60851c453ae4d80_a.jpg [cpicture] => default_c.jpg)

暂无
暂无

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

相关问题 我正在尝试将一些数据插入MySqli - I'm Trying to insert some Data into MySqli 我正在尝试将其重写为mysqli查询,但我一直收到错误消息 - I am trying to rerwrite this as a mysqli query but I keep getting an error 我正在尝试使用 php 格式化数组的输出 - I'm Trying to format the output of an array using php 每次尝试安装php-mysqli扩展名时,都会出现类似“ E:软件包&#39;php-mysqli&#39;没有候选安装程序”的错误。 - Every time I'm trying to install the php-mysqli extension I'm getting error like “E: Package 'php-mysqli' has no installation candidate” 我在php array_reverse()函数中得到了奇怪的输出 - i'm getting the weird output in php array_reverse() functions 我正在尝试使用PHP连接到Azure服务总线队列,但一直出现此错误 - I'm trying to connect to an Azure service bus queue using PHP and I keep getting this error 我正在尝试使用mysqli查询我的mysql数据库,但是却出现内存错误 - I'm trying to query my mysql db with mysqli, however I'm getting memory error 我正在尝试将 HTML 添加到 PHP 数组中 - I'm trying to add HTML into a PHP array 我正在尝试运行 php artisan migrate 命令,但我没有得到它。 我收到此错误 - I'm trying to run the php artisan migrate command, but I'm not getting it. I'm getting this error 当我尝试使用php下载文件时文件为空 - File is empty when I'm trying to download a file using php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM