Method One: Using the built-in functions in PHP (for versions prior to 5.1)
//1. Build the connection $conn=mysql_connect("localhost", "root", "" "); //2. Select the Operational database mysql_select_db("Today",$conn); //3. Write SQL statements $sql= "SELECT * FROM Info"; //4. Execute MySQL statement $result=mysql_query($sql); //5. Extracting Data $array=Mysql_fetch_row($result); $array=Mysql_fetch_row($result); Print_r($array);
Results:
//1. Build the connection $conn=mysql_connect("localhost", "root", "" "); //2. Select the Operational database mysql_select_db("Today",$conn); //3. Write SQL statements $sql= "INSERT into Info values (' p100 ', ' Zhang San ', false, ' n001 ', ' 1998-3-4 ')"; //4. Execute MySQL statement $result=mysql_query($sql); //5. Extracting Data Echo Var_dump($result);
Results:
Mode two: Using Classes and objects (for versions after 5.1)
//1. Making Connection Objects $db=NewMysqli ("localhost", "root", "" "," Today "); //2. Determine if the connection is successful if(Mysqli_connect_error()) { Echo"Connection Failed"; Exit;//exit the entire program } Else { //3. Write SQL statements $sql= "SELECT * FROM Info"; //4. Execute SQL statements $result=$db->query ($sql); //5. Processing data (extracting data) Echo"<table width= ' 100% ' cellpadding= ' 0 ' cellspacing= ' 0 ' border= ' 1 ' >"; Echo"<tr bgcolor= ' #660099 ' ><td> code </td><td> name </td><td> Gender </td><td> National </td><td> Birthdays </td><tr> "; //traverse each piece of data while($row=$result-Fetch_row ()) { //dealing with gender $sex= (BOOL)$row[2]? " Male ":" Female "; //dealing with ethnic $nation= Nationname ($db,$row[3]); //Processing Birthdays $birthday=Date("Y year M D Day",Strtotime($row[4]));//required parameter is a timestamp, need to turn, turn into timestamp Echo"<tr><td>{$row[0]} </td><td>{$row[1]} </td><td>{$sex}</td><td>{$nation}</td><td>{$birthday}</td><tr> "; } Echo"</table>"; } //Search national names according to national code functionNationname ($db,$code) { //Write SQL statements $sql= "SELECT * from Nation where code= ' {$code}‘"; //Execute SQL statement $result=$db->query ($sql); //working with Data if($row=$result-Fetch_row ()) { return $row[1]; } Else { return""; } }
Results:
PHP Course---Practice connecting to the database and adding and deleting