The PDO connection database for PHP is important in data manipulation and will be explained in detail in this article.
The PDO connection database for PHP is a data object for an object-oriented database
The reason I chose PDO to connect to the database is because PDO is suitable for any database
and mysqli, only suitable for MySQL, no good, which is bad, which used to use which is handy.
First of all, of course we have to connect to the database. Then the PDO connection database has a specific statement that is:
$link = new PDO ("mysql:host= $servername;d bname=mydb", $username, $password); usually $servername,mydb, $username, $ Password are variables, in order to be secure, we can define (define) them define ("Db_host", "localhost") in the conf.php file,;d efine ("Db_user", "root");d efine (" Db_pwd "," 123456 ");d efine (" Db_dbname "," blog ");d efine (" Db_charset "," UTF8 ");
Then use them:
$link = new PDO ("mysql:host=". Db_host. "; Dbname= ". Db_dbname. "", Db_user, db_pwd);
Usually we have to connect to the database again and again, so we usually return to the $link, the result, each use call Connect ()
function Connect () { try { $link = new PDO ("mysql:host="). Db_host. "; Dbname= ". Db_dbname. "", Db_user, db_pwd); Sets the PDO error mode for throwing exceptions $link->setattribute (Pdo::attr_errmode, pdo::errmode_exception); } catch ( Pdoexception $e) { echo "database connection Failed". $e->getmessage (); } return $link;}
In this way, the Connect () method is complete.
Then we want to insert (insert) The data, then look down
Think first, what do you need to insert data?
Insert INTO {$table} ($key) values {$values};
This is an SQL statement, and also the SQL statement that I will use.
I use variables to represent what we need for our SQL statement, $table, $key, $values
$table as if we are easier to find, to get a variable, but $key and $values? Can you get it at the same time?
Then we can use the $array:
Array_key () and Array_values () can both acquire $key and $values
So that's easy, we're just going to need two variables $table and $array.
Therefore, we should add two variables to our insert ($table, $array) method.
Then we have to consider the data splicing problem, then I will not elaborate, because one can understand
Directly on the code:
function Insert ($table, $array) { try { $conn = connect (); $conn->setattribute (Pdo::attr_errmode, pdo::errmode_exception); $key = Join (",", Array_keys ($array)); $values = "'". Join ("', '", Array_values ($array)). "'"; $sql = "INSERT INTO {$table} ($key) values {$values}"; $result = $conn->exec ($sql); return $conn->lastinsertid (); } catch (Pdoexception $e) { echo "Insert Failed". $e->getmessage (); } }
OK, the code word a bit more feeling, so today first, then there is update (), delete () and other methods to share with you
Although these are old things, but I would like to write a solid upward should be written
This article explains the contents of PHP's PDO connection database, and more about it, please follow the PHP Chinese web.