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 address mysql_select_db ("Txst", $db); Select the database to operate $sql= "SELECT * from info"; Write MySQL statement $result=mysql_query ($sql); Executes the MySQL statement var_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 =new mysqli ("localhost", "root", "" "," Test "); Connect to the local 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 program if (Mysqli_connect_error ()) //detects if the connection database is successful, returns false{ exit successfully (); Exit program} $sql = "SELECT * from info"; Write SQL statements $result= $db->query ($sql); Executes the SQL statement, returns the result set object successfully, fails back Falseif ($result) //Determines whether 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 Var_dump ($result->fetch_object ()) per execution; Returns an object that returns Var_dump ($result->fetch_all ()) once per execution; Returns an indexed two-dimensional array, reading all information }
PHP front-end and database connections