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
https://forums.phpfreaks.com/topic/113561-simple-php-mysql-question/
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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.