maverick3d Posted July 7, 2008 Share Posted July 7, 2008 I am wanting to create a dynamic hyperlink using all the ID's in my Database Table which when clicked on the link will load a dynamic page with all the details of the selected ID. Wondering if someone could point out the flaw in my script. I am still pretty novice, c&c welcome: <?PHP $host="localhost"; $username="user"; $password="pass"; $conn = mysql_connect($host,$username,$password) or die(mysql_error()); mysql_select_db("test_db", $conn) or die(mysql_error()); $q = "SELECT id, first_name, last_name FROM names"; $result = mysql_query ($q); $num_rows = mysql_num_rows ($result); IF ($num_rows > 0) { FOR ($j=0; $j > $num_rows; $j++) { $id = mysql_query($result, $j, "id"); $firstname = mysql_query($result, $j, "first_name"); $lastname = mysql_query($result, $j, "last_name"); echo "<a href='index.php?id=", $id, "'>", $firstname, "</a>"; } //close FOR loop } // close for IF loop ?> Quote Link to comment Share on other sites More sharing options...
gigas10 Posted July 7, 2008 Share Posted July 7, 2008 ?> <a href="index.php?id=<?php echo $id; ?>"><?php echo $firstname; ?></a> <?php echoing html is a bad habit Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 7, 2008 Share Posted July 7, 2008 maverick3d: Any chance you can tell us what happens with the above? Do you get an error message? What's the output? What's the expected output? echoing html is a bad habit Errr, why's that then? Quote Link to comment Share on other sites More sharing options...
maverick3d Posted July 7, 2008 Author Share Posted July 7, 2008 gigas10: thanks for the tip champ GingerRobot: Ah, sorry... it renders a blank page, no errors, text, hyperlinks - just nothing Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 7, 2008 Share Posted July 7, 2008 Put this at the top of your script error_reporting(E_ALL); ini_set('error_reporting', 'on'); Then let us know if you get any errors. Quote Link to comment Share on other sites More sharing options...
gigas10 Posted July 7, 2008 Share Posted July 7, 2008 You do not want to echo html because it does not format properly which can make it hard for people reading the html. Also, usually a blank page means syntax error, most likely a missed ; or } Quote Link to comment Share on other sites More sharing options...
maverick3d Posted July 7, 2008 Author Share Posted July 7, 2008 Wolphie: I added your code to the top of the <?php and has not provided any errors or codes just a blank page. If I view the source it shows: <a href="index.php?id="></a> Quote Link to comment Share on other sites More sharing options...
gigas10 Posted July 7, 2008 Share Posted July 7, 2008 Did you try what i said? Quote Link to comment Share on other sites More sharing options...
craygo Posted July 7, 2008 Share Posted July 7, 2008 error reporting not going to help if he does not use the tools to report it. Always use mysql_error() in your queries until code is running correctly. As a personal preference I like to use a while loop for my sql results try this instead <?php $host="localhost"; $username="user"; $password="pass"; $conn = mysql_connect($host,$username,$password) or die(mysql_error()); mysql_select_db("test_db", $conn) or die(mysql_error()); $q = "SELECT id, first_name, last_name FROM names"; $result = mysql_query ($q) or die(mysql_error()); // use error reporting here $num_rows = mysql_num_rows ($result); IF ($num_rows > 0) { while($r = mysql_fetch_assoc($result)){ { $id = $r['id']; $firstname = $r['first_name']; $lastname = $r['last_name']; echo "<a href=\"index.php?id=". $id . "\">". $firstname . "</a>"; } //close FOR loop } // close while loop ?> Ray Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 7, 2008 Share Posted July 7, 2008 Yeah, as craygo said, always use error handling. I didn't realise you forgot mysql_error on the actual query, I assumed you did because you had it on the initial database connection. Quote Link to comment Share on other sites More sharing options...
gigas10 Posted July 7, 2008 Share Posted July 7, 2008 The problem is the line echo "<a href='index.php?id=", $id, "'>", $firstname, "</a>"; The formatting is wrong and the quotes are causing the echo statement to end early. Format it like I said and it will work. Quote Link to comment Share on other sites More sharing options...
maverick3d Posted July 7, 2008 Author Share Posted July 7, 2008 wow such a positive response, thanks guys! craygo: I have tested your script and it is erroring with Parse error: syntax error, unexpected $end in C:\root\tlw.db\index.php on line 24 Line 24 is ending PHP ?> not sure why it would error this? Quote Link to comment Share on other sites More sharing options...
craygo Posted July 7, 2008 Share Posted July 7, 2008 sorry one to many brackets <?php $host="localhost"; $username="user"; $password="pass"; $conn = mysql_connect($host,$username,$password) or die(mysql_error()); mysql_select_db("test_db", $conn) or die(mysql_error()); $q = "SELECT id, first_name, last_name FROM names"; $result = mysql_query ($q) or die(mysql_error()); // use error reporting here $num_rows = mysql_num_rows ($result); IF ($num_rows > 0) { while($r = mysql_fetch_assoc($result)){ $id = $r['id']; $firstname = $r['first_name']; $lastname = $r['last_name']; echo "<a href=\"index.php?id=". $id . "\">". $firstname . "</a>"; } //close while loop } // close IF statement ?> Ray Quote Link to comment Share on other sites More sharing options...
Wolphie Posted July 7, 2008 Share Posted July 7, 2008 The problem is the line echo "<a href='index.php?id=", $id, "'>", $firstname, "</a>"; The formatting is wrong and the quotes are causing the echo statement to end early. Format it like I said and it will work. As gigas10 pointed out, your formatting is wrong. Try: echo '<a href="index.php?id=' . $id . '">' . $firstname . '</a>'; Your quotes were ok by the looks of it, but to concatenate strings, you use a period (full-stop) rather than a comma. Quote Link to comment Share on other sites More sharing options...
maverick3d Posted July 7, 2008 Author Share Posted July 7, 2008 craygo: thanks champ, this has created the link perfectly and so much easier from what I was using ! Quote Link to comment 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.