10 days Learn PHP Day 5

web front end five thousand six hundred and thirty-eight 13 years ago (2011-04-08)

Learning purpose: learn to read data

Let's look at two functions:
    1、mysql_query
Send a query string. Syntax: int mysql_query (string query, int [link_identifier]); Return value: integer

This function sends a query string for MySQL to perform related processing or execution. If the link_identifier parameter is not specified, the program will automatically find the recently opened ID. When the query string is UPDATE, INSERT and DELETE, it may return true or false; If the query string is SELECT, a new ID value will be returned. If false is returned, the query string is not successful but has an error.

2. Mysql_fetch_object returns class data. Syntax: object mysql_fetch_object (int result, int [result_typ]); Return value: class

This function is used to disassemble the query result into a class variable. If the result has no data, the value of false is returned.

Look at a simple example:

PHP code
  1. <?     
  2. $exec = "select * from user" ;     
  3. $result =mysql_query( $exec );   
  4. while ( $rs =mysql_fetch_object( $result ))   
  5. {   
  6. echo   "username:" . $rs ->username. "<br>" ;     
  7. }   
  8. ?>   

Of course, there is a username field in the table user, which is similar to ASP In

PHP code
  1. <%   
  2. exec = "select * from user"   
  3. set rs=server.createobject( "adodb.recordset" )    
  4. rs.open  exec ,conn,1,1   
  5. do   while  not rs.eof   
  6. response.write  "username:" &rs( "username" ) "<br>"   
  7. rs.movenext    
  8. loop    
  9. %>  

Of course, connect first data base , we generally require_once ('conn. php '); The conn.php is the code for connecting to the database mentioned last time.

Two small commands can complete the task of reading data. Today, we will talk about adding, deleting and modifying data next time.