473,385 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Help with displaying MySQL query results

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
10 2623
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
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
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
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Erich Trowbridge | last post by:
Has anybody seen this tool? It is awesome. check out http://vw.vermeer.org/ . It's a php front end for large-scale syslog deployments. It makes managing syslog in large networks a snap. The idea...
5
by: Dariusz | last post by:
I have PHP code (below) which reads data from a MySQL format database. The problem I am having is trying to find out when the last ID entry was made. When the script is executed, the $gbID is...
7
by: Robert | last post by:
I have a php/mysql query working like so: $Query = "SELECT * FROM $TableName WHERE name LIKE '%".$searchterm."%' " All I want to do now is sort them alphabetically. By using the above...
2
by: jaysonsch | last post by:
Hello! I am having some problems with a database query that I am trying to do. I am trying to develop a way to search a database for an entry and then edit the existing values. Upon submit, the...
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
7
by: Christo | last post by:
OK. I have a mysql database and i have it displaying whats currently there using php, the problem is that I cannot insert into the database correctly. please check my code below <?php...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
5
by: deaconj999 | last post by:
Hi, I have nearly finished my database and I would like to add a query that uses a combo box to get the results, not the usual paramater style input. I suppose it would need a form and a query...
2
by: dmstn | last post by:
Hey! I've got a little problem. I have to make a web site for a university essay. I curently have to create a search engine. Users can enter a hotel name in a search bar and results have to appear in...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.