teknospr Posted March 6, 2011 Share Posted March 6, 2011 Good day: Im trying to assign a variable to each link to get passed to another page. The variable is selected from a table in the database, and as each link is generated it has the proper variable and the next page can retrieve the correct articles type from the database. The code is not showing an error but it is not sending the variable to the other page "or the next page is not retrieving it. This is the code for both pages, the one that creates the links with the variable and the page that supposed to retrieve the articles from the database and display it. <?php $connection = mysql_connect("localhost", "uername", "password"); mysql_select_db("articles", $connection); $query="SELECT name, articlesid FROM artiles_description WHERE letter='a' AND place=1"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close();; ?> <table width="300" border="0" cellspacing="2" cellpadding="2"> <tr> <th><font face="Arial, Helvetica, sans-serif">A</font></th> </tr> <?php $f3='articles.php'; $i=0; while ($i < $num) { $backgroundclass = (($i % 2) == 0) ? 'gray' : 'white'; $f1=mysql_result($result,$i,"name"); $f2=mysql_result($result,$i,"articlesid") ?> <tr class='<?php echo $backgroundclass ?>'> <td align="center"><font face="Arial, Helvetica, sans-serif"><?php echo "<a href=\"$f3\"?id='$f2' target=\"_self\">$f1</a>"; ?></font></td> </tr> </td> <?php $i++; } ?> </table> <td> and the second page <html> <head> <title>Articles</title> <style> .gray { backgroundcolor: gray; } .white { backgroundcolor: white; } </style> </head> <body> <?php $_GET[‘aid’]; $connection = mysql_connect("localhost", "username", "password"); mysql_select_db("articles", $connection); $query="SELECT * FROM articles WHERE id='aid'"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); ?> <table border="1" cellspacing="2" cellpadding="2"> <tr> <th><font face="Arial, Helvetica, sans-serif">Article</font></th> <th><font face="Arial, Helvetica, sans-serif">Year</font></th> <th><font face="Arial, Helvetica, sans-serif">Description</font></th> <th><font face="Arial, Helvetica, sans-serif">Location</font></th> </tr> <?php $i=0; while ($i < $num) { $backgroundclass = (($i % 2) == 0) ? 'gray' : 'white'; $f1=mysql_result($result,$i,"article"); $f2=mysql_result($result,$i,"year"); $f3=mysql_result($result,$i,"description"); $f4=mysql_result($result,$i,"location"); $f5=mysql_result($result,$i,"link") ?> <tr class='<?php echo $backgroundclass ?>'> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f1; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f2; ?></font></td> <td><font face="Arial, Helvetica, sans-serif"><?php echo $f3; ?></font></td> <td ><font face="Arial, Helvetica, sans-serif"><?php echo "<a href=\"$f5\" target=\"_blank\">$f4</a>"; ?></font></td> </tr> <tr> </tr> <?php $i++; } ?> </body> </html> Any help will be appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/229770-passing-and-getting-a-variable/ Share on other sites More sharing options...
QuickOldCar Posted March 6, 2011 Share Posted March 6, 2011 I just took a fast peek, could it be that you are closing mysql too early and losing your results. Quote Link to comment https://forums.phpfreaks.com/topic/229770-passing-and-getting-a-variable/#findComment-1183548 Share on other sites More sharing options...
QuickOldCar Posted March 6, 2011 Share Posted March 6, 2011 I just looked again, usually you assign a variable to the get and use that variable into your select statement. be sure you have the correct single quotes as well, also escape bad characters from any input $aid = mysql_real_escape_string($_GET['aid']); $query="SELECT * FROM articles WHERE id='$aid'"; Quote Link to comment https://forums.phpfreaks.com/topic/229770-passing-and-getting-a-variable/#findComment-1183553 Share on other sites More sharing options...
teknospr Posted March 6, 2011 Author Share Posted March 6, 2011 I got this error messages: Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Can't connect to local MySQL server through socket Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in Quote Link to comment https://forums.phpfreaks.com/topic/229770-passing-and-getting-a-variable/#findComment-1183560 Share on other sites More sharing options...
QuickOldCar Posted March 6, 2011 Share Posted March 6, 2011 Open the connection before the get. So move $aid = mysql_real_escape_string($_GET['aid']); after your $connection Quote Link to comment https://forums.phpfreaks.com/topic/229770-passing-and-getting-a-variable/#findComment-1183582 Share on other sites More sharing options...
QuickOldCar Posted March 6, 2011 Share Posted March 6, 2011 One more thing, Pass your variable in a form link like so <form method=GET action='http://www.yoursite.com/script.php'> Use your values of course. Quote Link to comment https://forums.phpfreaks.com/topic/229770-passing-and-getting-a-variable/#findComment-1183584 Share on other sites More sharing options...
teknospr Posted March 6, 2011 Author Share Posted March 6, 2011 Stll getting the same message, no matter where I place the argument. Quote Link to comment https://forums.phpfreaks.com/topic/229770-passing-and-getting-a-variable/#findComment-1183585 Share on other sites More sharing options...
QuickOldCar Posted March 6, 2011 Share Posted March 6, 2011 Odd, well remove the $aid = mysql_real_escape_string($_GET['aid']); and make it $aid = $_GET['aid']; see if can connect Quote Link to comment https://forums.phpfreaks.com/topic/229770-passing-and-getting-a-variable/#findComment-1183590 Share on other sites More sharing options...
teknospr Posted March 6, 2011 Author Share Posted March 6, 2011 It connects, but the variable is not passing from the first page. The variable has to be assigned from the database into the links as the articles hyperlinks are created. That way when the user clicks on the hyperlink, it goes to a general format page but retrieves the information according to that variable that was passed. Quote Link to comment https://forums.phpfreaks.com/topic/229770-passing-and-getting-a-variable/#findComment-1183634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.