4 Replies - 1431 Views - Last Post: 12 September 2008 - 04:40 AM Rate Topic: -----

#1 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

how to use prepared statements in php

Post icon  Posted 11 September 2008 - 09:38 PM

Hi all,

i heard that prepared statements help u when ever u want to execute a query repeatedly

in such a scenario prepared stmts helps a lot and improves speed also

is it right. if so can i implement them in php

can someone give me any example of how to use prepared statements in php

thanks
Is This A Good Question/Topic? 0
  • +

Replies To: how to use prepared statements in php

#2 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: how to use prepared statements in php

Posted 11 September 2008 - 10:36 PM

Hi ,

i'm trying the following code to work with prepared stmts
 
<?
	$remdt=$_GET['date'];
	$res="Getting..";
	$dbh = new mysqli("localhost", "iamuser", "password", "finddb");

	
	$stmt = $dbh->prepare("SELECT remMsg FROM reminder where remDate = ?");

	$stmt->bind_param('s',$remdt);
	
		//print "Hi";	
	
	if ($stmt->execute()) 
	{
		if ($stmt->fetch()===true) 
		{
			print "Hi";
			$stmt->bind_result($res);
			printf("%s", $res);
		}
		else
		{
			print " U have no reminder on : $remdt";
		}
	
	}
	else
	{
		print "Query can not be executed..";
	}
?>



but the above is returning nothing

my table structure is as follows
id number
remMsg varchar
remDate varchar



i have one record as follows
id	  remMsg				remDate
1	   Happy new year	1-1-2009


i'm passing $_get['date'] as 1-1-2009..(if i echo it i get 1-1-2009)

so my output should be Happy new year
but i am not getting desired output.i am getting only "higetting"

any help pls...

This post has been edited by kummu4help: 11 September 2008 - 10:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: how to use prepared statements in php

Posted 12 September 2008 - 01:20 AM

don't have time to run through all the code now, but i will over my lunch time.

Had a quick browse over it and noticed this:

if ($stmt->fetch()===true)

which should be:

if ($stmt->fetch()==true)
Was This Post Helpful? 0
  • +
  • -

#4 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: how to use prepared statements in php

Posted 12 September 2008 - 04:05 AM

Hi all,

thanks for quick reply
particularly pemcconnell. He wil always be there when ever i have a post..
any way i could able to solve my problem

i just changed the following block i.e
   if ($stmt->execute()) 
	{
		if ($stmt->fetch()===true) 
		{
			print "Hi";
			$stmt->bind_result($res);
			printf("%s", $res);
		}


as follows
 
  $stmt->bind_result($res);
  if ($stmt->execute()) 
	{
		if ($stmt->fetch()===true) 
		{
			print "Hi";
  
			printf("%s", $res);
		}



the $stmt->bind_result($res) should be above the $stmt->execute() stmt...

after a big search i found that

any way thanks for all
but can any one point me to a good prepared statement explanation link

i saw one in dic tutorial but it is a bit tough to grasp for me
:blink:
Was This Post Helpful? 0
  • +
  • -

#5 pemcconnell  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 54
  • View blog
  • Posts: 472
  • Joined: 05-August 08

Re: how to use prepared statements in php

Posted 12 September 2008 - 04:40 AM

There's a bit of coverage on this page:

http://devzone.zend....ode/view/id/686

Seems to break it down quite well. (The prepared statement section is about half way down that page)

Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1