Basic Syntax PHP MySQL


You can use PHP and MySQL together in a single file to perform various database operations. Here’s a step-by-step guide on how to do this using procedural style in PHP:

Steps to Use PHP and MySQL in a Single File

  1. Establish a Database Connection
  2. Execute SQL Queries
  3. Fetch Results
  4. Close the Connection

Here’s an example that demonstrates how to connect to a MySQL database, execute a query, and fetch results using procedural PHP:

Example: Fetching Data from a MySQL Database

<?php // Step 1: Establish a Database Connection $servername = "localhost"; // Replace with your database server name $username = "root"; // Replace with your database username $password = ""; // Replace with your database password $dbname = "test"; // Replace with your database name // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Step 2: Execute SQL Query $sql = "SELECT id, name, email FROM users"; // Replace with your SQL query $result = mysqli_query($conn, $sql); // Step 3: Fetch Results if (mysqli_num_rows($result) > 0) { // Output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>"; } } else { echo "0 results"; } // Step 4: Close the Connection mysqli_close($conn); ?>

Explanation

  1. Establish a Database Connection:

    • Use mysqli_connect() to connect to the MySQL database.
    • Check if the connection is successful; if not, display an error message.
  2. Execute SQL Query:

    • Write your SQL query as a string and execute it using mysqli_query().
    • mysqli_query() returns a result set for SELECT queries, or TRUE for INSERT, UPDATE, and DELETE queries.
  3. Fetch Results:

    • Use mysqli_num_rows() to check if there are any rows in the result set.
    • Loop through the result set with mysqli_fetch_assoc() to fetch rows as associative arrays.
    • Output the data as needed.
  4. Close the Connection:

    • Use mysqli_close() to close the connection to the MySQL database.

Additional Operations

You can perform other operations such as inserting, updating, or deleting records using similar procedures. Here's an example of inserting a new record:

<?php // Establish a Database Connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Insert Data $sql = "INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com')"; if (mysqli_query($conn, $sql)) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } // Close the Connection mysqli_close($conn); ?>

Summary

  • Establish a connection: Use mysqli_connect() to connect to the database.
  • Execute a query: Use mysqli_query() to run your SQL query.
  • Fetch results: Use mysqli_fetch_assoc() to retrieve rows.
  • Close the connection: Use mysqli_close() to close the database connection.