Functions in PHP that manipulate MySQL database
PHP initially provides a set of connection operations MySQL function mysql_xxx ();
Later, an enhanced version of the function mysqli_xxx () was provided;
Connect MySQL server steps with PHP
1: Create a connection to the MySQL server
$conn = Mysqli_connect (url,uname,upwd,dbname);
URL MySQL server address ' 127.0.0.1 '
uname MySQL user name ' root '
upwd mysql password '
dbname Connect which library ' Dangdang '
2: Send SQL statement to MySQL server
$result = mysqli_query (data connection, SQL statement);
3: Read MySQL server return results
if ($result ===true) {
Successful execution
}else{
Execution failed
}
4: Disconnect from MySQL server---(can be omitted)
Mysqli_close ($conn);
Example: 8.php Add data
Dangdang/dd_category (Id,name,bcount);
Pit:
php-> database record garbled!
Cause: Php-->mysql Add data by default
Code latin_1
Workaround: Add a line after creating the connection
$conn = Mysqli_conn ....
Mysqli_query ($conn, "SET NAMES UTF8");
PHP sends SQL transcoding--LATIN_1-->UTF8
Instance code:
<?PHP$conn=Mysqli_connect(' 127.0.0.1 ', ' root ', ', ' Dangdang ') or die(' Server Connection failed! ‘); Mysqli_query($conn, ' Set names UTF8 '); $sql= ' SELECT * from Dd_book '; $re=Mysqli_query($conn,$sql); if($re){ foreach($re as $k=$v){ Var_dump($k,$v); } }Else{ Throw New Exception(' Data table query failed! ‘); }
PHP Operation MySQL