0

Been always used MySQL with PHP with the typical mysql_query or mysql_fetch_array but now i'm moving to PDO: Prepared Statement since it will be the future.

I'm newbie and using it for personal so i decide to ask you how to rewrite all this in rules of PDO: Prepared Statement

Let us suppose we've the following database users(id,name,job,number)

CREATE TABLE `users` (
  `id` bigint(20) unsigned NOT NULL auto_increment,
  `name` varchar(255) default '0',
  `job` varchar(255) default NULL,
  `number` varchar(255) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

INSERT INTO `users` VALUES (1, 'John', 'Plumber', '555');
INSERT INTO `users` VALUES (2, 'Iva', 'Reporter', '666');
INSERT INTO `users` VALUES (3, 'Robert', 'Writer', '777');
INSERT INTO `users` VALUES (4, 'Irin', 'Writer', '888');

Now those are of normal Mysql so can anyone rewrite to be with PDO: Prepared Statement

1) Connection

$DB["host"]   = "localhost";
$DB["dbName"] = "dbname";
$DB["user"]   = "dbuser";
$DB["pass"]   = "dbpass";
$link = mysql_connect($DB["host"],$DB["user"],$DB["pass"]) or die("Connection Failed");
mysql_select_db($DB["dbName"]);

2) Call from the database exact entry

$sql = "select * from users where job='Plumber'";
$reg = mysql_query($sql) or die(mysql_error());
$aee = mysql_fetch_array($reg);

echo $aee[name]; // should gives name John

3) Call from the database results

$qry="select * from users where job='Writer'";
$result=mysql_query($qry) or die($qry);

if(mysql_num_rows($result)=='0'){

echo "No results"; // if not found any for this conditional job

}else{
while($line=mysql_fetch_array($result)){

echo $line[name]."<br>"; // should gives Robert <br> Irin

}
}

That would really helps me a lot and discourage me to open next new question for insert,update,delete as i believe this helpful for newbies like me ~ Thanks

Reham Fahmy
  • 4,937
  • 15
  • 50
  • 71

1 Answers1

2

The PDO manual is an excellent resource for learning PDO for almost any situation. Prepared statements may seem bizarre and hard at first, but you will come to love them once you master some basics...

PDO SELECT...

$db = new PDO("mysql:host=localhost;dbname=db", "user", "password");

$query = "SELECT * FROM users WHERE name = " . $db->quote($name);

$result = $db->query($query);

while($row = $result->fetch(PDO::FETCH_ASSOC)) {
    print_r($row);
}

$result->closeCursor();

PDO INSERT...

$query = $db->prepare("INSERT INTO users (first_name, last_name, email) VALUES (:fname, :lname, :email)");

// bind params
$query->bindParam(":fname", $firstName);
$query->bindParam(":lname", $lastName);
$query->bindParam(":email", $email);

// execute the query
$query->execute();

In spite of the last (simple) example, I would also encourage you to learn how to do INSERT queries for multiple values in one query. It's not only faster than doing many single INSERTs, it's also considered good practice.

Community
  • 1
  • 1
Ian Atkin
  • 6,302
  • 2
  • 17
  • 24