mysql_query to PDO

13,234

Solution 1

You have to execute the query

$query = $db->prepare('SELECT defe FROM information WHERE term = 1');
$query->execute();
$fetch = $query->fetch();
print_r($fetch);

You can also use PDO::query since you aren't using any parameters in your query

$query = $db->query('SELECT defe FROM information WHERE term = 1');
$fetch = $query->fetch();
print_r($fetch);

Solution 2

You can simply do

$query = $db->query('SELECT defe FROM information WHERE term = 1');  
$result = $query->fetch();  
print_r($result);  

But if you want to prepare it (may be for security reasons) , you need to execute it.

So it would be

$query = $db->prepare('SELECT defe FROM information WHERE term = 1');  
$query->execute();  
$result = $query->fetch();  
print_r($result);   
Share:
13,234
syrkull
Author by

syrkull

A freelancer who deal with PHP, HTML, CSS and tries to design creative Arabic websites.

Updated on November 23, 2022

Comments

  • syrkull
    syrkull over 1 year

    I have the following code in the old "mysql_query"

    $query = mysql_query("SELECT defe FROM information WHERE term = 1");
    $fetch = $db->fetch_array($query);
    print_r($fetch);
    

    I want to transform this to the PDO way of retrieving information from database. I tried the following (did not work- does not display any result) :

    $query = $db->prepare('SELECT defe FROM information WHERE term = 1');
    $fetch = $query->fetch();
    print_r($fetch);
    

    the connection to the database is established and it stored in the $db variable (only the PDO).

    • PeeHaa
      PeeHaa about 11 years
      Please don´t just switch by guessing, but read up on what you are doing.
    • syrkull
      syrkull about 11 years
      @PeeHaa I was thinking that prepare() is something necessary in PDO. that is why I did not delete it.
    • PeeHaa
      PeeHaa about 11 years
      That's the guessing I was talking about ;-)
  • mpen
    mpen about 11 years
    or use $db->query() instead. php.net/manual/en/pdo.query.php (I'm not advocating unescaped values, but he isn't using any variables here)
  • syrkull
    syrkull about 11 years
    I am a beginner in this PDO thingy.. I do not know what prepare does .. I copied the code from somewhere else and started working on it :( That is how I learn lol
  • adi rohan
    adi rohan about 11 years
    then you better learn about prepare ....php.net/manual/en/pdo.prepare.php .... because they are very useful
  • syrkull
    syrkull about 11 years
    @Mark and Musa Thanks.. I appreciate your help.
  • mpen
    mpen about 11 years
    @shnisaka: "prepare" essentially sends the the query to the MySQL server which then compiles it (prepares it for execution). "execute" is what actually causes it to run and fetch the data and return it to your PHP script. Furthermore, you can put variables in your prepared query (using ? or :myvar) which then you can then substitute by passing an [associative] array to the execute method. This is the preferred way of doing things as it's both safe, and fast (particularly when running the same query multiple times).