PHP database connection and simple operation

Source: Internet
Author: User
Tags php database

Chinese garbled solution: cause the encoding format is inconsistent
1. When creating a database, the character set selection Utf-8
2. Modify the configuration of MySQL: Add Character_set_server=utf8 below the [mysqld] Module
3. The encoding format of the modified page is Uft-8,header ("Content-type:text/html;charset=utf-8");

How the database is connected:

<Body><Table>    <TR>          <TD>Number</TD>          <TD>Name</TD>          <TD>Gender</TD>          <TD>Birthday</TD>          <TD>Position</TD>          <TD>Department of Learning</TD>    </TR><?php//Connection object: Build a MySQL object $db = new mysqli ("localhost", "root", "123", "test_01");//prepare an SQL statement $sql = "SELECT * FROM teacher  ";///Execute SQL statement, if it is a query statement, successfully return result set object; If not, execution fails to False$reslut = $db->query ($sql);//Determines whether the return execution succeeds if ($reslut) {                      while ($attr = $reslut->fetch_row ()) {echo "<tr> <td>{$attr [0]}</td>                      <td> {$attr [1]}</td> <td>{$attr [2]}</td> <td> {$attr [3]} </td> <td>{$attr [4]}</td> <td> {$attr [5]}</td> </tr> "}}?></Table></Body>                  

Instance:

Build a connection
$con = new Mysqli ("localhost", "root", "" "," Z-stu ");

Determine if there is an error

if (Mysqli_connect_error ()) {

echo "Connection Failed";

Exit ();

}

Write SQL statements

$sql = "SELECT * from Nation";

Execute SQL statement, return result set

$result = $con->query ($sql); The symbol indicates the meaning of the call, which is equivalent to a point in C #.

Reading data from the result set
while ($a = $result->fetch_row ())
{
Var_dump ($ATTR);
}

PHP simple operation based on database connection

Through the PHP connection database to implement additions and deletions to change the operation:

1. Add Data:

1. Create a form in the Web page, note the location of the submission, and the manner of submission

 echo "<form action =\" tinajia1.php\ "method=\" post\ ">

<input type=\ "text\" name=\ "code\"/>

<input type=\ "text\" name=\ "name\"/>

<input type=\ "submit\" value=\ "add \"/>

</form> ";

2, in the submitted page processing, first receive the form of data, and then inserted into the database

$code = $_post["code"];

$name = $_post["name"];

$con = @mysql_connect ("localhost", "root", "");

mysql_select_db ("Z-stu", $con);

$sql = "INSERT into nation values (' {$code} ', ' {$name} ')";

$result = mysql_query ($sql);

if ($result) {

Header ("location:chaxun1.php"); page Jump mode in//php

}else{

echo "Add failed";

}

2. Delete data:

1, through the hyperlink to submit the address of the Web page, plus the data, is the get way to transmit data

<a onclick=\ "return confirm (' OK to delete ') \" href=\ "delete1.php?code={$a [0]}\" ><input type=\ "button\" value=\ "delete \ "/></a>

Return is the Boolean value that returns confirm to whether the label "a" is executed

2. In the PHP page, process the return

$code = $_get["code"];

$con = @mysql_connect ("localhost", "root", "");

mysql_select_db ("Z-stu", $con);

$sql = "Delete from Nation where code = ' {$code} '";

$result = mysql_query ($sql);

if ($result) {

Header ("location:chaxun1.php");

}else{

echo "Add failed";

}

3. Modify the data

1. Hyperlink to a form

<a href=\ "xiugai.php?code={$a [0]}\" ><input type=\ "button\" value=\ "Modify \"/></a>

2, fill in the modified content submitted to PHP processing

Header ("Content-type:text/html;charset=utf-8");
$a = $_get["code"];
$con = @mysql_connect ("localhost", "root", "");

mysql_select_db ("Z-stu", $con);

$sql = "SELECT * from Nation where code = ' {$a} '";

$result = mysql_query ($sql);

$sttr = Mysql_fetch_row ($result);

echo "<form action =\" xiugai1.php\ "method=\" post\ ">
<input readonly=\ "readonly\" type=\ "text\" name=\ "code1\" value=\ "{$sttr [0]} \"/>
<input type=\ "text\" name=\ "name1\" value=\ "{$sttr [1]} \" />
<input type=\ "submit\" value=\ "Modify \"/>
</form> ";

/* Hide data, primary key, etc. cannot be changed
Style:visdispaly
Type:hidden
Readonly=\ "readonly\" */

3. In the PHP page, process the return

$b = $_post["Code1"];

$c = $_post["name1"];

$con = @mysql_connect ("localhost", "root", "");

mysql_select_db ("Z-stu", $con);

$sql = "Update Nation set name = ' {$c} '";


$result = mysql_query ($sql);

if ($result) {

Header ("location:chaxun1.php");

}else{

echo "Add failed";
}

PHP database connection and simple operation

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.