Getting started with MySQLi


Creating a MySQL Database via cPanel


To create a database and user in cPanel, follow these steps:

  • Step 1: Create the Database
    Go to the MySQL Databases section in cPanel. In the "New Database" box, enter a name for your database and click Create Database.
  • Step 2: Create the Database User
    Scroll down to the "MySQL Users" section. Enter a username and password in the respective boxes, then click Create User.
  • Step 3: Assign User to the Database
    Scroll further down to the "Add User to Database" section. Select the user and database from the dropdown menus, then click Add User to Database. Assign the required privileges and click Make Changes.


Your database and user are now set up and ready to use with the PHP connection code provided above.



The database connection - mysqli_connect


The following example demonstrates how to connect to a MySQL database using the procedural approach:

PHP Code
  1. <?php
  2. $connection = mysqli_connect("localhost", "your_username", "your_password", "your_database");
  3.  
  4. if (!$connection) {
  5. die("Connection failed: " . mysqli_connect_error());
  6. }
  7.  
  8. echo "Connected successfully";
  9. ?>


What’s Happening Here:
  • mysqli_connect(): Establishes a connection to the MySQL database.
  • mysqli_connect_error(): Returns the error message if the connection fails.
  • die(): Stops script execution and outputs the error message.




Example MySQLi query


Always validate and sanitise user input to prevent SQL injection and other vulnerabilities. Use prepared statements with mysqli for executing SQL queries. Here’s a quick example:

PHP Code
  1. <?php
  2. // Ensure the database connection code is included before this block
  3.  
  4. // Prepare a SQL query with a placeholder for the email parameter
  5. $stmt = $connection->prepare("SELECT * FROM users WHERE email = ?");
  6.  
  7. // Bind the email parameter to the query ("s" indicates the parameter is a string)
  8. $stmt->bind_param("s", $email);
  9.  
  10. // Set the value for the parameter
  11. $email = "example@example.com";
  12.  
  13. // Execute the prepared statement
  14. $stmt->execute();
  15.  
  16. // Get the result of the query
  17. $result = $stmt->get_result();
  18.  
  19. // Loop through each row in the result set and display the 'name' column
  20. while ($row = $result->fetch_assoc()) {
  21. echo $row['name'];
  22. }
  23.  
  24. // Close the statement to free up resources
  25. $stmt->close();
  26.  
  27. // Close the database connection
  28. $connection->close();
  29. ?>




Conclusion


This tutorial demonstrates how to connect to a MySQL database using MySQLi in PHP.
We have learnt how to create a database via cPanel and includes important security tips to ensure your code is safe and functional.
Always use modern methods like MySQLi or PDO to stay compatible with current PHP versions.
Joshua's Avatar
Author:
Views:
2,195
Rating:
Posted on Friday 9th February 2007 at 09:48 PM
Joshua
Joshua's Avatar
Is mine good?

If so, thanks, this was actually one of the firsts I wrote.
Posted on Friday 9th February 2007 at 09:35 PM
DanielXP
DanielXP's Avatar
I will have a look into my one.

Posted in the early days, you know what i mean.
Posted on Friday 9th February 2007 at 09:31 PM
Dean
Dean's Avatar
yah ano it is . But still its already been posted :)
Posted on Friday 9th February 2007 at 05:06 PM
Nevram
Nevram's Avatar
yes, but this is much better explained. lol
Posted on Thursday 8th February 2007 at 10:11 PM
Dean
Dean's Avatar
Well daniel posted a tutorial similar to this.
Posted on Thursday 8th February 2007 at 08:41 PM
Nevram
Nevram's Avatar
Lol im the biggest n00b. I never got it until i read this!:p