PHP 5 and PDO

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mpar612

    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;dbna me=name', 'username',
    'password');

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

    foreach($dbh->query('SELEC T artist_id, artist_name FROM 10spot_artist
    ORDER BY artist_name') as $row) {
    $id = $row['artist_id'];
    $name = stripslashes($r ow['artist_name']);
    echo "<tr>
    <td width=\"240\" height=\"35\"
    background=\"im ages/ArtistColumnBac k.gif\">
    <b><font face=\"Arial\" size=\"2\"
    color=\"#FFFFFF
    \">&nbsp;&nbsp; &nbsp;&nbsp;&nb sp;&nbsp;&nbsp; &nbsp;&nbsp;
    <a href=\"Artist.p hp?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('SELEC T select_id, headline_id FROM
    10spot_head_sel ');
    $headline_id = $head_sel_sql['headline_id'];
    $dbh = null;

    $head_display = $dbh->query('SELEC T headline_id,
    date, headline, headline_descri ption FROM 10spot_headline s WHERE
    headline_id = $headline_id');
    $display_head =
    stripslashes($h ead_display['headline']);
    $display_date = $head_display['date'];
    $display_descri ption =
    stripslashes($h ead_display['headline_descr iption']);
    echo "<tr>
    <td><i><b><fo nt face=\"Arial\" color=\"#BE2E2E
    \">$display_hea d</font></b></i></td>
    <td>
    <p align=\"right\" ><font size=\"2\" face=
    \"Arial\">$disp lay_date</font></td>
    </tr>
    <tr>
    <td colspan=\"2\">
    <font face=\"Arial\" style=\"font-size: 9pt\">
    $display_descri ption</font></td>
    </tr>";
    $dbh = null;

  • Schraalhans Keukenmeester

    #2
    Re: PHP 5 and PDO

    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;dbna me=name', 'username',
    'password');
    >
    Later on in the same page I use this code and it works just fine:
    >
    foreach($dbh->query('SELEC T 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('SELEC T 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

    Comment

    • mpar612

      #3
      Re: PHP 5 and PDO

      (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!

      Comment

      • amygdala

        #4
        Re: PHP 5 and PDO


        "mpar612" <mpar612@gmail. comschreef in bericht
        news:1177877211 .921774.82090@h 2g2000hsg.googl egroups.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_Q UERY, 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


        Comment

        • Schraalhans Keukenmeester

          #5
          Re: PHP 5 and PDO

          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.

          Comment

          • mpar612

            #6
            Re: PHP 5 and PDO

            On Apr 29, 4:12 pm, "amygdala" <nore...@norepl y.comwrote:
            "mpar612" <mpar...@gmail. comschreef in berichtnews:117 7877211.921774. 82090@h2g2000hs g.googlegroups. 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_Q UERY, 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!

            Comment

            • amygdala

              #7
              Re: PHP 5 and PDO


              "mpar612" <mpar612@gmail. comschreef in bericht
              news:1177882592 .869930.194900@ h2g2000hsg.goog legroups.com...
              On Apr 29, 4:12 pm, "amygdala" <nore...@norepl y.comwrote:
              >"mpar612" <mpar...@gmail. comschreef in
              >berichtnews:11 77877211.921774 .82090@h2g2000h sg.googlegroups .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_Q UERY, 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('SELEC T headline_id,
              date, headline, headline_descri ption FROM 10spot_headline s 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


              Comment

              • mpar612

                #8
                Re: PHP 5 and PDO

                On Apr 29, 8:33 pm, "amygdala" <nore...@norepl y.comwrote:
                "mpar612" <mpar...@gmail. comschreef in berichtnews:117 7882592.869930. 194900@h2g2000h sg.googlegroups .com...
                >
                >
                >
                On Apr 29, 4:12 pm, "amygdala" <nore...@norepl y.comwrote:
                "mpar612" <mpar...@gmail. comschreef in
                berichtnews:117 7877211.921774. 82090@h2g2000hs g.googlegroups. 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_Q UERY, 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('SELEC T headline_id,
                date, headline, headline_descri ption FROM 10spot_headline s 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!

                Comment

                Working...