Jump to content

Issue displaying database rows


Lewisdow

Recommended Posts

hi

 

I have a basic script that connects to the mysql database server successfully.

There are no connection errors however when I query the table the script then reports that 0 rows were returned.  I know that there is data available as I can query the table in the mysql server tool.

Please could someone point out what may be the problem.

Below is the script I am using.

thanks,

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "databasename";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, column FROM databasetable";
$result = mysqli_query($conn, $sql);

echo "id: " . $row["id"]. "<br>";

if (mysqli_num_rows($result) > 0) {
	
	echo $row;
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
    	echo $sql;
        echo "id: " . $row["id"]."<br>";
    }
} else {
    echo "0 results";
}

mysqli_close($conn);
?>
Link to comment
Share on other sites

Hi, thank you

Nothing was returned from try error, unless I have a setting or something that is hiding the error message?

Script below...

<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "databasename";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT id, column FROM databasetable";
$result = mysqli_query($conn, $sql);



$result = mysqli_query($conn, $sql);

// error check query. mysqli_query returned false on error
if($result) 
{
    // process query results

    if (mysqli_num_rows($result) > 0) {
	
	    // output data of each row
	    while($row = mysqli_fetch_assoc($result)) {
	    	echo $sql;
	        echo "id: " . $row["id"]."<br>";
	    }
	} else {
	    echo "0 results";
	}
    
    
}
else
{
    // get the error from the query
    trigger_error('Query returned an error: ' . mysqli_error($conn));
}

mysqli_close($conn);
?>
Link to comment
Share on other sites

Upon further review:

 

YOu are doing two query calls. You are trying to grab data before you do the fetch, which I finally noticed.

 

- do the query call (just once!)

- check if it ran

- check if there are rows

If all this works:

- loop thru the rows and grab the items from the $row and echo them

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.