-
- /**
- * PHP operates database (connection, query, display results)
- * edit: bbs.it-home.org
- */
- define ('HOSTNAME', 'localhost'); //Database host name
- define ('USERNAME', 'username'); //Database Username
- define ('PASSWORD', 'password'); //Database user login password
- define ('DATABASE_NAME', 'testdb'); //Database to be queried
- $db = mysql_connect(HOSTNAME, USERNAME, PASSWORD) or
- die (mysql_error());
- //If the connection cannot be made, the reason for the mysql error will be displayed.
- mysql_select_db(DATABASE_NAME);
- //Switch to testdb
- $query =
- "SELECT uri,title FROM testdb WHERE 1 ORDER by rand() LIMIT 1";
- //The above sentence means to randomly extract an item from testdb data.
- $result = mysql_query($query);
- //Query
- while ($row = mysql_fetch_array($result)) { echo "
" ,
- ($row['title'] ), "
–" , nl2br($row['uri'])
- , ""; }
- //Display results
- mysql_free_result ($result);
- //Release the result
- mysql_close();
- //Close the connection
- ?>
Copy code
You may also like:
php mysql database class (compatible with php4 and php5)
PHP class to connect to mysql database (interface implementation)
Simple example of php operating database
A class for php to connect to mysql database
php mysql database class (php newbies’ introduction)
Simple example of php connecting to database
php mysql database operation class
php database operation class (implementing table additions, deletions, modifications, querying, fetching rows, querying multiple pieces of data, etc.)
A simple mysql database class
A class for php to call the database
php database operation code collection
Application examples of PHP database calling class (detailed comments)
|