59th Day class PHP front-end and database connection

Source: Internet
Author: User

Connect to a database (traditional method)

mysql_connect("Lcoalhost", "Root", "" ");//connect the native database, connect the external machine localhost can be changed to make IP addressmysql_select_db("Txst",$db);//Select the database to manipulate$sql= "SELECT * from Info";//writing MySQL statements$result=mysql_query($sql);//Execute MySQL statementVar_dump(Mysql_fetch_row($result));//each time it is executed, a row of data is returned, which can be read by loop

Connect to the database (current method)

$db=NewMysqli ("localhost", "root", "" "," Test ");//connect to the native database and assign a value to the specific library to $db!Mysqli_connect_error() or die("Connection Failed");//detects if the connection database is successful, fails to return "Connection failed", and exits the programif(Mysqli_connect_error())//detects if the connection database was successful and returns false successfully{    Exit();//Exit Program}$sql= "SELECT * from Info";//Writing SQL statements$result=$db->query ($sql);//Execute SQL statement, return result set object successfully, fail return falseif($result)//determine if the execution succeeds{    Var_dump($row=$result->fetch_row ());//returns an array of indexes, one row for each execution, plus a loop to read more data    Var_dump($result->FETCH_ASSOC ());//returns an associative array that returns one row per execution    Var_dump($result->fetch_object ());//returns an object that is returned once per execution    Var_dump($result->fetch_all ());//returns an indexed two-dimensional array, reading all information}

Jobs: Connect to a database query and print a table

$db=NewMysqli ("localhost", "root", "" "," index ");!Mysqli_connect_error() or die("Connection Failed");$sql= "SELECT * FROM Info";$result=$db->query ($sql);if($result){    $a=$result-Fetch_all (); Echo"<br><br>"; Echo"<table border= ' 1px ' width= ' 1000px ' align= ' center ' >"; Echo"<tr align= ' center ' ><td>Code</td><td>Name</td><td>Sex</td><td> Nation</td><td>birthday</td></tr> "; foreach($a  as $i)    {        Echo"<tr align= ' center ' ><td>$i[0]</td><td>$i[1]</td><td>$i[2]</td><td>$i[3]</td><td>$i[4]</td></tr> "; }    Echo"</table>";}

Header (location: address); Jump page

59th Day class PHP front-end and database connection

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.