Php-based connection to mysql database and data query, php-based mysql database
There are three methods to connect to the database:
1. General method:
$ Con = mysql_connect ($ dbhostip, $ username, $ userpassword) or die ("Unable to connect to the MySQL! "); $ Db = mysql_select_db ($ dbdatabasename, $ con); // execute the statement $ qres = mysql_query (" SELECT id, GoodsName FROM user "); // extract a piece of data 11 $ row = mysql_fetch_row ($ result); // mysql_fetch_row can only extract the first record of the query result // extract multiple records $ reslist = array (); $ I = 0; while ($ row = mysql_fetch_row ($ res) {$ reslist [$ I] = $ row; $ I ++;} mysql_close ($ con );
// The extracted result of mysql_fetch_row is that there is no field name in the query (that is, there is no key id, GoodsName, only value), such:
// The extracted result of mysql_fetch_assoc has a key value, for example:
// The extracted result of mysql_fetch_array has a key value, which is a combination of the preceding two types, for example:
Use @ (error control operator) before functions such as mysql_connect () and mysql_select_db () to ignore system errors. Then we use die () to customize error messages;
For the return value of the mysql_query () function, if the executed statement has a return value (such as SELECT, SHOW, DESCRIBE, etc.), the corresponding data (when successful) or FALSE (when failed) is returned ); if the executed statement does not return values (such as DELETE, DROP, INSERT, and UPDATE), TRUE (when successful) or FALSE (when failed) is returned ).
2. Object-oriented form
$db=new mysqli($dbhostip,$username,$userpassword,$dbdatabasename);if(mysqli_connect_error()){ echo 'Could not connect to database.'; exit;}$result=$db->query("SELECT id,GoodsName FROM user");$row=$result->fetch_row();
Mysqli is used here, which means mysql extension. It can be used to interact with the database through process-oriented or object-oriented methods.
3. PDO Method
PDO is actually the abbreviation of PHP Database Objects. Chinese is the PHP Database object. It provides a unified PHP-database interaction method.
Its advantage is that, as long as the data source is provided correctly, the basic operations on the database are the same. That is to say, the same code can interact with MySQL, SQLite3, or PostgreSQL, provided that you provide the correct data source.
Code for connecting to MySQL:
$dsn='mysql:host='.$dbhost.';dbname='.$dbdatabase.';' $dbh=new PDO($dsn,$username,$userpass);
SQLite3:
$dsn='sqlite3:"D:\sqlite\user.db"';$dbh=new PDO($dsn);PostgreSQL:$dsn='pgsql:host='.$dbhost.' port=5432 dbname='.$dbdatabase.' user='.$username.' password='.$userpass;$dbh=new PDO($dsn);
Operation:
$stmt=$dbh->query('SELECT id,name FROM user');$row=$stmt->fetch();
PHP and MYSQL database connection display query content
<?
$ Conn = @ mysql_connect ("localhost", "root", "root") or die ("Database Link error ");
Mysql_select_db ("news", $ conn );
Mysql_query ("set names 'gbk'"); // uses GBK Chinese encoding; $ SQL = "select info from message ";
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )){
?>
Echo $ row [title];
?>
The mysql database is successfully connected, but the results cannot be found in the PHP form. The database has a user table with two records.
Echo ('database connection failed. Please check your database system, and the username and password for the connection .');
Echo does not seem to require parentheses