Jump to content

working with php and postgre


funbinod

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?

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...