Getting Started with PHP database operations basics

Source: Internet
Author: User
Tags getting started with php php database

This article is mainly to share with you a basic introduction to PHP database operations, hoping to help everyone.

Get form data

_post["] Gets the data of the POST submission, and

_request gets a data structure that resembles a map key-value pair.

1 Querying data

Database Information $url = "192.168.199.146";    $url = "localhost";    $username = "root";    $password = "123123";    $db _name = "Test";    Get Data $conn = new Mysqli ($url, $username, $password, $db _name);    if ($conn->connect_error) {die ("DB connection Error");    } $start _rows = ($page-1) * $rows; $sql = "SELECT * from User LIMIT". $start _rows. "," .    $rows;    $result = $conn->query ($sql);    $jarr = Array (); while ($rows =mysqli_fetch_array ($result, Mysql_assoc)) {$count =count ($rows);//cannot be in a looping statement because each deletion of the row array length decreases the for (    $i =0; $i < $count; $i + +) {unset ($rows [$i]);//delete redundant data} array_push ($jarr, $rows); } $jobj =new Stdclass ();//Instantiate Stdclass, which is an empty class built into PHP that can be used to pass data, since the Json_encode data is stored as an array of objects,//$jobj->total=$    result->num_rows;//Total//Get total $get _all_user_sql= "SELECT COUNT (*) as totals from user";    $result _total = $conn->query ($get _all_user_sql); $rows _total= $result _total->fetch_assoc () ["Total"];   $jobj->total = $rows _total;    $jobj->rows= $jarr;    So when we generate it we also store the data in the object/*foreach ($jarr as $key + $value) {$jobj $key = $value; }*/Echo Json_encode ($jobj);

2 Adding data

    $url = "192.168.199.146";    $url = "localhost";    $username = "root";    $password = "123123";    $db _name = "Test";    Connect database    $conn = new Mysqli ($url, $username, $password, $db _name);    if ($conn->connect_error) {die        ("DB connection Error");    }    Save    The data $insert _sql= "Insert User (Name,password)";    $insert _sql= $insert _sql. " VALUES (' ". $param [' name ']." ', ' ". $param [' Password ']." ') ";        echo $insert _sql;    $result = $conn->query ($insert _sql);    if ($result ===true) {        echo "new record inserted successfully";    } else{        echo "Insert new record Failed";    }

Reference:
Query result output to JSON format: https://www.cnblogs.com/yiven/p/6491019.html
Get data in bulk and deposit data: http://bbs.blueidea.com/thread-3056406-1-1.html

Get form data

_post["] Gets the data of the POST submission, and

_request gets a data structure that resembles a map key-value pair.

1 Querying data

Database Information $url = "192.168.199.146";    $url = "localhost";    $username = "root";    $password = "123123";    $db _name = "Test";    Get Data $conn = new Mysqli ($url, $username, $password, $db _name);    if ($conn->connect_error) {die ("DB connection Error");    } $start _rows = ($page-1) * $rows; $sql = "SELECT * from User LIMIT". $start _rows. "," .    $rows;    $result = $conn->query ($sql);    $jarr = Array (); while ($rows =mysqli_fetch_array ($result, Mysql_assoc)) {$count =count ($rows);//cannot be in a looping statement because each deletion of the row array length decreases the for (    $i =0; $i < $count; $i + +) {unset ($rows [$i]);//delete redundant data} array_push ($jarr, $rows); } $jobj =new Stdclass ();//Instantiate Stdclass, which is an empty class built into PHP that can be used to pass data, since the Json_encode data is stored as an array of objects,//$jobj->total=$    result->num_rows;//Total//Get total $get _all_user_sql= "SELECT COUNT (*) as totals from user";    $result _total = $conn->query ($get _all_user_sql); $rows _total= $result _total->fetch_assoc () ["Total"];   $jobj->total = $rows _total;    $jobj->rows= $jarr;    So when we generate it we also store the data in the object/*foreach ($jarr as $key + $value) {$jobj $key = $value; }*/Echo Json_encode ($jobj);

2 Adding data

    $url = "192.168.199.146";    $url = "localhost";    $username = "root";    $password = "123123";    $db _name = "Test";    Connect database    $conn = new Mysqli ($url, $username, $password, $db _name);    if ($conn->connect_error) {die        ("DB connection Error");    }    Save    The data $insert _sql= "Insert User (Name,password)";    $insert _sql= $insert _sql. " VALUES (' ". $param [' name ']." ', ' ". $param [' Password ']." ') ";        echo $insert _sql;    $result = $conn->query ($insert _sql);    if ($result ===true) {        echo "new record inserted successfully";    } else{        echo "Insert new record Failed";    }

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.