Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDO method for mysql_fetch_assoc()?

I used to do :

$resource = mysql_query("SELECT * FROM table WHERE id = " . $id);
$user = mysql_fetch_assoc($resource);
echo "Hello User, your number is" . $user['number'];

I read that mysql statements are all deprecated and should not be used.

How can i do this with PDO?

The first line would be :

$stmt = $db->prepare("SELECT * FROM table WHERE id = " . $id); // there was an aditional double quote in here.
$stmt->execute(array(':id' => $id));

What about the mysql_fetch_assoc() function?

I am using php

like image 961
donparalias Avatar asked May 03 '13 16:05

donparalias


People also ask

What will mysql_fetch_assoc () return?

Description ¶ Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. mysql_fetch_assoc() is equivalent to calling mysql_fetch_array() with MYSQL_ASSOC for the optional second parameter. It only returns an associative array.

What is PDO :: Fetch_obj?

PDO::FETCH_OBJ : returns an anonymous object with property names that correspond to the column names.

What is Mysqli_fetch_assoc () function?

The mysqli_fetch_assoc() function is used to return an associative array representing the next row in the result set for the result represented by the result parameter, where each key in the array represents the name of one of the result set's columns.

What is PDO :: Fetch_assoc?

PDO::FETCH_ASSOC. Specifies an array indexed by column name. PDO::FETCH_BOTH. Specifies an array indexed by column name and 0-based order. This is the default.


1 Answers

You can use (PDO::FETCH_ASSOC) constant
Usage will be while ($res = $stmt->fetch(PDO::FETCH_ASSOC)){ .... }
Here's the reference (documentation precisely) : http://www.php.net/manual/en/pdostatement.fetch.php

like image 77
Willy Pt Avatar answered Oct 20 '22 20:10

Willy Pt