简体   繁体   中英

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

$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.

thanks.

Try this

Fetch & display the results inside the while loop

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

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

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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