The logical operation of using PHP to access MySQL database and adding and deleting the instance operation

Source: Internet
Author: User

PHP Access MySQL Database

<?php
//Build Connection Object
$db = new Mysqli ("localhost", "root", "" "," 0710_test ");

//write SQL statements
$sql = "SELECT * from student";
//detects if the connection database is successful, fails to return "Connection failed" and exits the program
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
//Execute SQL statement, return result set object
$result = $db->query ($sql);
Var_dump ($result->num_rows);
//judgment result with or without data
if ($result->num_rows) {
echo "presence data";
}
//Fetch data (query statement)
$arr = $result->fetch_all ();//Get all the data and exist in a two-dimensional array
$arr = $result->fetch_all (MYSQLI_ASSOC);//get associative array in all data
$arr = $result->fetch_array ();

//while looping through an array of all data
while ($arr = $result->fetch_array ()) {
Var_dump ($arr);
}

$arr = $result->fetch_assoc ();//return associative array
$arr = $result->fetch_object ();//Column name corresponds to member variable
$arr = $result->fetch_row ();//returns an indexed array
Var_dump ($arr);

///delete and change statements
Add a piece of data (return value TRUE or FALSE)
$sql = "INSERT into student values (' 102 ', ' king ', ' Male ', ' 1987-7-1 ', ' 95033 ')";
Delete a piece of data (return value TRUE or FALSE)
$sql = "Delete from student where Sname= ' Li June '";
$r = $db->query ($sql);
Var_dump ($R);
?>

Additions and deletions of the self-growing list in the database

<?php
$db = new Mysqli ("localhost", "root", "" "," 0710_info ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql = "INSERT into work values (0, ' P005 ', ' 2005-01-01 ', ' 2010-10-1 ', ' shan Normal University ', ' Ministry of Education ', 2)";
if ($db->query ($sql)) {
echo "Add success";
//Insert ID of this data (common)
Echo $db->insert_id;
}else{
echo "Add failed";
}
?>

The logical operation of using PHP to access MySQL database and adding and deleting the instance operation

Related Article

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.