You are here: Re: Help with displaying MySQL query results « PHP Programming Language « IT news, forums, messages
Re: Help with displaying MySQL query results

Posted by Jerry Stuckle on 11/14/07 21:15

Lloyd Harold wrote:
> Jerry Stuckle <jstucklex@attglobal.net> wrote:
>
>> Lloyd Harold wrote:
>>> Jerry Stuckle <jstucklex@attglobal.net> wrote:
>>>
>>>> Hi, Lloyd,
>>>>
>>>> Sounds like a homework question?
>>>>
>>>> This isn't too bad, once you get the hang of it.
>>>>
>>>> First you need to execute the mysql query. Then, in a loop, retrieve
>>>> each result and do what you want with it. For instance (error checking
>>>> left out for clarity, but you should be checking the result of every
>>>> MySQL call except the mysql_fetch_array()):
>>>>
>>>>
>>>> $location = 4; // Assumed to be passed from somewhere & validated
>>>>
>>>> $link = mysql_connect('localhost', 'userid', 'password');
>>>> mysql_select_db('mydb');
>>>>
>>>> $result = mysql_select("SELECT id, name, details FROM mytable WHERE
>>>> location=$location"); // Sorry for the wrapping
>>>> if (mysql_num_rows($result) == 0)
>>>> echo "No results found<br>\n";
>>>> else {
>>>> while($data = mysql_fetch_array($result)) {
>>>> // $data is an array with elements ['id'], ['name'] and ['details']
>>>> echo
>>>> "<p><a href='/details.php?id=$data['id']'>$data['name']</a></p>\n";
>>>> // You could also display the details, here. Or, if you're not going to
>>>> // display the details on this page, just leave them out of the query
>>>> }
>>>>
>>>> Then on your details.php page, retrieve the data for the specific ID you
>>>> want, and display them with code similar to the above (although you
>>>> won't need a loop as you'll only have one result).
>>> Thanks for your help and encouragement, Jerry.
>>>
>>> I've tried the code and am seeing this error:
>>>
>>> expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in
>>>
>>> "<p><a href='/details.php?id=$data['id']'>$data['name']</a></p>\n";
>>>
>> It should be all one line, with the echo in front of it (split because
>> of my line length limits).
>>
>> And I was wrong - it should be
>>
>> "<p><a href='/details.php?id={$data['id']}'>{$data['name']}</a></p>\n";
>>
>> since it's an array...
>>
>> Or, you could do use this instead:
>>
>> '<p><a href="/details.php?id=' . $data['id'] . '>' . $data['name'] .
>> "</a></p>\n";
>>
>> But you should know enough about PHP to be able to debug minor syntax
>> errors like this. In the newsgroups we (almost) never guarantee our
>> code to be perfect.
>
> Thanks for pursuing this for me, and to all others who've contributed.
> The code is now working fine. Not sure why, but I needed to add another
> variable for the query result, making:
>
> $location = 4;
>
> $link = mysql_connect('localhost', 'userid', 'password');
> mysql_select_db('mydb');
>
> $query = "SELECT id, name, details FROM mytable WHERE
> location=$location");
>
> $result = mysql_query($query);
>
> if (mysql_num_rows($result) == 0)
> echo "No results found<br>\n";
>
> else {
>
> while ($data = mysql_fetch_array($result))
>
> {
>
> echo
> "<p><a href='/details.php?id={$data['id']}'>{$data['name']}</a></p>\n";
>
> }
>
> }
>
> // managed to debug the missing final brace for myself! :)
>
>
>

The extra variable is because typically you would want different
locations, based on some passed parameter. The $location variable is a
"stand-in" for that parameter. If you're always going to use
location=4, then you don't need it.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация