0

I am starting to learn php PDO because I've read that it is more efficient and secure. I could do the following with simple mysqli but am having trouble making it work with PDO.

PID stands for an id number. fname stands for: first name. lname stands for: last name. age stands for ... age.

Basically I have an index.php that contains links from a test table called "persons" inside of the database drinks. When I click on the link which shows the fname of every row, it goes to insertcarbonated.php which is then supposed to $_GET['fname']; of the link and search up that specific row. However, my code in insertcarbonated.php is not working and I am not familiar enough with PDO to know exactly why, I would like some enlightenment on this because I literally begun learning PDO yesterday. :(

Here is my insertcarbonated.php:

<html>
<?php 
/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'theusername';

/*** mysql ***/
$password = 'thepass';

try {
     $dbh = new PDO("mysql:host=$hostname;dbname=drinks", $username, $password);
    /*** echo a message saying we have connected ***/
    echo 'Connected to database';
    /*** The SQL SELECT statement ***/

    $fname = $_GET['fname'];
      //is _GET even working with PDO? 

    $STH = $dbh-> prepare( "SELECT * FROM persons WHERE fname LIKE '$fname'" );
     /***as Joachim suggested, I had actually two different variables here, however, it     
       did not solve the issue **EDITED** from ($DBH to $dbh)****/

    $STH -> execute();
    $result = $STH -> fetch(0);
     //$result should print out the first column correct? which is the person's ID. 

   }
catch(PDOException $e)
   {
   echo $e->getMessage();
   }



?>
<head>



</head>
<body>
 <p><?php print $result; ?></p>
 //me trying to print out person's ID number here. 

</body>



</html>

As previously mentioned, I'm not sure where my error is, I get fatal error: Call to a member function prepare() on a non-object?

and If I try to not use that function, my page is simply blank and nothing prints out. Basically I would just like to print out different bits of information from that row (that is from it's relevant link in index.php). I would like to know how to solve this using PDO.

Here is the previous question I asked, and it was solved but not with PDO. Previous question

Community
  • 1
  • 1
Bryan Fajardo
  • 161
  • 3
  • 15

1 Answers1

1

You could do something like this...

    try {
        $dbh = new PDO("mysql:host=$hostname;dbname=drinks", $username, $password);

        $fname = $_GET['fname'];

        $sth = $dbh->prepare("SELECT * FROM persons WHERE fname LIKE ?");
        $sth->execute( array($fname) );
        $result = $sth->fetch(PDO::FETCH_OBJ); // or try PDO::FETCH_ASSOC for an associative array
   }
   catch(PDOException $e)
   {
       die( $e->getMessage() );
   }

In the HTML part you can do print_r($result) and you will see the exact structure of your results.

Comments: one of the best reasons to use PDO is the automatic escaping of the dynamic user inputs, like $fname here, so you should use it. Also, with $sth->fetch($param) the $param is not the column number but the type of the fetch method PDO will use (see PHP manual). Depending the method, you can get the PID of the result by $result->PID in case of PDO::FETCH_OBJ or by $result['PID'] when using PDO::FETCH_ASSOC. I hope this helps.

Barnabas Kecskes
  • 1,861
  • 17
  • 24
  • 1
    That query may return multiple rows so you'd want to use `fetch()` in a loop or use `fetchAll()` – Phil Jul 07 '14 at 22:54