Create a connection to a database (different methods)

Source: Internet
Author: User

<?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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.