Building Chinese Web Sites using PHP
Dr. Herong Yang, Version 2.11

Fetching ASCII Text from Database to Web

This section describes how to fetch ASCII text from MySQL database and send it to a Web page through Apache.

Since all application components are ASCII compatible by default, fetching ASCII text data from a MySQL database table and send it to a Web page is easy. All you have to do is:

  • Build a SQL SELECT statement.
  • Run the SQL SELECT statement with the mysql_query() function.
  • Retrieve the ASCII text data from the returning result set object.
  • Send the retrieved ASCII text data to Apache Web server with the print() function or echo() function.

Here is my sample PHP script that fetches some ASCII text from the Comment_Mixed table. The fetched text data is delivered to Apache by calling the print() function.

<?php #MySQL-Web-ASCII.php
# Copyright (c) 2007 by Dr. Herong Yang, http://www.herongyang.com/
#
  $con = mysql_connect("localhost", "Herong", "TopSecret");
  $ok = mysql_select_db("HerongDB", $con);
  $test_name = "Moving ASCII Text from Database to Web";
  print('<html>');
  print('<body><pre>'."\n");

# Delete the record
  $sql = "DELETE FROM Comment_Mixed WHERE Test_Name ='$test_name'";
  mysql_query($sql, $con);
  print("\nNumber of rows deleted: ".mysql_affected_rows()."\n");

# Build the SQL INSERT statement
  $sql = <<<END_OF_MESSAGE
INSERT INTO Comment_Mixed (Test_Name, String_ASCII, 
    String_Latin1, String_UTF8, String_GBK, String_Big5)
  VALUES ('$test_name', 'Television', 
    'Television', 'Television', 'Television', 'Television')
END_OF_MESSAGE;
  mysql_query($sql, $con);
  print("\nNumber of rows inserted: ".mysql_affected_rows()."\n");

# Fetch text data from database
  $sql = "SELECT * FROM Comment_Mixed"
    . " WHERE Test_Name = '$test_name'";
  $res = mysql_query($sql, $con);

# Send text data out
  if ($row = mysql_fetch_array($res)) {
    print("\nTest Name = ".$row['Test_Name']."\n");
    print("   String_ASCII: ".$row['String_ASCII']."\n");
    print("   String_Latin1: ".$row['String_Latin1']."\n");
    print("   String_UTF8: ".$row['String_UTF8']."\n");
    print("   String_GBK: ".$row['String_GBK']."\n");
    print("   String_Big5: ".$row['String_Big5']."\n");
  }  
  mysql_free_result($res);

  mysql_close($con); 
  print('</pre></body></html>');
?>

After moving this PHP script file to Apache server document directory, I tested it with Internet Explorer (IE) with this URL: http://localhost/MySQL-Web-ASCII.php. No surprise on the returing Web page:

Number of rows deleted: 0

Number of rows inserted: 1

Test Name = Moving ASCII Text from Database to Web
   String_ASCII: Television
   String_Latin1: Television
   String_UTF8: Television
   String_GBK: Television
   String_Big5: Television

Sections in This Chapter

Steps and Application Components Involved

Fetching ASCII Text from Database to Web

Fetching Chinese Text from Database to Web

Fetching Chinese Text from Database to Web in UTF-8

Fetching Chinese Text from Database to Web in GBK

Fetching Chinese Text from Database to Web in Big5

Summary - Fetching Chinese Text from Database to Web

Dr. Herong Yang, updated in 2007
Fetching ASCII Text from Database to Web