Jump to content

Simple PHP / MySQL Question


maverick3d

Recommended Posts

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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.