MySQL Connection database

Source: Internet
Author: User

<?PHP$con[Email protected]mysql_connect(' localhost ', ' root ', ', ' 3306 '); if(!$con){         die(' Connection failed '.Mysql_error()); }Else{        Echo' Connected successfully! ‘; }    mysql_query(' Set name UTF8 ',$con); mysql_select_db(' Kun ',$con); $result=mysql_query(' SELECT * from T ',$con) or die(Mysql_error()); //$result =mysql_query (' Select COUNT (*) from T ');  $row =mysql_fetch_row ();    echo $row [0]; DQL First output: (Recommended with Mysql_fetch_row () and MYSQL_FETCH_ASSOC)//performance is highest, you must know the column number of the query data (that is, the index 0123)     while($row=Mysql_fetch_row($result)){        Echo $row[0]. ' <br/> '; }    /*While ($row =mysql_fetch_array ($result)) {//Returns an indexed array and an associative array//Echo $row [3]. '    <br/> ';//The data is more used, the lowest efficiency, the deposit of double (Index and association) [0] = laughing and joking [name], and laughing and joking Print_r ($row); }    */    /*While ($row =mysql_fetch_assoc ($result)) {//Returns an associative array echo $row [' name ']. '    <br/> '; }           */     while($row-Mysql_fetch_object($result)){//returns an object        Echo $row-uid; }       //the second output:     while($row=$result-Fetch_row ()) {        foreach($row  as $key=$a){            Echo" --$a--  "; }    }    //DML Output:        $result=mysql_query(' delete from T where id=3 ',$con); or$result=mysql_query(' INSERT into T (uname) value (' Wang ') ',$con); or$result=mysql_query(' Update t set uagree=43 where id=2 ',$con); $lastInsertId=mysql_insert_id();//gets the last inserted ID, only valid for insert    if(!$result){         die(' Operation failed '.Mysql_error()); }      if(mysql_affected_rows($con) >0){        Echo"Operation succeeded"; }Else{        Echo"Row count not affected"; }    Mysql_free_result($result);//releasing the resulting memory DML does not require this, as this frees the resource type, whereas the $result in DML is of type bool    Mysql_close($con);//Close the database (can not write, will automatically close, suggested write)          /*Expand: \s or Show tables view the current user's database Netstat–an view network connection Status Var_dump () view type Send directives include (DDL data definition statement, DQL (Sele CT), return MySQL result DML data Operation statement Update delete INSERT, return BOOL DTL data TRANSACTION statement Rollback commit ... )*/


MySQL Connection database

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.