Simple MySQL operation via PHP

Source: Internet
Author: User

In addition to operating MySQL in the terminal, you can also operate MySQL in PHP files, the steps are as follows:

1. Linked database: Mysqli_connect (Host,user[,password]);

<1>host generally write localhost or 127.0.0.1

<2>user is root

<3> have a password to write, no password will not write

$connect = Mysqli_connect ("127.0.0.1", "root", "chencuixin644152");
Tip: Usually add or die at the end ("Database not connected successfully");
$connect = Mysqli_connect ("127.0.0.1", "root", "chencuixin644152") or Die ("Database not connected successfully");

2. Select database: Mysqli_connect (link,dbname);

<1>link refers to a connected database;

<2>dbname refers to the name of the table you want to manipulate;

mysqli_select_db ($connect, "class") or Die (Mysqli_error ($connect));
Tip: Usually add or die at the end (Mysqli_error ($connect)), the error message displayed on MySQL is displayed on the PHP page;

3. Set the code: mysqli_select_db () or mysqli_query ();

Mysqli_set_charset ($connect, "UTF8") or Die (Mysqli_error ($connect));

Or it can be written as follows:
Mysqli_query ($connect, "Set names UTF8") or Die (Mysqli_error ($connect));

4. Statements to be executed in the database

$INSERTSQL = "INSERT into staff (id,name,sex,age,post,salary,position) VALUES (8, ' small just ', ' Male ', 7, ' personnel ', 3000, ' personnel department ')";

Tip: $INSERTSQL is a string type;

5. Insert the statement into the database: Mysqli_query ();

$result = Mysqli_query ($coon, $insertSql)  or Die (Mysqli_error ($connect));
A query that is successful for non- query returns TRUE. If it fails, it returns FALSE.
if ($result = = True) {
    echo "Data insertion success";
}else{
echo "Data insertion failed";
}
SELECT, SHOW, DESCRIBE, or EXPLAIN query, which returns a Mysqli_result object.
$resultSet = Mysqli_query ($connect, $SELECTSQL);//Returns an object

6. From the database value to PHP: Mysqli_fetch_assoc () or mysqli_fetch_object () or mysqli_fetch_row ();

<1>MYSQLI_FETCH_ASSOC (): Returns an indexed array;

<2>mysqli_fetch_obeject (): Returns an object;

<3>mysqli_fetch_row (): Returns an indexed array.

echo "<table border= ' 1 ' cellpadding= ' >";
while ($result =mysqli_fetch_assoc ($resultSet)) {
echo "<tr>";
echo "<td>{$result [' id ']}</td><td>{$result [' name ']}</td><td>{$result [' Sex ']}</ td><td>{$result [' Age ']}</td><td>{$result [' Post ']}</td><td>{$result [' Salary ']} </td><td>{$result [' position ']}</td> ';
echo "</tr>";
}
echo "</table>";


Tip: This method is similar to each, calling the method once, and the pointer is offset once.

Simple MySQL operation via PHP

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.