MySqlLoop.php - MySQL Functions Test

A tutorial example is provided 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 (c) 2015, HerongYang.com, All Rights Reserved.
# 
   $con = mysqli_connect('localhost','root','TopSecret');
   $rs = mysqli_query($con,'DROP DATABASE MyBase');
   $rs = mysqli_query($con,'CREATE DATABASE MyBase');
   $rs = mysqli_query($con,'USE MyBase');
   print "Creating a table...\n";
   $rs = mysqli_query($con,'CREATE TABLE MyTable (ID INTEGER,'
      .' Value INTEGER)');
   $n = 100;
   $i = 0;
   print "Inserting some rows to the table...\n";
   while ($i < $n) {
      $rs = mysqli_query($con,'INSERT INTO MyTable VALUES ('.$i.', '
         .rand(0,$n-1).')');
      $i++;
   }
   print "Query some rows from the table...\n";
   $rs = mysqli_query($con,'SELECT * FROM MyTable WHERE ID < 10');
   print "   ID   Value\n";
   while ($row = mysqli_fetch_array($rs)) {
      print "    ".$row[0].'   '.$row[1]."\n";
   }
   mysqli_free_result($rs);   
   mysqli_close($con);
?>

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

Last update: 2015.

Table of Contents

 About This Book

 Introduction of SQL

 MySQL Introduction and Installation

 Introduction of MySQL Programs

 Perl Programs and MySQL Servers

PHP Programs and MySQL Servers

 Configuring PHP for MySQL Server Access

 mysqli_connect() and Other MySQL Functions

MySqlLoop.php - MySQL Functions Test

 Java Programs and MySQL Servers

 Datatypes and Data Literals

 Operations and Expressions

 Character Strings and Bit Strings

 Commonly Used Functions

 Table Column Types for Different Types of Values

 Using DDL to Create Tables and Indexes

 Using DML to Insert, Update and Delete Records

 Using SELECT to Query Database

 Transaction Management and Isolation Levels

 Locks Used in MySQL

 Defining and Calling Stored Procedures

 Variables, Loops and Cursors Used in Stored Procedures

 Outdated Tutorials

 References

 PDF Printing Version