<?php
/**
* Created by Phpstorm.
* User: Cheng
* DATE:2017/7/24
* TIME:10:25
*/
///**
* Object-oriented approach
// */
$mysqli = Mysqli_init ();
if (! $mysqli) {
Die (' Mysqli_init failed ');
//}
//
if (! $mysqli->options (Mysqli_init_command, ' SET autocommit = 0 ')) {
Die (' Setting Mysqli_init_command failed ');
//}
//
if (! $mysqli->options (Mysqli_opt_connect_timeout, 5)) {
Die (' Setting mysqli_opt_connect_timeout failed ');
//}
//
if (! $mysqli->real_connect (' localhost ', ' root ', ', ' School ')) {
Die (' Connect Error ' ('. Mysqli_connect_errno (). ‘) ‘
// . Mysqli_connect_error ());
//}
//
if ($mysqli->connect_errno) {
printf ("Connect failed:%s\n", $mysqli->connect_error);
Exit ();
//}
//
* Create table doesn ' t return a resultset */
if ($mysqli->query ("CREATE temporary TABLE myuser like user") = = = = TRUE) {
printf ("Table MyUser successfully created.\n");
//}
//
* Select queries return a resultset */
if ($result = $mysqli->query ("Select UserName from User LIMIT 10") {
printf ("Select returned%d rows.\n", $result->num_rows);
//
/* FREE Result set */
$result->close ();
//}
//
* If we have to retrieve large amount of the data we use Mysqli_use_result */
if ($result = $mysqli->query ("SELECT * from User", Mysqli_use_result)) {
//
/* Note, that we can ' t execute any functions which interact with the
Server until result set was closed. All calls would return an
' Out of sync ' ERROR */
if (! $mysqli->query ("SET @a:= ' This won't work '")) {
printf ("Error:%s\n", $mysqli->error);
//// }
$result->close ();
//}
Echo ' Success ... ‘ . $mysqli->host_info. "\ n";
//
$mysqli->close ();
$link =mysqli_real_connect (' 127.0.0.1:3306 ', ' root ', ') or exit (' Connection failed ... ');
mysqli_select_db (' pg39 ', $link);
$infos = Mysqli_query (' SELECT * from student ', $link);
//
$arr =array ();
if ($infos) {
while ($info = Mysqli_fetch_array ($infos, Mysql_both)) {
Array_push ($arr, $info);
// }
//}
//
Mysqli_close ($link);
/**
* mysqli Library for connecting database
* Using a procedural style code
*/
Get a connection to a database
$link =mysqli_init ();
Determine if the Mysqli object was created successfully
if (! $link) {
Die (' Mysqli_init creation failed ... ');
}
Sets the parameter value of the MYSQLI, which is the setting whether to submit the SQL statement automatically
if (!mysqli_options ($link, Mysqli_init_command, ' SET autocommit = 0 ')) {
Die (' Set auto commit failed ... ');
}
Set the actual time for the connection to 5 seconds
if (!mysqli_options ($link, mysqli_opt_connect_timeout,5)) {
Die (' Set the connection time failed ... ');
}
Creating a MYSQLI Connection database
if (!mysqli_real_connect ($link, ' localhost ', ' root ', ' ', ' School ')) {
Die (' Connection error ('. Mysqli_connect_errno (). ') ');
Mysqli_connect_error ();
}
Define SQL statements and send to database side to perform operations and return results
if (!mysqli_set_charset ($link, ' UTF8 ')) {
Die (' Set database query encoding set failed ... ');
}
$infos =mysqli_query ($link, ' select * from user ', Mysqli_use_result);
Handling returned results
$arr =array ();
if ($infos) {
while ($info = Mysqli_fetch_array ($infos, mysqli_num)) {
Array_push ($arr, $info);
}
Processing end gets the data collection to the purge
Mysqli_free_result ($infos);
}
Var_dump ($arr);
Close an open resource
Mysqli_close ($link);
Create a connection to a database (different methods)