Link database:
mysql_connect (' server ', ' username ', ' password ');
<?PHP $link= Mysql_connect ('localhost','Root','abc123'); if(!$link) {Die ('Link failed:'. Mysql_error ()); } Echo"link success with MySQL <br/>"; Echo Mysql_get_client_info (); //version information for the Client API function libraryEcho Mysql_get_host_info ();//link type for MySQL serverEcho Mysql_get_proto_info ();//version information for communication protocolsEcho Mysql_get_server_info ();//version information for MySQL serverEcho mysql_client_encoding ();//default character set used by the clientEcho Mysql_stat ();//Current working status of MySQL servermysql_close ($link); //close the link established by the MySQL server
Select the database you have created:
mysql_select_db (' database name ');
<?PHP$link=mysql_connect(' localhost ', ' root ', ' abc123 '); if(!$link){ die(' Link failed: '.Mysql_error()); } Echo"Database Link succeeded"; mysql_select_db(' book ',$link) or die(' cannot select Database book '.Mysql_error());//$link parameters can be omitted Mysql_close($link);
Execute SQL command:
mysql_query (SQL statement);
<?PHPHeader("content-type:text/html; Charset=utf-8 "); $link=mysql_connect(' localhost ', ' root ', ' abc123 '); if(!$link){ die(' Database link failed: '.Mysql_error()); } Echo' Database link succeeded '; mysql_select_db(' Test ',$link) or die(' Cannot select Database Test '.)Mysql_error()); $create= "CREATE TABLE IF not EXISTS book (id SMALLINT UNSIGNED auto_increment PRIMARY KEY, BookName VARCHAR (50) Not null default ' ', author VARCHAR (+) NOT null default ')"; //Create a data table book $result 1=mysql_query($create); if($result 1){ Echo"Create data Table Success <br/>"; }Else{ Echo"Failed to create data table".Mysql_error(); } $insert= "INSERT into book VALUES (null, ' PHP ', ' Gaulo '), (null, ' JSP ', ' lou xxx '), (null, ' ASP ', ' Gold xxx ' )"; $result 2=mysql_query($insert); //Insert Record, mysql_affected_rows () gets the number of bars that have modified the record if($result 2&&mysql_affected_rows() >0){ Echo"The data record was inserted successfully and the last data record ID is".mysql_insert_id()." <br/> "; }Else{ Echo"Insert record failed, error number:".Mysql_errno(). ", Error Reason:".Mysql_error(); } //execute the UPDATE command to modify the records in book to change the author of Php to ' Li ' $result 3=mysql_query("UPDATE book SET author= ' Lee ' WHERE bookname= ' PHP '"); if($result 3&&mysql_affected_rows() >0){ Echo"Data record modified successfully <br/>"; }Else{ Echo"Data record modification failed with error number:".Mysql_errno(). ", Error Reason:".Mysql_error()." <br/> "; } //Close the database Mysql_close($link);
php-php built-in MySQL function