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