Jump to content

need help making query results more appealing and readable


BrentonHale

Recommended Posts

Ever time I play around with the code -- i get an error message and then nothing appears.  I've been stuck on this page for three days.  At first I was not able to connect to the database, but that HAS BEEN resolved.  I just need some help with formatting the results that appear in the browser. Try loading this page in your browser and see what I mean.

 

<?php
$page_title = 'View the Current Users';

include ('./header.html');

// Page header.
echo '<h1 id="mainhead">Registered Users</h1>';



$username = "username";
$password = "password";
$hostname = "localhost"; 

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
  or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


//select a database to work with
$selected = mysql_select_db("sitename",$dbhandle)
  or die("Could not select examples");


  
  
//execute the SQL query and return records
$result = mysql_query("SELECT user_id, first_name, last_name, registration_date FROM users");




//fetch tha data from the database



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

   echo "first_name:".$row{'first_name'}."last_name:" .$row{'last_name'}."registration_date:".
   $row{'registration_date'}."<br>";  //display the results
   
}



//close the connection
mysql_close($dbhandle);

include ('./footer.html');
?>

Link to comment
Share on other sites

Add these two lines after your opening tag:

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

 

That should list any and all errors in your code preventing it from being displayed. As for formatting, What do you mean? What are you wanting to do? You're being fairly vague on the problem.

Link to comment
Share on other sites

Add these two lines after your opening tag:

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

 

That should list any and all errors in your code preventing it from being displayed. As for formatting, What do you mean? What are you wanting to do? You're being fairly vague on the problem.

 

For example, on the first result from database that displays in the browser:

 

first_name:Larrylast_name:Ullmanregistration_date:2009-12-25 19:12:05

 

It all runs together,  I would like to have spaces between the first name, last name, registration date and time.  I've tried added simple html, but when I do the page stops displaying anything in the browser.  Maybe it i was not coding properly.

 

It would be nice if I could get the results in some sort of table. I need to have Name and Date Registered printed above the columns.

Link to comment
Share on other sites

Hm? All it needs is simple spacing in php, for example look at my linebreak version of it:

while ($row = mysql_fetch_array($result)) {
   echo "first_name: ".$row['first_name']."\n<br/>";
   echo "last_name: " .$row['last_name']."\n<br/>";
   echo "registration_date: ". $row['registration_date']."<br>"; 
}

 

Or spaces is fine, You just need to add them to the echo!

Link to comment
Share on other sites

<?php
echo '<table>';
while ($row = mysql_fetch_array($result)) {

   echo "<tr>
<td>first_name:</td><td>".$row{'first_name'}."</td></tr>
<tr>
<td>last_name:</td><td>" .$row{'last_name'}."</td></tr>
<tr>
<td>registration_date:</td><td>".$row{'registration_date'}."</td></tr>";  //display the results

}
echo '</table>';
?>

or something

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.