Archived: MySqlLoop.php using php_mysql.dll Library

This section provides a tutorial example on how to use MySQL functions to connect to a MySQL server, and run SQL statements to create a table, insert rows and fetch rows with the MySQL server.

To show you how some of those MySQL functions should be used, I wrote this simple script, MySqlLoop.php:

<?php
#  MySqlLoop.php
#- Copyright 2009 (c) HerongYang.com. All Rights Reserved.
#
   $con = mysql_connect('localhost');
   $rs = mysql_query('DROP DATABASE MyBase');
   $rs = mysql_query('CREATE DATABASE MyBase');
   $rs = mysql_query('USE MyBase');
   print "Creating a table...\n";
   $rs = mysql_query('CREATE TABLE MyTable (ID INTEGER,'
      .' Value INTEGER)');
   $n = 100;
   $i = 0;
   print "Inserting some rows to the table...\n";
   while ($i < $n) {
      $rs = mysql_query('INSERT INTO MyTable VALUES ('.$i.', '
         .rand(0,$n-1).')');
      $i++;
   }
   print "Query some rows from the table...\n";
   $rs = mysql_query('SELECT * FROM MyTable WHERE ID < 10');
   print "   ".mysql_field_name($rs,0)."   "
      .mysql_field_name($rs,1)."\n";
   while ($row = mysql_fetch_array($rs)) {
      print "    ".$row[0].'   '.$row[1]."\n";
   }
   mysql_free_result($rs);
   mysql_close($con);

function mysqli_field_name($result, $field_offset) {
   $properties = mysqli_fetch_field_direct($result, $field_offset);
   return is_object($properties) ? $properties->name : null;
}
?>

Note that if the connection resource is not specified in a query call, the last connection resource will be used. If you run this script, you will get something like:

Creating a table...
Inserting some rows to the table...
Query some rows from the table...
   ID   Value
    0   14
    1   91
    2   84
    3   16
    4   88
    5   51
    6   12
    7   19
    8   39
    9   5

Table of Contents

 About This Book

 Introduction and Installation of PHP

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Interface with Operating System

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 Managing File Upload

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

 Managing Non-ASCII Character Strings with MySQL Servers

 Parsing and Managing HTML Documents

 Configuring and Sending Out Emails

 Image and Picture Processing

 Managing ZIP Archive Files

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

Archived Tutorials

 Archived: Downloading and Installing PHP 7.0 for Windows

 Archived: Downloading and Installing PHP 5.6.6 for Windows

 Archived: Downloading and Installing PHP 5.4.3 for Windows

 Archived: Downloading and Installing PHP 5.2.2 for Windows

 Archived: Downloading and Installing PHP 5.0.4 for Windows

 Archived: Adding PHP to IIS as CGI

 Archived: Downloading Apache HTTP Server 2.4.12 for Windows

 Archived: Installing Apache HTTP Server 2.4.12 on Windows

 Archived: Publishing PHP Scripts as Web Pages

 Archived: Configuration for php_mysql.dll Library

 Archived: mysql_connect() using php_mysql.dll Library

Archived: MySqlLoop.php using php_mysql.dll Library

 Archived: Get_Temperature.php - First Example of SOAP

 Archived: Get_Temperature_Dump.php - Dumping Debugging Information

 Archived: MySqlUnicode.php using php_mysql.dll Library

 References

 Full Version in PDF/EPUB