Advice need on how to fetch data from mysql database using PDO

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hamiltonsteele
    Confirmed User
    • Jul 2003
    • 1041

    #1

    Advice need on how to fetch data from mysql database using PDO

    I'm seriously rusty with this stuff.

    I've also bee trying to do the same thing with mysqli_query but no success.

    So far the only luck I've had is being able to connect to the database
    http://steelehard.com/ الإباحية باللغة العربية
    Discord: https://discord.gg/QWJpmDSFc2
    http://le-beverley.com/ Under Development
    Twitter: https://twitter.com/steele_hard
    Spotify : https://open.spotify.com/show/43uaTA1AEPa2Nf0Tmvppd0
  • fuzebox
    making it rain
    • Oct 2003
    • 22385

    #2
    You should ask Publisher Bucks

    Comment

    • sarettah
      still insane
      • Oct 2002
      • 14468

      #3
      Originally posted by hamiltonsteele
      I'm seriously rusty with this stuff.

      I've also bee trying to do the same thing with mysqli_query but no success.

      So far the only luck I've had is being able to connect to the database
      Ok, you have the connection

      ($db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf 8",$dbuser,$dbpass,array(PDO::ATTR_EMULATE_PREPARE S => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));)

      Now you just use the connection.

      simple query:

      $stmnt=$db->query('select * from tablename')

      check for results

      $stmnt->rowcount()

      fetch results into an array:

      $stmnt->fetch(PDO::FETCH_ASSOC)

      So for a typical table read you have:

      $db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf 8",$dbuser,$dbpass,array(PDO::ATTR_EMULATE_PREPARE S => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

      $stmnt=$db->query('select * from tablename');

      if($stmnt->rowcount()>0)
      {
      while($row = $stmnt->fetch(PDO::FETCH_ASSOC))
      {
      do something.....
      }

      That is a very simplified view. In a production environment you would be using try...catch on the connect and on the query calls. For the query itself you would probably use a prepare and execute.

      Your list of pdo functions and constants are at:

      https://www.php.net/manual/en/book.pdo.php

      Have fun.


      .
      Free Huggles !!!!!!

      Comment

      • hamiltonsteele
        Confirmed User
        • Jul 2003
        • 1041

        #4
        Originally posted by sarettah
        Ok, you have the connection

        ($db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf 8",$dbuser,$dbpass,array(PDO::ATTR_EMULATE_PREPARE S => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));)

        Now you just use the connection.

        simple query:

        $stmnt=$db->query('select * from tablename')

        check for results

        $stmnt->rowcount()

        fetch results into an array:

        $stmnt->fetch(PDO::FETCH_ASSOC)

        So for a typical table read you have:

        $db = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf 8",$dbuser,$dbpass,array(PDO::ATTR_EMULATE_PREPARE S => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

        $stmnt=$db->query('select * from tablename');

        if($stmnt->rowcount()>0)
        {
        while($row = $stmnt->fetch(PDO::FETCH_ASSOC))
        {
        do something.....
        }

        That is a very simplified view. In a production environment you would be using try...catch on the connect and on the query calls. For the query itself you would probably use a prepare and execute.

        Your list of pdo functions and constants are at:

        https://www.php.net/manual/en/book.pdo.php

        Have fun.


        .
        This is the actual file that I'm working on:

        <!doctype html>
        <html>
        <head>
        <base href="https://gfy.com/" /><!--[if IE]></base><![endif]--></head>
        <title>mysql test</title>
        <body>
        <?php
        $servername = "localhost";
        $username = "root";
        $password = "password";

        try {
        $conn = new PDO("mysql:host=$servername;dbname=testing", $username, $password);
        // set the PDO error mode to Exception
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        echo "Connected successfully";
        } catch(PDOException $e) {
        echo "Connection failed: " . $e->getMessage();
        }
        $stmnt=$conn->query('select * from test01')
        $stmnt->rowcount()
        $stmnt->fetch(PDO::FETCH_ASSOC)
        $conn = null;

        ?>
        </body>
        </html>

        I'm getting this :

        Parse error: syntax error, unexpected '$stmnt' (T_VARIABLE) in C:\wamp64\www\index2.php on line 20
        http://steelehard.com/ الإباحية باللغة العربية
        Discord: https://discord.gg/QWJpmDSFc2
        http://le-beverley.com/ Under Development
        Twitter: https://twitter.com/steele_hard
        Spotify : https://open.spotify.com/show/43uaTA1AEPa2Nf0Tmvppd0

        Comment

        • sarettah
          still insane
          • Oct 2002
          • 14468

          #5
          Originally posted by hamiltonsteele
          T
          $stmnt=$conn->query('select * from test01')
          $stmnt->rowcount()
          $stmnt->fetch(PDO::FETCH_ASSOC)
          $conn = null;

          I'm getting this :

          Parse error: syntax error, unexpected '$stmnt' (T_VARIABLE) in C:\wamp64\www\index2.php on line 20
          You apparently did not actually read what I wrote. You used tehe statements but ignored the usage.

          The error is because you do not have semi-colons at the end of your statements but once you fix that the code still won't do shit.

          You need to use the rowcount() inside an if for it to make sense.

          You also need to attempt to understand what you are writing.

          .
          Free Huggles !!!!!!

          Comment

          • sarettah
            still insane
            • Oct 2002
            • 14468

            #6
            1. extablish the mysql cocnnection (this you are doinfg properly)$db = new

            2. Perfform an action on the database , again you did this properly but need to end the statement with the semi colon.

            $stmnt=$conn->(query('select * from test01');

            3. Check to see if you have results:
            4. If you have results, do something with those results such as iterate through and echo a field or something.

            if($stmnt->rowcount()>0)
            {
            while($row = $stmnt->fetch(PDO::FETCH_ASSOC))
            {
            do something.....;
            }

            Setting the connection to null really doesn't do anything for you imho. The connection will close at the end of the program.

            .
            Free Huggles !!!!!!

            Comment

            • hamiltonsteele
              Confirmed User
              • Jul 2003
              • 1041

              #7
              Originally posted by sarettah

              You also need to attempt to understand what you are writing.

              .
              I honestly appreciate the help and got it doing something that looks like it's working.
              So I think you managed to push me in the right direction.

              But... I've got sad news...
              I am attempting to understand what I'm writing.

              When everything is all said and done... Let me just say that there are good reasons
              why I stopped pursing an education and career in anything that couldn't be fixed with
              hooker or a hammer.
              http://steelehard.com/ الإباحية باللغة العربية
              Discord: https://discord.gg/QWJpmDSFc2
              http://le-beverley.com/ Under Development
              Twitter: https://twitter.com/steele_hard
              Spotify : https://open.spotify.com/show/43uaTA1AEPa2Nf0Tmvppd0

              Comment

              • Clown
                Confirmed User
                • Feb 2022
                • 38

                #8
                Always be sure to close our your PHP lines of code with ";" as sarettah said. You miss one little thing and PHP will always bitch at you.
                Cheap VPS/Shared Hosting: RackNerd
                Discord: Clown#5147
                Projects: JuicySluts
                Website: Clownonymous
                Other Domains: JuicyBate - HornyFeed - Pimp Ink - Fansily

                Skills: API/HTML/CSS/JS/PHP/PYTHON

                Comment

                • sarettah
                  still insane
                  • Oct 2002
                  • 14468

                  #9
                  Originally posted by hamiltonsteele
                  When everything is all said and done... Let me just say that there are good reasons
                  why I stopped pursing an education and career in anything that couldn't be fixed with
                  hooker or a hammer.
                  My apologies for sounding harsh. I was frustrated.

                  I try to remember that everybody learns things differently.

                  .
                  Free Huggles !!!!!!

                  Comment

                  • Klen
                    • Aug 2006
                    • 32234

                    #10
                    See this: https://www.w3schools.com/php/php_mysql_connect.asp
                    That is how i learned to use both mysqli and pdo connections.

                    Comment

                    • hamiltonsteele
                      Confirmed User
                      • Jul 2003
                      • 1041

                      #11
                      Originally posted by sarettah
                      My apologies for sounding harsh. I was frustrated.

                      I try to remember that everybody learns things differently.

                      .
                      Don't sweat it... I know that I'm kind of retarded when it comes to this kind of stuff.
                      http://steelehard.com/ الإباحية باللغة العربية
                      Discord: https://discord.gg/QWJpmDSFc2
                      http://le-beverley.com/ Under Development
                      Twitter: https://twitter.com/steele_hard
                      Spotify : https://open.spotify.com/show/43uaTA1AEPa2Nf0Tmvppd0

                      Comment

                      Working...