Novice PHP Connection database probably. Simple process analysis and the problems encountered

Source: Internet
Author: User

Focus: PHP running on the server please remember!!!

1. Before connecting to the database and PHP, first check the php.ini configuration file in your PHP folder for

; Extension=php_mysqli.dll

Some words change to extension=php_mysqli.dll if they are, they don't have to change.

Extension=php_mysql.dll As for this existence can be a few years ago this was Extension=php_mysqli.dll to replace the use of the enhanced version of the mysqli.

If you are using Apache or XAMPP, go inside to find a PHP folder, the other, finally in the execution of the time remember to open the database!!!

Get to the point: the first step:

Create a connection to the MySQL database
* Mysqli_connect (Host,username,passwd,dbname,port)
* IP address of the server where the Host-mysql database resides
* Username-username of login MySQL database
* passwd-Password to log in to MySQL database
* dbname-Specify the name of the login database
* Port number of the Port-mysql database
*
* Return value of the method-database Connection object

$conn = Mysqli_connect (' 127.0.0.1 ', ' root ', ' 123456 ', ' Testa ', ' 3306 ');

That's what I'm writing. 127.0.0.1 is actually the server IP address on behalf of the default itself can also be written in the same way as localhost, but on Linux, there may be errors. To modify some configuration to write localhost

User name of root own database

123465 This is my password. You can write your own first login database without a password this can be empty, but keep the location.

Testa This is my database name. After MYSQL logs in, we can create many databases each database has its own name you create.

3306 This is my database port number This port number can be found and modified in the profile Xxx.ini of its own database folder, which is generally used by default to 3306.

Step Two:

Create a table in your own database what type of how many fields such as self-grasp

Then define the SQL statement-string type

$sql = "INSERT into MyUser VALUES (' 1 ', ' JANE ', ' 12345 ')";

This is where I'm going to myuser. Insert a data into the table I created. I have only three IDs in my form. NAME pwd

Step Three:

Send SQL statements to MySQL database
* Mysqli_query (Link,query)
* *link-The Connection object representing the MySQL database
* *query-Represents the sent SQL statement
*

$bool Mysqli_query ($conn,$sql);     // Var_dump ($bool);

You can print your own var_dump ($bool) after execution; Verify

The return value of the method
* If execution succeeds
* insert| update| Delete-true
* Select-mysqli_result Object
* If execution fails-false

The second step I'm using is to insert a piece of data. If this is a SELECT query statement, the Mysqli_query () method is used to return the Mysqli_result result set object
* We can parse the Mysqli_result result set object

One final step:

Mysql_close ("$conn");

Close the connection to the MySQL database
Mysqli_close (Connection object)

That's probably the code.

<? PHP $conn = mysqli_connet (' 127.0.0.1 ', ' root ', ' 123456 ', ' Testa ', ' 3306 ');$sql = "INSERT into MyUser vaules (' 1 ', ' JANE ', ' 12345 ') ";$resultmysqli_query($conn,$sql ); mysql_close($conn);?>

If an error occurs, check your configuration or punctuation marks in English

These are the process-connected databases.

Looking at the object-oriented connection database

<?PHP//connect MySQL with object-oriented style    /** 1. Create mysqli or MySQL object * $mysqli = new mysqli (host,username,passwd,dbname,port); * Equivalent to establishing a connection to the MySQL database*/    $mysqli=NewMysqli (' 127.0.0.1 ', ' root ', ' 123456 ', ' Testa ', ' 3306 ');/** 2. Define SQL statements * The encoding format by default is iso8859-1 * * How to solve Chinese garbled problem * mysqli_query ("SET NAMES UTF8"); */        $sql= "INSERT into MyUser VALUES (NULL, ' JANE ', ' 12345 '," "[email protected] ', ' Beijing ')"; //$mysqli->query ("SET NAMES UTF8");/** 3. Call the query () method of the Mysqli object * $mysqli->query ($sql) * * This method returns the execution result*/    $result=$mysqli->query ($sql);$mysqli-close ();?>

Novice PHP Connection database probably. Simple process analysis and the problems encountered

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.