PHP Course---Practice connecting the database and adding and deleting changes

Source: Internet
Author: User

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

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.