EmperorJazzy Posted March 4, 2011 Share Posted March 4, 2011 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> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 If the 'view source' of the page in your browser shows the 'php code', then you got caught using php's lasy-way short open tags - <? You should only use full opening php tags - <?php Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 another example of lazyness is using SELECT * (especialy when you are only echoing a single field!) Quote Link to comment Share on other sites More sharing options...
EmperorJazzy Posted March 4, 2011 Author Share Posted March 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted March 4, 2011 Share Posted March 4, 2011 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 , 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? Quote Link to comment Share on other sites More sharing options...
EmperorJazzy Posted March 4, 2011 Author Share Posted March 4, 2011 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 , 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
EmperorJazzy Posted March 4, 2011 Author Share Posted March 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 Either php is not installed on your server or your file name does not end in .php or you are browsing directly to the file on your computer instead of using a URL (such as http://localhost/your_file.php ) in your browser's address bar. Quote Link to comment Share on other sites More sharing options...
EmperorJazzy Posted March 4, 2011 Author Share Posted March 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 Does the 'view source' show your </body></html> tags? Quote Link to comment Share on other sites More sharing options...
EmperorJazzy Posted March 4, 2011 Author Share Posted March 4, 2011 Does the 'view source' show your </body></html> tags? So now my 'view source' looks like this; <html> <head> <title>The List Site</title> </head> <body> </body> </html> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
EmperorJazzy Posted March 4, 2011 Author Share Posted March 4, 2011 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(); ? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 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); Quote Link to comment Share on other sites More sharing options...
EmperorJazzy Posted March 4, 2011 Author Share Posted March 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2011 Share Posted March 4, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.