Jump to content


Making A Query Loop...

  • Please log in to reply
2 replies to this topic

#1 northerncomfort

  • Members
  • Pip
  • Newbie
  • 5 posts

Posted 12 June 2006 - 02:32 PM

I've encountered a bit of a problem with my contacts database. I'm building a contacts database for my office, which is working pretty nicely so far. The actual contacts part is working perfectly, and is based in mySQL. However, I've also added a "My Contacts" as this was a big request in the office. It uses a simple login, takes the name, creates a table in the database called $username_contacts, etc. Adding a contact to the mycontacts table has been working nicely so far, but I'm having a problem with listing the "My Contacts" part that hopefully I can find some help here with.

Basically, each contact in the main contacts table has an id number. The $username_contacts table keeps track of who you have in your contacts list by the id number.

Here is the code in question. It first checks to make sure you're logged in, then puts your username as $name and grabs the correct table. Then it sees how many contacts you have in my contacts (this is where the script starts to flounder), and for each contact you have in my contacts, it grabs the proper contact info from the main contacts table. However, that isn't working so nicely at the moment, and hopefully somebody here with a beady eye will be able to spot the problem.

Also, is there any way to keep each contact email address in a different variable so I could have an option to display a table of the email addresses so that the folks in the office could quickly email all of "my contacts" at once?

echo "My Contacts";
include "database.php";
$name = $_SESSION['username'];
@mysql_select_db($database) or die( "Unable to select database");
$sql2 = "SELECT * FROM ".$name."_contacts";
$result2 = mysql_query($sql2);
$i = 1;
while ($i <= $num){
$toget = mysql_result($result2,$i,"contact_id");
$sql = "select * from contacts where id='".$toget."'";
$result = mysql_query($sql);
echo "All the usual contact table stuff goes here, it works fine but is bulky so I editted it out for your sake...";
   echo "You are not logged in. You need to be logged in to use My Contacts.<br /><a href=\"index.php\">Log In</a>";

#2 joquius

  • Members
  • PipPipPip
  • Advanced Member
  • 319 posts

Posted 12 June 2006 - 03:13 PM

first off, you wanna change that long list of mysql_results to this

while ($data = mysql_fetch_array ($result))
$field = $data['field']; // and all other stuff which was in the for() loop

actually...should look like this:

if ($logged_in)
echo "My Contacts";

include "database.php";

$name = $_SESSION['username'];

mysql_connect ($connect, $username, $password);
mysql_select_db ($database) or die( "Unable to select database");

$sql = "SELECT * FROM ".$name."_contacts";
$result = mysql_query ($sql) or die (mysql_error());
while ($data = mysql_fetch_array ($result))
    $first = $data['firstname']; //etc
   echo "You are not logged in. You need to be logged in to use My Contacts.<br /><a href=\"index.php\">Log In</a>";


#3 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 12 June 2006 - 03:26 PM

$sql2 = "SELECT * FROM ".$name."_contacts";
$result2 = mysql_query($sql2);

$all_contacts = array();
while ($row = mysql_fetch_assoc($result2)) { 
    $all_contacts[] = $email;
   // put all email addresses into comma-delimited string
$to_all = join (',' , $all_contacts);

Why a separate table per user? Add username column then

SELECT * FROM contacts WHERE username = '$name';
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users