DeveloperBreeze

MySQLi Database Connection and Query

// Database connection parameters
$servername = 'localhost';
$username = 'username';
$password = 'password';
$dbname = 'database';

// Create a MySQLi connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die('Connection failed: ' . $conn->connect_error);
}

// SQL query to select all rows from the 'users' table
$sql = 'SELECT * FROM users';
$result = $conn->query($sql);

// Check if there are results
if ($result->num_rows > 0) {
    // Loop through the results and display 'name' field
    while ($row = $result->fetch_assoc()) {
        echo 'Name: ' . $row['name'];
    }
} else {
    echo 'No results found.';
}

// Close the database connection
$conn->close();

Related Posts

More content you might like

Code
python

Configuring SQLAlchemy with PostgreSQL on Heroku: A Quick Guide

Instead of manually replacing the URI, you can use libraries like dj-database-url to parse and adapt the DATABASE_URL for different frameworks or drivers. However, the manual approach is straightforward and works well for most cases.

Nov 08, 2024
Read More
Tutorial

Connecting a Node.js Application to an SQLite Database Using sqlite3

   mkdir sqlite3-tutorial
   cd sqlite3-tutorial

Initialize the project with default settings:

Oct 24, 2024
Read More
Code
php

MySQL Database Query and Result Processing

No preview available for this content.

Jan 26, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!