parsing MySQL query results

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I am very new to php/MySQL and know this question has been answered
before (I can't seem to locate it however).

I created a function that pull data from a MySQL db.  I use the
returned values using php to generate my html page.  The question
being that the qeury returns 2 values.  right now I simply loop
through all the values and output them sequentially.  However, I need
more control over these values.  How can I call each value
independantly (instead of looping)?

function getEvents($yearno,$monthno,$dayno) {
 $evnts = array();
 $sql = mysql_query("SELECT event_start_time, event_title ".
                    "FROM events ".
                  "WHERE event_date = '$yearno-$monthno-$dayno' ORDER BY
while ($row = mysql_fetch_assoc($sql)) array_push($evnts,$row);
return $evnts;

Then I use it in php as follows:
$events = getEvents($year,month_number($month),getDayofMonth($curdt));
foreach ($events as $key => $value) {
   echo $key['iId'] . "<br/>";
   foreach ($value as $ikey => $ivalue) {
      echo $ivalue ."  \n";

Instead of looping through each $ivalue, I am looking for a way to
call the 'event_start_time' and 'event_title' as I need the to
appear.  i am trying to build an a href statement with this

Thank you,


Re: parsing MySQL query results

Daniel wrote:
Quoted text here. Click to load it

In your first foreach loop, $value is an associative array, with the
keys being the column names. So you can dispose of the second foreach
loop and:

    echo $value['event_start_time'];

Matt M.

Re: parsing MySQL query results


Quoted text here. Click to load it
Quoted text here. Click to load it

Euhm, doesn't that always just return only "<br/>"?

<do not pay attention to my rambling is the following doesn't make sense=

to you?

$key is an integer, or should be as this is the current code. Only if it=

gets cast to a string by the foreach function (which it doesn't AFAIK), =
so  =

I'm going off on a ramble here: Would it be a string (which is isn't) th=
e  =

[] makes some kind of sense: the nth character in the string. It only  =

takes a number, so any endresult between the [] is cast to it (usually 0=

if fed a string). That would mean the first character.

So that, boys and girls, is what happens if you accidentaly lose track o=
f  =

wether your variable is a string or an array. Particularly nasty when  =

checking for wether some important key which controls further flow is se=

$var = 'You think this is an array.';
if(isset($var['important key'])) echo 'my important key is  =

'.$var['important key'];
my important key is Y

</ok, now pay attention again>

Quoted text here. Click to load it

    echo $value['event_start_time'], $value['event_title']

Quoted text here. Click to load it

See above, it's in the $value array, var_dump($value) to check what a  =

variable holds while debugging.
-- =

Rik Wasmus

Site Timeline