0

I'm trying to store the title(summary) and date(created) of an event in an array. But I think im missing something in my loop.

<?php
$summary = array();
$date = array();

mysql_connect('mysql.server', 'myUsername', 'myPass') or die('Could not connect: ' . mysql_error());
mysql_select_db("mxgsite") or die(mysql_error());
$query_summary = mysql_query('SELECT summary FROM event_info') or die(mysql_error());
$query_date = mysql_query('SELECT created FROM event_details') or die(mysql_error());

$row_summary = mysql_fetch_array($query_summary);
$row_date = mysql_fetch_array($query_date);

$i = 0;
while(($row1 = mysql_fetch_array($query_summary))) {
    $row2 = mysql_fetch_array($query_date);
    $summary[] = $row['summary'];
    $date[] = $row['created'];
    echo $summary[$i] . " " . $date[$i] . "<br ?>";
    $i++;
}

I know i'm getting values because I can echo out 1 value, but if I want to put all the values in an array and try to echo out that array I keep getting blank values?

5
  • I think your bigger problem is that there's no guarantee whatsoever that row x from the event_info table has anything to do with row x from the event_details table, no? Commented Apr 26, 2012 at 0:05
  • they use event_id as it's shared key? Commented Apr 26, 2012 at 0:07
  • Agree with deceze. If the 2 tables can be related by an "eventid" or something of that nature, you're better off doing a join and getting summary and created in 1 query. That way, you can then store and work with them as linked pairs. Commented Apr 26, 2012 at 0:07
  • @Howdy You're not using that event_id anywhere in your query though, so you're just getting random rows. Commented Apr 26, 2012 at 0:08
  • :S i'm going to have to search joins then. I wanted them in 2 separate arrays though. Commented Apr 26, 2012 at 0:12

1 Answer 1

4

It seems to me like you are trying to do too many things here. Since the 2 sets of values are not being stored in a way where they are related/linked to each other, you might as well deal with them in separate while loops. Try something like this:

 while ($row = mysql_fetch_array($query_summary)){
     $summary[] = $row[0];
 }
 while ($row = mysql_fetch_array($query_date)){
     $date[] = $row[0];
 }

If you want to relate the tables, per the comments above, you can try something more like:

$result = mysql_query('SELECT a.eventid, a.summary, b.created 
                         FROM event_info a 
                         join event_details b 
                         on a.eventid = b.eventid');

$events = array();

while ($row = mysql_fetch_array($result)){
    $event = array();
    foreach ($row as $key=>$value){
      $event[$key]=$value;
    }
    $events[] = $event;
}
Sign up to request clarification or add additional context in comments.

1 Comment

+1, except for that inner loop. You're just copying an array into another array, which is unnecessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.