Contact PHP is equivalent to a step closer to the background, has been doing the front end, but also learning PHP, but has not dared to write a blog, now finally have the courage to step forward, but also please bo friends more magnanimous.
Server is the background development of the necessary tools, but for the general beginner is not their own server, so we use their own computer to do the server, if it is Windows system download Wamp, that is, Windows, Apache, Mysql, If PHP is a Linux system then download Lamp,l is Linux. After the installation is successful, you can create your own project by opening localhost, while phpMyAdmin is turning on MySQL.
Start by creating a database that is named php010
Next Create the Weather table
Then enter the statement that created the form in the SQL option
CREATE TABLEWeather ('Index`INT(Ten) not NULLauto_increment, ' Citycode 'INT( -) not NULL, ' CityName 'CHAR( -) not NULL ,PRIMARY KEY(`Index`))
After successful execution, in order to avoid garbled problem, need to change a little bit, the CityName encoding format to GB2312_CHINESE_CI encoding, now the table has been created, began to write PHP
Find out a few common key statements
Mysql_connect () A server connection to turn on MySQL
mysql_select_db () Select a MySQL database
mysql_query (String $query, Resource $link) send a MySQL query
@ and Or die hide errors and condition display
Mysql_error () returns the text error message from the previous MySQL operation
<?PHP$conn= @mysql_connect("localhost", "root", "") or die("link Error".)Mysql_error()); //@ Mask Errorif($conn){ Echo"Successfully connected server .<br>";}Else{ Echo"Connection Server failed .<br>";}$result=mysql_select_db("php010",$conn);if($result){ Echo"Successfully connected Database .<br>";}Else{ Echo"Connection Database failed .<br>";}$GBK=mysql_query("Set names ' GBK '");if($GBK){ Echo"GBK is setted.<br>";}Else{ Echo"GBK Set faliure.<br>";}$sql= "INSERT into weather (' index ', ' Citycode ', ' CityName ') VALUES (1, 10101111, ' Shenzhen ')";$res=mysql_query($sql,$conn);//link PHP executes a MySQL statementif($res){ Echo"You have successfully added the statement";}Else{ Echo"Add statement failed";}?>
To learn more about the success of each step of our code execution, the IF-ELSE statement was executed several times, the following is a brief version
<? PHP $conn = @mysql_connect die ("link Error". ) Mysql_errormysql_select_db("php010",$connmysql_query("set Names ' GBK ' "$sql =" INSERT into weather (' index ', ' Citycode ', ' CityName ') VALUES (1, 10101111, ' Shenzhen ') "mysql_query($sql$conn);? >
After successful execution, you can discover that a set of data has been added to the weather table
Next, look for the data in the table.
Mysql_fetch_row () is used to save a row of query results to an array,
Mysql_fetch_array () can also be indexed with domain names (field name, Id,uid,) in addition to the 0-based offset
Mysql_fetch_array () is an extended version of Mysql_fetch_row (). In addition to storing data as a numeric index in an array, you can also store the data as an associated index, using the field name as the key name
Print_r ($arr) displays easy-to-understand information about a variable. If a string, integer, or float is given, the variable value itself is printed. If an array is given, the keys and elements will be displayed in a certain format
Mysql_num_rows () returns the number of rows in the result set. This command is valid only for SELECT statements. To get the number of rows affected by the Insert,update or DELETE query, use Mysql_affected_rows ()
<?PHP$conn= @mysql_connect("localhost", "root", "") or die("Connect Error"); mysql_select_db("php010",$conn); mysql_query("Set names ' GBK '"); $sql= "SELECT * from Weather"; $query=mysql_query($sql,$conn); while($row=Mysql_fetch_array($query)){ Print_r($row); $id= 2; Echo"<br>".$row[$id]." <br> "; } Echo mysql_num_rows($query);?>
The result is
This enables the ability to create and look up tables in MySQL.
Shortcomings, please give us a lot of advice!
Create MySQL database with PHP