By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,060 Members | 1,906 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,060 IT Pros & Developers. It's quick & easy.

Help with displaying MySQL query results

P: n/a
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.
Nov 14 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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("<p><A HREF=Mycode.php?id=%d>id=%d,</A>\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
Nov 14 '07 #2

P: n/a
The Natural Philosopher <a@b.cwrote:
$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("<p><A HREF=Mycode.php?id=%d>id=%d,</A>\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++)
Nov 14 '07 #3

P: n/a
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<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).

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

Nov 14 '07 #4

P: n/a
Jerry Stuckle <js*******@attglobal.netwrote:
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";
Nov 14 '07 #5

P: n/a
Lloyd Harold wrote:
The Natural Philosopher <a@b.cwrote:
>$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("<p><A HREF=Mycode.php?id=%d>id=%d,</A>\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 ;-)
Nov 14 '07 #6

P: n/a
Sanders Kaufman wrote:
"Lloyd Harold" <ll***@harold.invalidwrote in message
news:1i*************************@harold.invalid...
>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";

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

Thus, it should be:
"<p><a href='/details.php?id={$data['id']}'>{$data['name']}</a></p>\n";

or use backlslashes...
Nov 14 '07 #7

P: n/a
"The Natural Philosopher" <a@b.cwrote in message
news:11****************@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:
"<p><a href='/details.php?id={$data['id']}'>{$data['name']}</a></p>\n";

or use backlslashes...
Wouldn't that escape the $, rather than enclose the variable?
Nov 14 '07 #8

P: n/a
Lloyd Harold wrote:
Jerry Stuckle <js*******@attglobal.netwrote:
>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.

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

Nov 14 '07 #9

P: n/a
Jerry Stuckle <js*******@attglobal.netwrote:
Lloyd Harold wrote:
Jerry Stuckle <js*******@attglobal.netwrote:
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! :)
Nov 14 '07 #10

P: n/a
Lloyd Harold wrote:
Jerry Stuckle <js*******@attglobal.netwrote:
>Lloyd Harold wrote:
>>Jerry Stuckle <js*******@attglobal.netwrote:

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.
js*******@attglobal.net
==================

Nov 14 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.