Google Groups Home
Help | Sign in
mysqli's prepared statement api
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
noodlet@gmail.com  
View profile
 More options Nov 21 2005, 11:46 am
From: "nood...@gmail.com" <nood...@gmail.com>
Date: Mon, 21 Nov 2005 16:46:39 -0000
Local: Mon, Nov 21 2005 11:46 am
Subject: mysqli's prepared statement api
Hi,
First Google Groups post, so be nice etc.

Is there a way to get a result object from a prepared statement? Using
$stmt->bind_result() isn't possible in the case of the application I'm
trying to write as I don't know the query in advance. It seems like a
pretty dumb idea altogether, come to think of it.

Cheers,
noodl


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
johnja...@gmail.com  
View profile
 More options Nov 21 2005, 1:25 pm
From: johnja...@gmail.com
Date: Mon, 21 Nov 2005 18:25:27 -0000
Local: Mon, Nov 21 2005 1:25 pm
Subject: Re: mysqli's prepared statement api
noodl,

There is no way natively to do this, I think you are probably talking
about the equalivant to mysql_fetch_object() for mysqli.

I too have run into the problem you are having, and got around it by
this:

class clsDB extends mysqli {

[ ... constructor/connection stuff here ]

public function getResultParams($objST) {
               $objRes = $objST->result_metadata();
                while ($field = $objRes->fetch_field()) {
                        $arrayReturn[] = "\$res" . $field->name . "";
                }
                $strReturn = implode(",",$arrayReturn);
                return $strReturn;

}
}

$objDB = new clsDB();
$objStmt = $objDB->prepare("SELECT column1,some_other_column from
table");
$objStmt->execute();
$strResParams = getResultParams($objStmt);
$strEvalCode = '$objStmt->bind_result('. $strResParams .');';
eval($strEvalCode);
$objStmt->fetch();
// result stored in vars prefixed by "res": $rescolumn1,
$ressome_other_column
?>

Now this method uses eval, and quite certainly you could easily mod the
code above to store the values into an instance of stdClass...but back
to the eval. I know historically a lot of people have dubbed eval a
"slow" operation that should be avoided.

While I tend to agree with the avoided part, the "slow" part is just
plain rubbish. There have been numerous benchmarks under the comments
section of the eval function:

http://us2.php.net/eval

And none have shown "slower" PHP scripts because of the use of eval.
Albeit with one exception, Windows and mod_php. Spawning processes in a
*nix system is much less resource intensive than in Windows. IIRC,
eval() calls result in a new PHP process being spawned. Again, even in
Windows, the extra load time is mostly irrelevant.

Using the above code to get an result object instead of just variables:

$strResParams = getResultParams($objStmt);
$strEvalCode = '$objStmt->bind_result('. $strResParams .');
$objResult = new stdClass();
$arrayVariables = explode(",",$strResParams);
foreach($arrayVariables as $pValue) {
        $pValue = str_replace("\$res","",$pValue); //
        $objResult->{$pValue};

}

';
eval($strEvalCode);

Probably better ways to do that, but just trying to give you a
conceptual idea.

Regards


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
noodlet@gmail.com  
View profile
 More options Nov 22 2005, 4:58 am
From: "nood...@gmail.com" <nood...@gmail.com>
Date: Tue, 22 Nov 2005 09:58:46 -0000
Local: Tues, Nov 22 2005 4:58 am
Subject: Re: mysqli's prepared statement api
Hi,

Thanks for your quick reply. I tried using eval(), and even
call_user_func_array() but couldn't get a sensible result from either
of them. Your code looks neat enough, and like you said, performance
considerations with eval() are irrelevant.

I got around this by using PEAR::DB instead. A problem I have now is
that prepared statements don't work when using LIKE. So,
SELECT blah FROM table WHERE blah LIKE '?%'
causes all sorts of mayhem. I don't know if this is a limitation of
PEAR::DB or just prepared statements in general. Is mysqli able to
handle this case?

Cheers,
noodl


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Jawed  
View profile
 More options Nov 23 2005, 2:00 am
From: John Jawed <johnja...@gmail.com>
Date: Tue, 22 Nov 2005 23:00:02 -0800
Local: Wed, Nov 23 2005 2:00 am
Subject: Re: [MySQLi-g] Re: mysqli's prepared statement api

noodl,

You may do so by binding the wildcard in the actual variable:

$blah = new mysqli();
$stmt = $blah->prepare("SELECT * FROM test where ID LIKE ?");
$null = 9 . "%"; // Put wildcard in here
$stmt->bind_param("s",$null);
$stmt->execute();
$stmt->bind_result($b,$c);
while($stmt->fetch()) {
        echo "$b <br />"; // will print all ids that start with 9

}

Excuse my variable naming, just did a rough copy from a test script.

Regards

On 11/22/05, nood...@gmail.com <nood...@gmail.com> wrote:

--
John Jawed
Zend Certified Software Engineer
MySQL AB Certified Developer
http://www.johnjawed.com
http://groups-beta.google.com/group/MySQLi

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
noodlet@gmail.com  
View profile
 More options Nov 23 2005, 6:02 am
From: "nood...@gmail.com" <nood...@gmail.com>
Date: Wed, 23 Nov 2005 11:02:44 -0000
Local: Wed, Nov 23 2005 6:02 am
Subject: Re: mysqli's prepared statement api
Thanks again John.

Your solution looks fine for situations where the query is known in
advance, but like the original case, doesn't neatly handle a generic
query implementation. Hopefully mysqli will address these issues as it
matures.

Cheers!
noodl


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Jawed  
View profile
 More options Nov 23 2005, 4:58 pm
From: John Jawed <johnja...@gmail.com>
Date: Wed, 23 Nov 2005 13:58:03 -0800
Local: Wed, Nov 23 2005 4:58 pm
Subject: Re: [MySQLi-g] Re: mysqli's prepared statement api

noodl,

I don't understand. You only need to know how the wildcard will be used in
advance, not the query. Am I missing something?

Regards

On 11/23/05, nood...@gmail.com <nood...@gmail.com> wrote:

> Thanks again John.

> Your solution looks fine for situations where the query is known in
> advance, but like the original case, doesn't neatly handle a generic
> query implementation. Hopefully mysqli will address these issues as it
> matures.

> Cheers!
> noodl

--
John Jawed

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google