Jump to content

prepared statement within a function

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Hi guys..i'm 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='yonghan79@gmail.com';

	$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 just a blank page..Suppose i did something wrong..Please help me..Thanks...

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
the $handle becomes an object, and it can't be copied like normal values.

try
return &$handle;
which would return a reference to the object instead.

EDIT

    $stmt->bind_result($email,$nama,$alamat,$kota,$propinsi);  
    $result=$stmt->fetch();

makes a small mess for you. the $result will only contain true or false. the actual values will end up in $email,$nama,$alamat,$kota,$propinsi variables.

suggestion:
    $stmt->bind_result($email,$nama,$alamat,$kota,$propinsi);  
    $stmt->fetch();
    $result['email'] = $email;
    $result['nama'] $nama;
    etc etc...
    return $result;

I'm unsure if you can bind results to arrays directly, try this...
$stmt->bind_result($result['email'],$result['nama'],$result['alamat'],
                          $result['kota'],$result['propinsi']);
but I'm not sure if it works...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Do you mean that i should put the


return &$handle;


on the function get_member()?About your suggestion,how should i put it on the tes.php page?Thanks a lot..

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
on your first question, yes.

in tes.php if the othersuggestin works, do as you have it now, but with correct names in thearray...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
How about this code that you gave me?

$stmt->bind_result($email,$nama,$alamat,$kota,$propinsi);  
    $stmt->fetch();
    $result['email'] = $email;
    $result['nama'] $nama;
    etc etc...
    return $result; 

What should i use on the tes.php page?Thanks..

#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
same there too actually...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Hi Jan,i got it work finally.And sorry before,i didn't try your codes yet..I got this code from PHP and MySQL coding tips - Page 8 - SitePoint Forums
This is the code what i got

$mysqli = new mysqli($dbHost, $dbUsername, $dbPassword, $dbDatabase);

$stmt = $mysqli->prepare('select * from foobar');

$stmt->execute();

$stmt->store_result();



// this returns info about the query, script uses the 

// $meta->name to create an array with the key's as

// the column names

$meta = $stmt->result_metadata();



// this needs to be $bindVarArray[] = &$results[$column->name];

// rather than $results[$column->name] = NULL, when using 

// call_user_func_array it needs the &, I'm not sure why

while ($column = $meta->fetch_field()) {

    $bindVarArray[] = &$results[$column->name];

}        

call_user_func_array(array($stmt, 'bind_result'), $bindVarArray);



$stmt->fetch();



echo var_dump($results);

// outputs:

// 

// array(3) {

//  ["id"]=>

//  &int(1)

//  ["foo"]=>

//  &string(11) "This is Foo"

//  ["bar"]=>

//  &string(11) "This is Bar"

// } 


i modified it this way because i'm using a function

function get_member()
{
    $handle=db_connect();
    //$sql="Select email,nama,alamat,kota,propinsi from users where email='yonghan79@gmail.com'";
    $sql="Select id,email,nama,alamat,kota,propinsi from users where email=?";
    //$hasil=$handle->query($sql);
    //$result=$hasil->fetch_array();
	$stmt=$handle->prepare($sql);
	$mail='yonghan79@gmail.com';
	$stmt->bind_param("s",$mail); 
	$stmt->execute();
    $stmt->store_result();
// this returns info about the query, script uses the 
// $meta->name to create an array with the key's as
// the column names
$meta = $stmt->result_metadata();
// this needs to be $bindVarArray[] = &$results[$column->name];
// rather than $results[$column->name] = NULL, when using 
// call_user_func_array it needs the &, I'm not sure why
while ($column = $meta->fetch_field()) {
    $bindVarArray[] = &$results[$column->name];
}        
call_user_func_array(array($stmt, 'bind_result'), $bindVarArray);
$stmt->fetch();
return $results;
}

As for the tes.php,i did'nt modify..Thanks a lot jan. :)

#8
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
by the way, I'm Orjan, not Jan...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#9
yonghan

yonghan

    Learning Programmer

  • Members
  • PipPipPip
  • 99 posts
Sorry Orjan... :D