[英]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.