1

I have one table which contains events and dates, and another which contains the guestlist for each of these nights. I can print the nights like so:

$day = date("l");
$date = getFullDateString($day);

$result = mysql_query("SELECT * FROM nights WHERE day = '$day' AND promoter = 'blah'") or die(mysql_error);

while ($row = mysql_fetch_array($result)) {
    echo "<h2>";
    echo $row[name];
    echo " (";
    echo getFullDateString($row[day]);
    echo ")</h2>";
}

The problem is that for each of these events I want the guestlist to be printed below it. I've tried putting another while loop inside, but even the test echo doesn't work.

$result2 = mysql_query("SELECT DISTINCT guest FROM guestlists WHERE night = '$row[name]' AND date = 'getFullDateString($row[day])'") or die(mysql_error ());

    while($row2 = mysql_fetch_array($result2)) {
        echo $row2[0];
        echo "test";
        echo "<br />";
}

Essentially I want the following:

Event 1

Name 1

Name 2

Name 3

Event 2

Name 1

Name 2

Name 3

2
  • are you having event id in guestlist table? Jun 16, 2012 at 9:50
  • does the table have entries ?
    – avk
    Jun 16, 2012 at 9:53

3 Answers 3

1

You have a method inside the query string which won't be run.

Try:

$sql = "SELECT DISTINCT guest FROM guestlists WHERE night = '".$row['name']."' AND `date` = '".getFullDateString($row['day'])."'"

Also, date is a reserved word in MySQL and should be encapsulated within backticks or changed to something else.

Additionally, the mysql_ functions are being deprecated and it is recommended to replace them with PDO or MySQLi.

1

Take a look at Complex (curly) syntax for string parsing to learn the proper way to embed your variables in the string.

0

Try this...

$result = mysql_query("SELECT * FROM nights WHERE day = '".$day."' AND promoter = 'blah'") or die(mysql_error);

while ($row = mysql_fetch_array($result)) {
    echo "<h2>".$row[cname]."</h2>";

    $result2 = mysql_query("SELECT DISTINCT guest FROM guestlists WHERE night = '$row[name]' AND date = '".getFullDateString($row[day])."'") or die(mysql_error);

   while ($row2 = mysql_fetch_array($result2))  
    {
        echo $row2[cname];  
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.