PHP variables
- Started
- Last post
- 12 Responses
- seed
What is the best way to output a single variable from SQL in php? For instance, I have a loop outputting records and I want one value to only appear as a title - not in each record. Should I make a separate query to output that. What function should I use instead of mysql_fetch_array?
- moth0
In the loop, do something like;
$myVar = $sql['title']..
not sure i understand the question though.
- moth0
is the value common to every result row?
- seed0
Yes its a date but I only want the date to show up at the top of the page.
- seed0
What function do you normally use other than mysql_fetch_array to output a single variable in any case?
- acescence0
mysql_fetch_row
- seed0
Thanks. So I will have to make a separate query to output that one variable?
- ldww0
no. just output the first row in the array.
where array = the result from sql
echo $array[0]['date_field'];
- seed0
Thanks a lot.
- seed0
Sorry I am a PHP noob.
How can I access $e[0]['event_date']; (as mentioned in the prev post) before the while loop? I want to place it before the while loop but the fetch_array hasn't started yet.
while ($e = mysql_fetch_array($sql)) {
echo "$e[event_date]\n";
echo "$e[event_title]\n";
echo "$e[event_desc]\n";
}
- neverblink0
you can't, do the following:
while ($e = mysql_fetch_array($s ql)) {
$date = $e[event_date] . "\n";
$content .= $e[event_title]."\n" . $e[event_desc] . "\n";}
echo $date;
echo $content;
- ldww0
you should really have a db wrapper which you send a query to and it returns you an array.
not actually output directly from the fetch
- seed0
ok, I'll try that. neverblinks code worked. thanks.
The event_date variable normally outputs 2007-04-19 but with this code $e[0]['event_date'] it outputs the value of 5. Any idea why?