473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP 5 and PDO

I am working on converting some old PHP 4 scripts to PHP 5. I used to
use the PEAR DB module in PHP 4 and in PHP 5 I want to use PDO.

I am using this code to connect to the DB:

$dbh = new PDO('mysql:host=localhost;dbname=name', 'username',
'password');

Later on in the same page I use this code and it works just fine:

foreach($dbh->query('SELECT artist_id, artist_name FROM 10spot_artist
ORDER BY artist_name') as $row) {
$id = $row['artist_id'];
$name = stripslashes($row['artist_name']);
echo "<tr>
<td width=\"240\" height=\"35\"
background=\"images/ArtistColumnBack.gif\">
<b><font face=\"Arial\" size=\"2\"
color=\"#FFFFFF
\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp ;&nbsp;
<a href=\"Artist.php?id=$id\" style=
\"text-decoration: none\">$name</a></font></b></td>
</tr>";
}
$dbh = null;

Later on in the same page I use the following code and it doesn't
work. I don't get any errors or anything the page just stops executing
right at this code. I'm new to using PDO. Any thoughts on why this
doesn't work? Thanks in advance!

$head_sel_sql = $dbh->query('SELECT select_id, headline_id FROM
10spot_head_sel');
$headline_id = $head_sel_sql['headline_id'];
$dbh = null;

$head_display = $dbh->query('SELECT headline_id,
date, headline, headline_description FROM 10spot_headlines WHERE
headline_id = $headline_id');
$display_head =
stripslashes($head_display['headline']);
$display_date = $head_display['date'];
$display_description =
stripslashes($head_display['headline_description']);
echo "<tr>
<td><i><b><font face=\"Arial\" color=\"#BE2E2E
\">$display_head</font></b></i></td>
<td>
<p align=\"right\"><font size=\"2\" face=
\"Arial\">$display_date</font></td>
</tr>
<tr>
<td colspan=\"2\">
<font face=\"Arial\" style=\"font-size: 9pt\">
$display_description</font></td>
</tr>";
$dbh = null;

Apr 29 '07 #1
7 1359
On Sun, 29 Apr 2007 08:13:40 -0700, mpar612 wrote:
I am working on converting some old PHP 4 scripts to PHP 5. I used to
use the PEAR DB module in PHP 4 and in PHP 5 I want to use PDO.

I am using this code to connect to the DB:

$dbh = new PDO('mysql:host=localhost;dbname=name', 'username',
'password');

Later on in the same page I use this code and it works just fine:

foreach($dbh->query('SELECT artist_id, artist_name FROM 10spot_artist
ORDER BY artist_name') as $row) {
[SNAP]
}
$dbh = null;

Later on in the same page I use the following code and it doesn't
work. I don't get any errors or anything the page just stops executing
right at this code. I'm new to using PDO. Any thoughts on why this
doesn't work? Thanks in advance!

$head_sel_sql = $dbh->query('SELECT select_id, headline_id FROM
10spot_head_sel');
(assuming you posted all relevant code)

You create a PDO instance $dbh, use it, and then you destroy the object:
the next query can never be executed. Why the $dbh=null; ?
Either strip those out of your code, or re-instantiate a PDO object before
using it again.

The object will automatically be destroyed by PHP when the script ends.

HTH

Sh
Apr 29 '07 #2
(assuming you posted all relevant code)
>
You create a PDO instance $dbh, use it, and then you destroy the object:
the next query can never be executed. Why the $dbh=null; ?
Either strip those out of your code, or re-instantiate a PDO object before
using it again.

The object will automatically be destroyed by PHP when the script ends.

HTH

Sh
Thanks! I removed those $dbh=null; and still no luck. I know the SQL
statements are correct. They work perfectly in the code that I wrote
the code that uses the PEAR DB module. I have posted every bit of PHP
code that is on the page. All of the rest is just static HTML. Any
other thoughts? Thanks!

Apr 29 '07 #3

"mpar612" <mp*****@gmail.comschreef in bericht
news:11*********************@h2g2000hsg.googlegrou ps.com...
>(assuming you posted all relevant code)

You create a PDO instance $dbh, use it, and then you destroy the object:
the next query can never be executed. Why the $dbh=null; ?
Either strip those out of your code, or re-instantiate a PDO object
before
using it again.

The object will automatically be destroyed by PHP when the script ends.

HTH

Sh

Thanks! I removed those $dbh=null; and still no luck. I know the SQL
statements are correct. They work perfectly in the code that I wrote
the code that uses the PEAR DB module. I have posted every bit of PHP
code that is on the page. All of the rest is just static HTML. Any
other thoughts? Thanks!

What happens if you do a...

try
{
$dbh->query('my query');
}
catch ( PDOException $e )
{
die( $e->getMessage() . ' on line: ' . __LINE__ );
}

....for both queries?

I suspect it could have something to do with buffered queries. I've had some
occasions where I got error messages stating that I should use:

$dbh->setAttribute( PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE );

... because another query is not finished yet, eventhough for instance, only
one
row should be returned and I fetched that row already.
I haven't given it enough attention yet, and just set the attribute to true,
but it looks like it could be some bug.

HTH
Apr 29 '07 #4
On Sun, 29 Apr 2007 13:06:51 -0700, mpar612 wrote:
>(assuming you posted all relevant code)

You create a PDO instance $dbh, use it, and then you destroy the object:
the next query can never be executed. Why the $dbh=null; ?
Either strip those out of your code, or re-instantiate a PDO object before
using it again.

The object will automatically be destroyed by PHP when the script ends.

HTH

Sh

Thanks! I removed those $dbh=null; and still no luck. I know the SQL
statements are correct. They work perfectly in the code that I wrote
the code that uses the PEAR DB module. I have posted every bit of PHP
code that is on the page. All of the rest is just static HTML. Any
other thoughts? Thanks!
You're welcome.

Sorry, I am not even remotely familiar with PDO's intricacies. Any attempt
to say something meaningful about it on my part would be a waste of group
space.

Amygdala seems to have at least some experience with PDO, he's your man!
Signing off on this one.
Rgds
Sh.
Apr 29 '07 #5
On Apr 29, 4:12 pm, "amygdala" <nore...@noreply.comwrote:
"mpar612" <mpar...@gmail.comschreef in berichtnews:11*********************@h2g2000hsg.goo glegroups.com...
(assuming you posted all relevant code)
You create a PDO instance $dbh, use it, and then you destroy the object:
the next query can never be executed. Why the $dbh=null; ?
Either strip those out of your code, or re-instantiate a PDO object
before
using it again.
The object will automatically be destroyed by PHP when the script ends.
HTH
Sh
Thanks! I removed those $dbh=null; and still no luck. I know the SQL
statements are correct. They work perfectly in the code that I wrote
the code that uses the PEAR DB module. I have posted every bit of PHP
code that is on the page. All of the rest is just static HTML. Any
other thoughts? Thanks!

What happens if you do a...

try
{
$dbh->query('my query');}

catch ( PDOException $e )
{
die( $e->getMessage() . ' on line: ' . __LINE__ );

}

...for both queries?

I suspect it could have something to do with buffered queries. I've had some
occasions where I got error messages stating that I should use:

$dbh->setAttribute( PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE );

.. because another query is not finished yet, eventhough for instance, only
one
row should be returned and I fetched that row already.
I haven't given it enough attention yet, and just set the attribute to true,
but it looks like it could be some bug.

HTH
Thanks for the response! That didn't do anything, but return a blank
screen. I did a little playing around and the first statement works.
If I comment out that query and enter a hard value into the 2nd query,
it works. So, I think I need to figure out how to close the first
query so the 2nd query can execute. Does that make sense? Any
thoughts?

Thanks!

Apr 29 '07 #6

"mpar612" <mp*****@gmail.comschreef in bericht
news:11**********************@h2g2000hsg.googlegro ups.com...
On Apr 29, 4:12 pm, "amygdala" <nore...@noreply.comwrote:
>"mpar612" <mpar...@gmail.comschreef in
berichtnews:11*********************@h2g2000hsg.go oglegroups.com...
>(assuming you posted all relevant code)
>You create a PDO instance $dbh, use it, and then you destroy the
object:
the next query can never be executed. Why the $dbh=null; ?
Either strip those out of your code, or re-instantiate a PDO object
before
using it again.
>The object will automatically be destroyed by PHP when the script
ends.
>HTH
>Sh
Thanks! I removed those $dbh=null; and still no luck. I know the SQL
statements are correct. They work perfectly in the code that I wrote
the code that uses the PEAR DB module. I have posted every bit of PHP
code that is on the page. All of the rest is just static HTML. Any
other thoughts? Thanks!

What happens if you do a...

try
{
$dbh->query('my query');}

catch ( PDOException $e )
{
die( $e->getMessage() . ' on line: ' . __LINE__ );

}

...for both queries?

I suspect it could have something to do with buffered queries. I've had
some
occasions where I got error messages stating that I should use:

$dbh->setAttribute( PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE );

.. because another query is not finished yet, eventhough for instance,
only
one
row should be returned and I fetched that row already.
I haven't given it enough attention yet, and just set the attribute to
true,
but it looks like it could be some bug.

HTH

Thanks for the response! That didn't do anything, but return a blank
screen. I did a little playing around and the first statement works.
If I comment out that query and enter a hard value into the 2nd query,
it works. So, I think I need to figure out how to close the first
query so the 2nd query can execute. Does that make sense? Any
thoughts?

Thanks!
Now that I took a closer look at the code, is this the following really the
code you're using for the second query?

$head_display = $dbh->query('SELECT headline_id,
date, headline, headline_description FROM 10spot_headlines WHERE
headline_id = $headline_id');

If so, you should use " (double quotes), instead of ' (single quotes) to
let PHP evaluates the variable $headline_id.

HTH
Apr 30 '07 #7
On Apr 29, 8:33 pm, "amygdala" <nore...@noreply.comwrote:
"mpar612" <mpar...@gmail.comschreef in berichtnews:11**********************@h2g2000hsg.go oglegroups.com...
On Apr 29, 4:12 pm, "amygdala" <nore...@noreply.comwrote:
"mpar612" <mpar...@gmail.comschreef in
berichtnews:11*********************@h2g2000hsg.goo glegroups.com...
(assuming you posted all relevant code)
You create a PDO instance $dbh, use it, and then you destroy the
object:
the next query can never be executed. Why the $dbh=null; ?
Either strip those out of your code, or re-instantiate a PDO object
before
using it again.
The object will automatically be destroyed by PHP when the script
ends.
HTH
Sh
Thanks! I removed those $dbh=null; and still no luck. I know the SQL
statements are correct. They work perfectly in the code that I wrote
the code that uses the PEAR DB module. I have posted every bit of PHP
code that is on the page. All of the rest is just static HTML. Any
other thoughts? Thanks!
What happens if you do a...
try
{
$dbh->query('my query');}
catch ( PDOException $e )
{
die( $e->getMessage() . ' on line: ' . __LINE__ );
}
...for both queries?
I suspect it could have something to do with buffered queries. I've had
some
occasions where I got error messages stating that I should use:
$dbh->setAttribute( PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, TRUE );
.. because another query is not finished yet, eventhough for instance,
only
one
row should be returned and I fetched that row already.
I haven't given it enough attention yet, and just set the attribute to
true,
but it looks like it could be some bug.
HTH
Thanks for the response! That didn't do anything, but return a blank
screen. I did a little playing around and the first statement works.
If I comment out that query and enter a hard value into the 2nd query,
it works. So, I think I need to figure out how to close the first
query so the 2nd query can execute. Does that make sense? Any
thoughts?
Thanks!

Now that I took a closer look at the code, is this the following really the
code you're using for the second query?

$head_display = $dbh->query('SELECT headline_id,
date, headline, headline_description FROM 10spot_headlines WHERE
headline_id = $headline_id');

If so, you should use " (double quotes), instead of ' (single quotes) to
let PHP evaluates the variable $headline_id.

HTH
Thanks! It actually turned out to be an issue with my end. You need
to end the query() before you can begin using another one. Got it
figured out. Thanks again!

Apr 30 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
1
by: Richard Galli | last post by:
I want viewers to compare state laws on a single subject. Imagine a three-column table with a drop-down box on the top. A viewer selects a state from the list, and that state's text fills the...
4
by: Albert Ahtenberg | last post by:
Hello, I have two questions. 1. When the user presses the back button and returns to a form he filled the form is reseted. How do I leave there the values he inserted? 2. When the...
1
by: inderjit S Gabrie | last post by:
Hi all Here is the scenerio ...is it possibly to do this... i am getting valid course dates output on to a web which i have designed ....all is okay so far , look at the following web url ...
2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
3
by: Sandwick | last post by:
I am trying to change the size of a drawing so they are all 3x3. the script below is what i was trying to use to cut it in half ... I get errors. I can display the normal picture but not the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.