2

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?

3
  • When pasting code, please use the code function in the editor (binary icon) Commented Mar 4, 2009 at 13:19
  • @nduplessis: Nothing wrong with the question, lets stay productive. Commented Mar 4, 2009 at 13:20
  • 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? Commented Mar 4, 2009 at 15:18

2 Answers 2

5
$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);
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.