|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 > |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
The Natural Philosopher <a@b.c> 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("<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++) |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. jstucklex@attglobal.net ================== |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 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"; |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
"Lloyd Harold" <lloyd@harold.invalid> 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 > > "<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"; |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Lloyd Harold wrote:
> The Natural Philosopher <a@b.c> 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("<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 ;-) |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Sanders Kaufman wrote:
> "Lloyd Harold" <lloyd@harold.invalid> 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 >> >> "<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... |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
"The Natural Philosopher" <a@b.c> 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: >> "<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? |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
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 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. jstucklex@attglobal.net ================== |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
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 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! ![]() |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
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 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 ================== |
|
![]() |
| Outils de la discussion | |
|
|