Help with displaying MySQL query results

Help with displaying MySQL query results

am 14.11.2007 12:29:15 von LLOYD

I'm very new to PHP and attempting to put together a simple script for
retrieving MySQL data of personal records.

The MySQL table I'm using consists of:

0: id
1: name
2: location (an integer relating to a separate table of locations).
3: details

I want the script to list results where, for example, name has a
location of 4.

Each result should be displayed in it's own paragraph.

Each result should be within a link to open a new page (details.php ?)
that will retrieve the details for that name.

Ideally, but not essentially, the new page should contain a link back to
the previous results.

I've managed to cobble together something displays the initial results
but, despite trawling Google, haven't managed to suss out the rest.
(It's ugly and insecure so probably best to start from scratch.)

Any pointers gratefully received.

LH

--
No sig regrets.

Re: Help with displaying MySQL query results

am 14.11.2007 12:50:16 von Courtney

Lloyd Harold wrote:
> I'm very new to PHP and attempting to put together a simple script for
> retrieving MySQL data of personal records.
>
> The MySQL table I'm using consists of:
>
> 0: id
> 1: name
> 2: location (an integer relating to a separate table of locations).
> 3: details
>
> I want the script to list results where, for example, name has a
> location of 4.
>

$query="select * from mytable where location ='4'";
$result=mysql_query($query);
> Each result should be displayed in it's own paragraph.
>
if ($result && ($rows=myql_numrows($result) >0))
{
> Each result should be within a link to open a new page (details.php ?)
> that will retrieve the details for that name.
>
for($i=0;$;<$rows;$i++)
{
> Ideally, but not essentially, the new page should contain a link back to
> the previous results.
>
$last_id=$__GET['id'];
$var1=mysql_result($result,'id); // and any other variables you
want.
printf("

\r\n",
$last_id,$var1);
}
}

> I've managed to cobble together something displays the initial results
> but, despite trawling Google, haven't managed to suss out the rest.
> (It's ugly and insecure so probably best to start from scratch.)
>
> Any pointers gratefully received.
>
> LH
>

Re: Help with displaying MySQL query results

am 14.11.2007 13:50:29 von LLOYD

The Natural Philosopher wrote:

> $query="select * from mytable where location ='4'";
> $result=mysql_query($query);
> > Each result should be displayed in it's own paragraph.
> >
> if ($result && ($rows=myql_numrows($result) >0))
> {
> > Each result should be within a link to open a new page (details.php ?)
> > that will retrieve the details for that name.
> >
> for($i=0;$;<$rows;$i++)
> {
> > Ideally, but not essentially, the new page should contain a link back to
> > the previous results.
> >
> $last_id=$__GET['id'];
> $var1=mysql_result($result,'id); // and any other variables you
> want.
> printf("

\r\n",
> $last_id,$var1);
> }
> }

Thanks for your prompt response.

I've tried the code and am seeing this error:

PHP Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /- on
line 25, which is:

for($i=0;$;<$rows;$i++)

Re: Help with displaying MySQL query results

am 14.11.2007 14:31:06 von Jerry Stuckle

Lloyd Harold wrote:
> I'm very new to PHP and attempting to put together a simple script for
> retrieving MySQL data of personal records.
>
> The MySQL table I'm using consists of:
>
> 0: id
> 1: name
> 2: location (an integer relating to a separate table of locations).
> 3: details
>
> I want the script to list results where, for example, name has a
> location of 4.
>
> Each result should be displayed in it's own paragraph.
>
> Each result should be within a link to open a new page (details.php ?)
> that will retrieve the details for that name.
>
> Ideally, but not essentially, the new page should contain a link back to
> the previous results.
>
> I've managed to cobble together something displays the initial results
> but, despite trawling Google, haven't managed to suss out the rest.
> (It's ugly and insecure so probably best to start from scratch.)
>
> Any pointers gratefully received.
>
> LH
>

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
\n";
else {
while($data = mysql_fetch_array($result)) {
// $data is an array with elements ['id'], ['name'] and ['details']
echo
"

\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).

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

Re: Help with displaying MySQL query results

am 14.11.2007 15:23:54 von LLOYD

Jerry Stuckle 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
\n";
> else {
> while($data = mysql_fetch_array($result)) {
> // $data is an array with elements ['id'], ['name'] and ['details']
> echo
> "

\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

"

\n";

Re: Help with displaying MySQL query results

am 14.11.2007 16:41:28 von Bucky Kaufman

"Lloyd Harold" wrote in message
news:1i7knn8.xncn8ogzxqv9N%lloyd@harold.invalid...

> I've tried the code and am seeing this error:
>
> expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in
>
> "

\n";

You gotta enclose complex variables like "$data['id']" in curly-brackets
when you use them inside a string like that.

Thus, it should be:
"

\n";

Re: Help with displaying MySQL query results

am 14.11.2007 16:53:21 von Courtney

Lloyd Harold wrote:
> The Natural Philosopher wrote:
>
>> $query="select * from mytable where location ='4'";
>> $result=mysql_query($query);
>>> Each result should be displayed in it's own paragraph.
>>>
>> if ($result && ($rows=myql_numrows($result) >0))
>> {
>>> Each result should be within a link to open a new page (details.php ?)
>>> that will retrieve the details for that name.
>>>
>> for($i=0;$;<$rows;$i++)
>> {
>>> Ideally, but not essentially, the new page should contain a link back to
>>> the previous results.
>>>
>> $last_id=$__GET['id'];
>> $var1=mysql_result($result,'id); // and any other variables you
>> want.
>> printf("

\r\n",
>> $last_id,$var1);
>> }
>> }
>
> Thanks for your prompt response.
>
> I've tried the code and am seeing this error:
>
> PHP Parse error: parse error, expecting `T_VARIABLE' or `'$'' in /- on
> line 25, which is:
>
> for($i=0;$;<$rows;$i++)
theres missing i in there. Sorry. I don't debug the typos for free ;-)

Re: Help with displaying MySQL query results

am 14.11.2007 16:54:05 von Courtney

Sanders Kaufman wrote:
> "Lloyd Harold" wrote in message
> news:1i7knn8.xncn8ogzxqv9N%lloyd@harold.invalid...
>
>> I've tried the code and am seeing this error:
>>
>> expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in
>>
>> "

\n";
>
> You gotta enclose complex variables like "$data['id']" in curly-brackets
> when you use them inside a string like that.
>
> Thus, it should be:
> "

\n";
>
>

or use backlslashes...

Re: Help with displaying MySQL query results

am 14.11.2007 18:35:39 von Bucky Kaufman

"The Natural Philosopher" wrote in message
news:1195055646.25647.2@demeter.uk.clara.net...
> Sanders Kaufman wrote:

>> You gotta enclose complex variables like "$data['id']" in curly-brackets
>> when you use them inside a string like that.
>>
>> Thus, it should be:
>> "

\n";
>
> or use backlslashes...

Wouldn't that escape the $, rather than enclose the variable?

Re: Help with displaying MySQL query results

am 14.11.2007 18:47:32 von Jerry Stuckle

Lloyd Harold wrote:
> Jerry Stuckle 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
\n";
>> else {
>> while($data = mysql_fetch_array($result)) {
>> // $data is an array with elements ['id'], ['name'] and ['details']
>> echo
>> "

\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
>
> "

\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

"

\n";

since it's an array...

Or, you could do use this instead:

'

\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.

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

Re: Help with displaying MySQL query results

am 14.11.2007 21:48:13 von LLOYD

Jerry Stuckle wrote:

> Lloyd Harold wrote:
> > Jerry Stuckle 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
\n";
> >> else {
> >> while($data = mysql_fetch_array($result)) {
> >> // $data is an array with elements ['id'], ['name'] and ['details']
> >> echo
> >> "

\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
> >
> > "

\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
>
> "

\n";
>
> since it's an array...
>
> Or, you could do use this instead:
>
> '

\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
\n";

else {

while ($data = mysql_fetch_array($result))

{

echo
"

\n";

}

}

// managed to debug the missing final brace for myself! :)

Re: Help with displaying MySQL query results

am 14.11.2007 22:15:16 von Jerry Stuckle

Lloyd Harold wrote:
> Jerry Stuckle wrote:
>
>> Lloyd Harold wrote:
>>> Jerry Stuckle 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
\n";
>>>> else {
>>>> while($data = mysql_fetch_array($result)) {
>>>> // $data is an array with elements ['id'], ['name'] and ['details']
>>>> echo
>>>> "

\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
>>>
>>> "

\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
>>
>> "

\n";
>>
>> since it's an array...
>>
>> Or, you could do use this instead:
>>
>> '

\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
\n";
>
> else {
>
> while ($data = mysql_fetch_array($result))
>
> {
>
> echo
> "

\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
==================