Jump to content

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
https://forums.phpfreaks.com/topic/298110-issue-displaying-database-rows/
Share on other sites

Apply error checking to your query

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

// error check query. mysqli_query returned false on error
if($result) 
{
    // process query results
}
else
{
    // get the error from the query
    trigger_error('Query returned an error: ' . mysqli_error($conn));
}

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);
?>

If I echo in the // get the error from the query 

 

I do get a echo back but no error message so perhaps it is being supressed... Can you please tell me how I can show the error?

I can not locate the .ini file, perhaps you can provide one?

many thanks

 

Yes, make sure in your php.ini you have error_reporting set to E_ALL and display_errors is set to On. Alternatively add these two line at the top of your script

ini_set('display_errors', 1);
error_reporting(E_ALL);

PHP errors are also logged in your servers error logs.

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

 

$sql = "SELECT id, column FROM databasetable";

"column" is a reserved word - avoid using for column or table names. If you do use it then it needs to be in backticks

$sql = "SELECT id, `column` FROM databasetable";
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.