Php/mysql novice needs help.

  • digisales
  • Newbie
  • Newbie
  • digisales
  • Posts: 9

Post 3+ Months Ago

I am a novice with php and mysql, and need some help. The following script creates a list of email addresses on a mailing list housed in mysql.

$db_name = "dbname";
$table_name = "mlist";
$connection = @mysql_connect("localhost", "user", "pswd")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT id, email_addr FROM $table_name ORDER BY id";
$result = @mysql_query($sql, $connection) or die (mysql_error());
$contact_list = "<ul>";
while ($row = mysql_fetch_array ($result)) {
$id = $row['id'];
$email_addr = $row['email_addr'];
$contact_list .= "<li>$email_addr";
}
$contact_list .= "</ul>";
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<? echo "$contact_list"; ?><br>



It currently only lists the address, but the table also contains fields with first and last name (f_name and l_name). I need to adjust the above coding so that is will display and list all the info as follows:

f_name l_name email_addr

Any help will be much appreciated. Thank you. :?:
  • dark_lord
  • Graduate
  • Graduate
  • User avatar
  • Posts: 162
  • Loc: India-Kolkata

Post 3+ Months Ago

change this part
Code: [ Select ]
$sql = "SELECT id, email_addr FROM $table_name ORDER BY id";
$result = @mysql_query($sql, $connection) or die (mysql_error());
$contact_list = "<ul>";
while ($row = mysql_fetch_array ($result)) {
$id = $row['id'];
$email_addr = $row['email_addr'];
$contact_list .= "<li>$email_addr";
}
$contact_list .= "</ul>";
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<? echo "$contact_list"; ?><br>
  1. $sql = "SELECT id, email_addr FROM $table_name ORDER BY id";
  2. $result = @mysql_query($sql, $connection) or die (mysql_error());
  3. $contact_list = "<ul>";
  4. while ($row = mysql_fetch_array ($result)) {
  5. $id = $row['id'];
  6. $email_addr = $row['email_addr'];
  7. $contact_list .= "<li>$email_addr";
  8. }
  9. $contact_list .= "</ul>";
  10. ?>
  11. <html>
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  14. <? echo "$contact_list"; ?><br>

to something like this
Code: [ Select ]
$sql = "SELECT id, email_addr,f_name,l_name FROM $table_name ORDER BY id";
$result = @mysql_query($sql, $connection) or die (mysql_error());
$contact_list = "<ul>";
while ($row = mysql_fetch_array ($result)) {
$id = $row['id'];
$email_addr = $row['email_addr'];
$contact_list .= "<li>Name:".$row[2]." ".$row[3]." Email Id:".$email_addr."</li>\n";
}
$contact_list .= "</ul>";
?>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<? echo "$contact_list"; ?><br>
  1. $sql = "SELECT id, email_addr,f_name,l_name FROM $table_name ORDER BY id";
  2. $result = @mysql_query($sql, $connection) or die (mysql_error());
  3. $contact_list = "<ul>";
  4. while ($row = mysql_fetch_array ($result)) {
  5. $id = $row['id'];
  6. $email_addr = $row['email_addr'];
  7. $contact_list .= "<li>Name:".$row[2]." ".$row[3]." Email Id:".$email_addr."</li>\n";
  8. }
  9. $contact_list .= "</ul>";
  10. ?>
  11. <html>
  12. <head>
  13. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  14. <? echo "$contact_list"; ?><br>
  • effim
  • Beginner
  • Beginner
  • User avatar
  • Posts: 35
  • Loc: Austin, TX

Post 3+ Months Ago

This is pretty messy code. It's better to get set on the right foot now, so I'll go ahead and rewrite and explain....

Your code, for reference...

Code: [ Select ]
$db_name = "dbname";
$table_name = "mlist";
$connection = @mysql_connect("localhost", "user", "pswd")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT id, email_addr FROM $table_name ORDER BY id";
$result = @mysql_query($sql, $connection) or die (mysql_error());
$contact_list = "<ul>";
while ($row = mysql_fetch_array ($result)) {
$id = $row['id'];
$email_addr = $row['email_addr'];
$contact_list .= "<li>$email_addr";
}
$contact_list .= "</ul>";
?>
 
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<? echo "$contact_list"; ?><br>
  1. $db_name = "dbname";
  2. $table_name = "mlist";
  3. $connection = @mysql_connect("localhost", "user", "pswd")
  4. or die(mysql_error());
  5. $db = @mysql_select_db($db_name, $connection) or die(mysql_error());
  6. $sql = "SELECT id, email_addr FROM $table_name ORDER BY id";
  7. $result = @mysql_query($sql, $connection) or die (mysql_error());
  8. $contact_list = "<ul>";
  9. while ($row = mysql_fetch_array ($result)) {
  10. $id = $row['id'];
  11. $email_addr = $row['email_addr'];
  12. $contact_list .= "<li>$email_addr";
  13. }
  14. $contact_list .= "</ul>";
  15. ?>
  16.  
  17. <html>
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  20. <? echo "$contact_list"; ?><br>


Code: [ Select ]
<?
 
// First off, no sense creating variables to house information
// you're going to use once and never change
 
// Don't EVER use the @ to suppress error output. You're just
// making debugging work harder on yourself. Instead, turn
// error reporting off http://us2.php.net/manual/en/function.ini-set.php
// and you really should be handling the errors when you get to that level.
 
$conn = mysql_connect('localhost', 'user', 'password');
mysql_select_db('database', $connection);
 
$query = "SELECT id, email_addr FROM tablename ORDER BY id";
$result = mysql_query($query, $connection);
 
$contact_list = "<table><tr><th>ID</th><th>First Name</th><th>Last Name</th><th>Email</th></tr>";
 
// use "false !==" to verify that the row was fetched
// to verify that it is actually 'false' and not '' or 0
// also, use fetch_object to make things simpler on your
// eyes and avoid polluting the global variable pool
 
while (false !== ($row = mysql_fetch_object($result))) {
 
    // always use proper indentation, and make sure you
    // properly close your tags
    $contact_list .= "<tr><td>$row->id</td><td>$row->f_name</td><td>$row->l_name</td><td>$row->$email_addr</td></tr>";
 
}
 
$contact_list .= "</table>";
 
?>
 
<html>
<head>
    <title>Use a title</title>
 
    <!-- Get in the habit of using utf-8 and avoid character-encoding issues -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
 
<!-- make sure you're using body tags and not putting your text in the head -->
<body>
 
    <!-- No need for the echo since <?= does the same thing. Also, you
    don't have to put double-quotes around your variables unless they're
    numbers and you need to explicitly convert them to strings...and even
    then that's not the most ideal way. -->
    <?=$contact_list?>
    <br>
</body>
</html>
 
 
  1. <?
  2.  
  3. // First off, no sense creating variables to house information
  4. // you're going to use once and never change
  5.  
  6. // Don't EVER use the @ to suppress error output. You're just
  7. // making debugging work harder on yourself. Instead, turn
  8. // error reporting off http://us2.php.net/manual/en/function.ini-set.php
  9. // and you really should be handling the errors when you get to that level.
  10.  
  11. $conn = mysql_connect('localhost', 'user', 'password');
  12. mysql_select_db('database', $connection);
  13.  
  14. $query = "SELECT id, email_addr FROM tablename ORDER BY id";
  15. $result = mysql_query($query, $connection);
  16.  
  17. $contact_list = "<table><tr><th>ID</th><th>First Name</th><th>Last Name</th><th>Email</th></tr>";
  18.  
  19. // use "false !==" to verify that the row was fetched
  20. // to verify that it is actually 'false' and not '' or 0
  21. // also, use fetch_object to make things simpler on your
  22. // eyes and avoid polluting the global variable pool
  23.  
  24. while (false !== ($row = mysql_fetch_object($result))) {
  25.  
  26.     // always use proper indentation, and make sure you
  27.     // properly close your tags
  28.     $contact_list .= "<tr><td>$row->id</td><td>$row->f_name</td><td>$row->l_name</td><td>$row->$email_addr</td></tr>";
  29.  
  30. }
  31.  
  32. $contact_list .= "</table>";
  33.  
  34. ?>
  35.  
  36. <html>
  37. <head>
  38.     <title>Use a title</title>
  39.  
  40.     <!-- Get in the habit of using utf-8 and avoid character-encoding issues -->
  41.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  42. </head>
  43.  
  44. <!-- make sure you're using body tags and not putting your text in the head -->
  45. <body>
  46.  
  47.     <!-- No need for the echo since <?= does the same thing. Also, you
  48.     don't have to put double-quotes around your variables unless they're
  49.     numbers and you need to explicitly convert them to strings...and even
  50.     then that's not the most ideal way. -->
  51.     <?=$contact_list?>
  52.     <br>
  53. </body>
  54. </html>
  55.  
  56.  

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 97 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.