Question:
php question: How do you display table rows using the echo feature?
detroitkid17
2006-09-19 13:38:12 UTC
This is what I have so far:


(connection, and database strings erased)
$result = mysql_query("SELECT name,age,desc FROM contact WHERE ID = '$idno'");
$row = mysql_fetch_array($result);
echo 'Name: '.$row['name'].'
';
echo 'Age: '.$row['age'].'
';
echo 'Description: '.$row['desc'].'
';
?>

When I view the page in a browser, I change the url to .com/display.php?ID=7

The only thing that shows up is the Name:, Age:, Description:, not the actual values of the data in my table. Anyone know why?
Four answers:
mattmaul92
2006-09-19 13:50:51 UTC
That is because you need to add spaces here's a revised code -

(connection, and database strings erased)

$result = mysql_query("SELECT name,age,desc FROM contact WHERE ID = '$idno'");

$row = mysql_fetch_array($result);

echo 'Name: ' . $row['name'] . '
';

echo 'Age: ' . $row['age'] . '
';

echo 'Description: ' . $row['desc'] . '
';

?>



Also do you see that in your index page? If so and you don't have a record id in your database for 7 then know that PHP will automaticaly put any $_GET['page'] as index. (i'm sorta sick and tired so sorry if this is poorly worded)



Try posting the entire script as it might be more than that. One last thing -



Remember after select put parenthasise around name, age, date as well as spaces. One other thing you can try is putting the query in a $query variable then using it in the result variable. It seems for some weird reason to run smoother for me.
Interested Dude
2006-09-19 13:49:04 UTC
Perhaps the entire code is not shown, where are you assigning the $idno variable a value? You should add some error loops to get a better idea what is going on.
danielbuca
2006-09-19 13:50:32 UTC
I see no initialization of the variable $idno.

You should set $idno = $_GET["ID"];



For testing purposes put an echo on the sql statement and check it on the database (using phpmyadmin for example) and see if it returns any data.



Regards,

Daniel - www.mind-spinner.com
NC
2006-09-21 08:51:44 UTC
Wel, you forgot to set $idno. Try adding this to the beginning of your script:



$idno = (int) $_GET['ID'];



Do not drop the (int) part. Forcing $idno to be an integer helps prevent SQL injection attacks.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...