views:

554

answers:

3

I am trying the following code to get results from query and display it in the tes.php page.

db.inc.php

<?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;
}
?>

tes.php

<?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?

A: 

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.

Martijn Laarman
+3  A: 
$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);
soulmerge
A: 

Are there 3 <br> tags in the "blank document"s source? If not, there could be an exception which you just don't see. Try to check your apaches error.log. Maybe the mysqli extension is missing?

Karsten