MySQL Tutorials - Herong's Tutorial Examples - v4.46, by Herong Yang
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) 2005 HerongYang.com. All Rights Reserved. # # $con = mysqli_connect('localhost','root','TopSecret'); $con = mysqli_connect('localhost','herong','TopSecret'); print "Creating a database...\n"; $rs = mysqli_query($con,'DROP DATABASE MyBase'); $rs = mysqli_query($con,'CREATE DATABASE MyBase'); $rs = mysqli_query($con,'USE MyBase'); if (!$rs) { print(mysqli_error($con)); exit; } 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); ?>
Before running the above script, you need to make sure user "herong" has ALL privileges on all databases and tables:
herong> %mysql%\bin\mysql.exe --user=root --password Enter password: TopSecret mysql> GRANT ALL ON *.* TO herong; Query OK, 0 rows affected (0.01 sec)
If you run this script, you will get something like:
herong> \local\php\php MySqlLoop.php 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
MySQL Introduction and Installation
Introduction of MySQL Programs
►PHP Programs and MySQL Server
Configuring PHP for MySQL Server Access
MySQL Authentication Method "caching_sha2_password"
mysqli_connect() and Other MySQL Functions
►MySqlLoop.php - MySQL Functions Test
Perl Programs and MySQL Servers
Java Programs and MySQL Servers
Character Strings and Bit Strings
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
Window Functions for Statistical Analysis
Use Index for Better Performance
Transaction Management and Isolation Levels
Defining and Calling Stored Procedures
Variables, Loops and Cursors Used in Stored Procedures
System, User-Defined and Stored Procedure Variables
Storage Engines in MySQL Server
InnoDB Storage Engine - Primary and Secondary Indexes
Performance Tuning and Optimization
Installing MySQL Server on Linux