Jump to content
funbinod

working with php and postgre

Recommended Posts

hello all!

i'm learning postgresql.

i was just trying to fetch some data using prepared statement. but found that it is quiet different than mysql.

the all i learnt searching on the web is this.

$cuidstmt = $pgsql->prepare("SELECT cid, uid FROM user WHERE name=?");
if(!$cuidstmt) {
	echo "Error: " . $pgsql->errorInfo()[2];
} else {
	$cuidstmt->bindParam('i', $u);
	$cuidstmt->execute();
	$cuidstmt->fetch();
}

but i could not understand how can i print the required data. where the data is stored.

just like in mysqli we can bind result on a string, how can i do that in pgsql?

can u please help me?

Share this post


Link to post
Share on other sites

i found that what i learnt so far is also wrong.

i corrected the aforementioned code like this

$cuidstmt = $pgsql->prepare("SELECT cid, uid FROM user WHERE name=?");
if(!$cuidstmt) {
	echo "Error: " . $pgsql->errorInfo()[2];
} else {
	$cuidstmt->execute(array($u));
	$cuidstmt->execute();
	$cuidstmt->fetch();
}

but i still couldn't understand how can i get the result(s). where can i get the data from 'cid' and 'uid' columns mentioned in the prepared statement.

thank u all in advance.

Share this post


Link to post
Share on other sites

The same way you would use to display results from mysql, a while loop that gets each row from $cuidstmt ->fetch( ) where you refer to specific column names.

Share this post


Link to post
Share on other sites

Are you using a database wrapper or something?  I don't see an object-oriented interface for the PGSQL functions in the PHP manual.

Share this post


Link to post
Share on other sites
3 hours ago, justsomeguy said:

Are you using a database wrapper or something?  I don't see an object-oriented interface for the PGSQL functions in the PHP manual.

Looks like PDO to me.

Share this post


Link to post
Share on other sites

If that's PDO then it's exactly the same.  PDO isn't different based on which database driver is used, that's the entire purpose.  Different databases might have different capabilities, though, for example when checking the number of records that were returned.

Share this post


Link to post
Share on other sites

I'd assign the value returned by ->fetch() to a variable and print it out to see what it contains.

You only need to call ->execute() once, the second one will probably cause an error.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...