PHP Link Database 1

Source: Internet
Author: User

The data taken from the results is taken out as an array.

1.PHP querying a piece of information in a database

PHP link Database/*1. Link object IP address  user name  password  database name--*/$db =new mysqli ("localhost", "root", "123", "Zhousan");// Determine if the link is wrong Mysqli_connect_error ()? Die ("Link Failed"): "Link succeeded";//three-step operator//Prepare SQL statement $sql= "Select*from fruit";//4 Execute SQL statement return result set object $ result= $db->query ($sql);//Remove All data $rr= $result->fetch_all ();//return all data and exist in the form of a two-dimensional array var_dump ($RR);// You can also iterate through the collection (nested) or loop through the contents of the  data

2. Adding information to the database via PHP

$obj =new mysqli ("localhost", "root", "123", "Zhousan"); Mysqli_connect_error () Die ("error"): print "Success"; $tt = "INSERT INTO Fruit values (' w ', ' f ', 2.1, ' SD ', 2, ' SD ') "//insert statement  if the check error can be copied to the Mysqle server to see if it runs $rt= $obj->query ($TT); var_ Dump ($RT);//returns BOOL value  true or false//$yy = $rt->fetch_all (); This statement cannot be executed will be error   

3. Fetching data from the result set

$attr = $result->fetch_all (); All data is returned, and there are three optional arguments in the form of a two-dimensional array: Mysqli_assoc returns an associative array, Mysqli_num returns an indexed array, Mysqli_both returns an index and association, and does not need to double-quote $attr = $ when used Result->fetch_array ();//Returns the current data, returns a one-dimensional array, by default the index association has $attr = $result->fetch_assoc (); Returns the current data, returning a one-dimensional associative array $attr = $result->fetch_object (); Returns the object of the current data $attr = $result->fetch_row (); Returns the current data, returning an indexed array

4. Iterate through the collection to fetch data (note the data in while)

$connect 1=new mysqli ("localhost", "root", "123", "Zhousan"); $langu = "Select*from fruit"; $gg = $connect 1->query ($ Langu), while ($attr = $gg->fetch_row ()) {echo] <div>{$attr [0]}--{$attr [1]}--{$attr [2]}--{$attr [3]}--{$attr [4]} </div> ";}

  

  

PHP Link Database 1

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.