Jump to content

NEWB Q - Reading Records from mySQL but No Output


EmperorJazzy

Recommended Posts

Morning All,

 

A simple query; I've written a small piece of code to read all the records in my DB table and 'echo' them to the screen. When running, the other bits of the page (HTML only) is showing fine. The space where the code should display is blank (not even blank lines are output from my code). Any assistance would be helpful. I fear it may be something simple that I've overlooked. Thanks in advance.

 

<p>
Reading the users from the database…tblUser<br><br>

<?

$dbc = mysqli_connect('localhost','username','password','dbGeneral') or die('Error connecting to dbGeneral');

$query = "SELECT * FROM tblUser";

$result = mysqli_query($dbc, $query) or die('Error executing SELECT * statement');

while ($row = mysqli_fetch_array($result))
{

 echo $row['Full_name'];

}

mysqli_close($dbc);

?>

</p>

Link to comment
Share on other sites

Thanks for the feedback thus far.

 

1 - I had <?php but it wasn't working so I dropped the PHP, have put it back in. No go. Anything more?

 

2 - Laziness(?) it's not for a simple field, I am utilising all the fields being returned. However, when debugging, I reduced the output to one field. Though that doesn't help my problem of the code not working, the SELECT * is an SQL statement that will work.

Link to comment
Share on other sites

2 - Laziness(?) it's not for a simple field, I am utilising all the fields being returned. However, when debugging, I reduced the output to one field. Though that doesn't help my problem of the code not working, the SELECT * is an SQL statement that will work.

I don't care :P, the only reason to use SELECT * is because you can't be bothered typing in the field names.  Now, does the page source show anything where the php should be?

Link to comment
Share on other sites

2 - Laziness(?) it's not for a simple field, I am utilising all the fields being returned. However, when debugging, I reduced the output to one field. Though that doesn't help my problem of the code not working, the SELECT * is an SQL statement that will work.

I don't care :P, the only reason to use SELECT * is because you can't be bothered typing in the field names.  Now, does the page source show anything where the php should be?

 

I've reduced the page source to just the php code....

<html>
<head>
<title>The List Site</title>
</head>

<body>

<?php

$dbc = mysqli_connect('localhost','username','password','db') or die('Error connecting to db');

$query = "SELECT * FROM tblUser";

$result = mysqli_query($dbc, $query) or die('Error executing SELECT * statement');

while ($row = mysqli_fetch_array($result))
{

 echo $row['Fname'];

}

mysqli_close($dbc);

?>

</body>
</html>

 

The page now displays blank, not even error returns.

Link to comment
Share on other sites

And the 'view source' of that page is?????

 

The reason I keep asking what you are getting for the actual output to the browser is that will pin down where exactly the problem is.

<html>
<head>
<title>The List Site</title>
</head>

<body>

<?php

$dbc = mysqli_connect('localhost','username','password','db') or die('Error connecting to db');

$query = "SELECT * FROM tblUser";

$result = mysqli_query($dbc, $query) or die('Error executing SELECT * statement');

while ($row = mysqli_fetch_array($result))
{

 echo $row['Fname'];

}

mysqli_close($dbc);

?>

</body>
</html>

 

 

View source - Apologies. The viewsource shows the same code above (and I realise the <?php.....?> should not be showing in viewsource window.

 

FYI - There are 3 rows in the table, and I just tested another page that has PHP coding to add records to the table and it processed without issue.

Link to comment
Share on other sites

I'm using the application TEXTEDIT on the Mac. Have just reviewed the other php files that work and noticed the PLAIN TEXT ENCODING was different on this file. I've altered it to MAC OS Roman from a UNICODE format. Now the 'VIEW SOURCE' doesn't show the PHP, but the PHP still isn't returning any output.

 

I'm constantly working on the server with PHP installed on my web-hosting. I don't work off PHP files on my local machine.

Link to comment
Share on other sites

Either your query is matching zero rows or there is no column named Fname in your table.

 

Are you doing this on a system with error_reporting set to E_ALL (or a -1) and display_errors set to ON so that all the php detected errors will be reported and displayed? This would produce an error if the query returns a row but $row['Fname'] does not exist.

 

You can and should use mysqli_num_rows() to find out if there are any rows returned by a query and display an appropriate message if there are not.

 

Link to comment
Share on other sites

Either your query is matching zero rows or there is no column named Fname in your table.

 

Are you doing this on a system with error_reporting set to E_ALL (or a -1) and display_errors set to ON so that all the php detected errors will be reported and displayed? This would produce an error if the query returns a row but $row['Fname'] does not exist.

 

You can and should use mysqli_num_rows() to find out if there are any rows returned by a query and display an appropriate message if there are not.

 

- Not sure about the system configuration with regard to errors. I'll enquire through my web-hosting.

 

- What's the best application for mysqli_num_rows(); is it simply echo mysqli_num_rows(); ?

Link to comment
Share on other sites

	if(mysqli_num_rows($result)){
	// at least one row, process the data from the query
	while ($row = mysqli_fetch_array($result))
	{
		echo $row['Full_name'];
	}
} else {
	// query matched zero rows
	echo "Sorry, there is no data to display!";
}

 

I also see that your first code used $row['Full_name'] while your later code used $row['Fname']. If your php code is running at all (the following won't help with fatal parse errors in your main file), you can put the following two lines of code in your file to set the error_reporting/display_errors settings at runtime -

 

ini_set("display_errors", "1");
error_reporting(-1);

Link to comment
Share on other sites

Now we're cooking!! I added the error variables and we're getting feedback.

 

RE: FName and Full_Name, I just abbreviated for the purposes of pasting code.

 

I finally got a return of errors and now I'm progressing to debug.

 

Notice: Undefined index: Full_name in /var/www/vhosts/website/httpdocs/ListSite/usertest.php on line 24

 

Notice: Undefined index: Full_name in /var/www/vhosts/website/httpdocs/ListSite/usertest.php on line 24

 

Notice: Undefined index: Full_name in /var/www/vhosts/website/httpdocs/ListSite/usertest.php on line 24

 

Notice: Undefined index: Full_name in /var/www/vhosts/website/httpdocs/ListSite/usertest.php on line 24

 

 

So I looked over the database and found that the field name is Full_Name not Full_name...... I forgot about case-sensitivity. I am now getting output returned.

 

Thank you very much for your persistence and help with this.

Link to comment
Share on other sites

I just abbreviated for the purposes of pasting code

 

^^^ Computers only do exactly what their code tells them to do and we only see exactly the information you supply in your posts. When you don't post accurate code and data, it causes wild goose chases fixing things that are not actually present.

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.