Reputation:
I am trying the following code to get results from query and display it in the tes.php
page.
<?php
function db_connect()
{
$handle=new mysqli('localhost','rekandoa','rekandoa','rekandoa');
if (!$handle)
{
return false;
}
return $handle;
}
function get_member()
{
$handle=db_connect();
$sql="Select email,nama,alamat,kota,propinsi from users where email=?";
$stmt=$handle->prepare($sql);
$mail='[email protected]';
$stmt->bind_param("s",$mail);
$stmt->execute();
$stmt->bind_result($email,$nama,$alamat,$kota,$propinsi);
$result=$stmt->fetch();
return $result;
}
?>
<?php
error_reporting(E_ALL & ~E_NOTICE);
include('db.inc.php');
$w=get_member();
echo $w['member_id'];
echo '<br>';
echo $w['email'];
echo '<br>';
echo $w['status'];
echo '<br>';
?>
I got no error message but the results are not shown, it is just a blank page.
What did I do wrong?
Upvotes: 2
Views: 3869
Reputation: 75784
$stmt->bind_result($email,$nama,$alamat,$kota,$propinsi);
The above line will make sure that the results are stored in the variables that you provide. The fetch()
function returns TRUE
or FALSE
- whether the query was successfull, not the actual result. You would need something like
return array(
'email' => $email,
'nama' => $nama,
'alamat' => $alamat,
'kota' => $kota,
'propinsi' => $propinsi);
Upvotes: 5
Reputation: 13536
fetch() itself doesnt return an array it returns a boolean indicating wheter it got a row. thus you can do:
while($stmt->fetch()) {
//$email,$nama,$alamat,$kota,$propinsi are now filled
}
binding values into an array dynamically (unlike soulmerge's solution) takes a little more craft. I created a Database class that does just that. It acts as a wrapper arround mysqli prepared statements to return results as objects where the select columns act as properties on the object. If you take out the cast to object in the class it will return arrays as you want.
Upvotes: 1