Rob Avery IV Rob Avery IV - 2 years ago 85
SQL Question

Getting a empty entry for query in PHP

So I want to be able to get information from MySQL. In my PHP code, I call the query and echo the info in this sort of fashion:

$sql = "SELECT * FROM `person`";
$rs = $COMMON->executeQuery($sql, $_SERVER["SCRIPT_NAME"]);

while($row = mysql_fetch_row($rs)){
echo ("Name: ".$row['name']."<br />";
echo ("Gender: ".$row['gender']."<br />";
echo ("Age: ".$row['age']."<br />";

Though, the actual value of
and the rest is completely blank. Not even a single space. Though, when I call it by the actual column index like so
, it gives me the right value.

So, for example, lets say this had three person in the table. The end result page would look like this:


Is something I'm doing wrong? Why is it giving me this empty string instead of the query value?

MySQL database is saved on MyPHPAdmin. I don't know if it makes a difference or not. If there is anymore information missing that you deem necessary, please let me know. Thanks!

Answer Source

You need to be using mysql_fetch_assoc() instead of mysql_fetch_row(). mysql_fetch_assoc() returns an associative array of your results. mysql_fetch_row() returns a numerical array.

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download