PHP MySQL Connection Issue: Unable to Retrieve Data from Dat

Alles, was PHP betrifft, kann hier besprochen werden.

PHP MySQL Connection Issue: Unable to Retrieve Data from Dat

Postby joel2 » 31. August 2023 13:54

I'm facing a problem while trying to establish a connection between PHP and MySQL to retrieve data from a database. I'm using the mysqli extension for PHP, but I'm not able to fetch the expected results. Here's a simplified version of my code:

Code: Select all
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "mydatabase";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Attempt to fetch data
$query = "SELECT * FROM mytable";
$result = $conn->query($query);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
    }
} else {
    echo "No data found.";
}

$conn->close();
?>


When I run this script, I get the "No data found." message even though I have data in the mytable table. I've confirmed my database credentials and table name, but I can't figure out what's causing this issue.

Could someone please review my code and provide insights into what might be causing this problem? Is there a common mistake I might be overlooking or any debugging steps I should take to diagnose the issue? Any help would be greatly appreciated. Thank you!
joel2
 
Posts: 3
Joined: 26. April 2023 16:37
XAMPP version: 8.1.17
Operating System: Windows

Re: PHP MySQL Connection Issue: Unable to Retrieve Data from

Postby Nobbie » 01. September 2023 12:40

joel2 wrote:Here's a simplified version of my code:


What does that mean? Did you run that simplified script as well?
Nobbie
 
Posts: 12974
Joined: 09. March 2008 13:04


Return to PHP

Who is online

Users browsing this forum: No registered users and 32 guests