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