mysqli-hello.php - Test MySQLi Module

This section provides a tutorial example on how to test the MySQLi module configuration.

To try the MySQLi module, you need to install a MySQL server on your computer first. On my local Windows system, I have a MySQL server installed in \mysql directory. If you need help on installing a MySQL server on your system, please read my other tutorial book, "MySQL Tutorials - Herong's Tutorial Examples" at herongyang.com/MySQL/

To make sure my MySQL server is running on my local system, I executed commands below to start my MySQL server and check its status:

herong> \mysql\bin\mysqld

herong> \mysql\bin\mysqladmin ping
mysqld is alive

Now I am ready to test the MySQLi module. Here is my first PHP script:

<?php
#  mysqli-hello.php
#- Copyright 2009-2026 (c) HerongYang.com. All Rights Reserved.

  # Connect to the MySQL server through local socket pipe
  $con = mysqli_connect("localhost");

  # Prepare a MySQL query statement
  $sql = "SELECT 'Hello world!'";

  # Send the query to the server 
  $res = mysqli_query($con, $sql);

  # Fetch the first row of the query result 
  $row = mysqli_fetch_row($res);

  # Print out the first data element of the row 
  print($row[0]."\n");

  # Disconnect from the server
  mysqli_close($con);
?>

Here is what I get when running this script:

herong> php mysqli-hello.php

Hello world!

Cool, this confirmed that my PHP environment configured correctly to access my MySQL server.

Note that I did not specify user name and password when connecting to the MySQL server. The MySQLi module uses the default values from mysqli.default_user and mysqli.default_pw configuration settings in the php.ini file.

Table of Contents

 About This Book

 Introduction and Installation of PHP

 Managing PHP Environment and Modules on macOS

 Managing PHP Environment and Modules on CentOS

 Configuration and Modules

 cURL Module - Client for URL

 DOM Module - Parsing HTML Documents

 GD Module - Manipulating Images and Pictures

MySQLi Module - Accessing MySQL Server

 MySQLi Module and Configuration

mysqli-hello.php - Test MySQLi Module

 mysqli_connect() - Open MySQL Connection

 mysqli_query() - Run SQL Queries

 mysqli_affected_rows() - Count Affected Rows

 mysqli_insert_id() - ID from AUTO_INCREMENT

 mysqli_fetch_array() - Fetch Row from Query Result

 mysqli_fetch_fields() - Fetch Field Information

 Escape Escape Characters - \\\\ for \\

 Summary of Basic MySQLi Functions

 MySQLi Object-Oriented Programming

 OpenSSL Module - Cryptography and SSL/TLS Toolkit

 PCRE Module - Perl Compatible Regular Expressions

 SOAP Module - Creating and Calling Web Services

 SOAP Module - Server Functions and Examples

 Zip Module - Managing ZIP Archive Files

 References

 Index

 Full Version in PDF/EPUB