Hi,
I have a problem and don't know how to resolve it. I connect to database and run a query. When I try to do a mysql_fetch_rows, it doesn't work. If I just get the number of rows returned and go through them it works fine.

$sql="Select * from tbProducts";
$result = mysql_query($sql) or die("Invalid query: " . mysql_error());
$n = mysql_num_rows($result);

for ($i = 0; $i < $n; $i++) {   // this works
    printf("Name: %s<br>\n", mysql_result($result,$i,"ProdName"));
    printf("Price: %s<br>\n", mysql_result($result,$i,"ProdPrice"));
}

while ($row = mysql_fetch_row($result) {  // this doesn't work!
    printf("Name: %s<br>\t", $row["ProdName"]);
    printf("Price: %s<br>\n", $row["ProdPrice"]);
}

Thanks,

    You are looking to use mysql_fetch_assoc() instead. mysql_fetch_row() returns an array with numeric indices. If the code snippet is directly from your test code, then I must warn that you are missing a mysql_data_seek() to reset the row pointer.

      Thanks for the reply. I tried that and still didn't work. I tried mysql_fetch_array($result, MYSQL_BOTH) as well, but no luck.

      The sample code I included is from my code but I comment out one and use the other to test.

        What is interesting is that when I use the mysql_fetch_array or mysql_fetch_row, it seems nothing in the page gets executed. I have a bunch of echo statements that should display regardless but nothing on the page gets echo'ed!

          hmm... but it works for mysql_result()?

          Try this:

          $sql = "Select ProdName, ProdPrice from tbProducts";
          $result = mysql_query($sql) or die("Invalid query: " . mysql_error());
          
          while ($row = mysql_fetch_assoc($result)) {
              printf("Name: %s<br>\t", $row["ProdName"]);
              printf("Price: %s<br>\n", $row["ProdPrice"]);
          }

          I hope that you have error_reporting set to E_ALL, because your original code had a typo error (missing closing parenthesis).

            Her is the code, can you see if it works on your side?
            Could this be an hosting issue (I have external hosting)?

              
            $hostname = "myhostid"; $database = "mydb"; $username = "myid"; $password = "mypwd"; $conn = mysql_pconnect($hostname, $username, $password); echo "hostname: $hostname, database: $database, username: $username, password: $password <br />"; if (!$conn) echo 'failed in mysql_pconnect<br />'; else { echo "connected to host $hostname<br />"; if (!mysql_select_db($database)) echo 'failed in mysql_select_db<br />'; else{ echo "selected database $database<br />"; $sql="Select * from tbProducts"; $result = mysql_query($sql) or die("Invalid query: " . mysql_error()); echo "result: $result<br />"; $n = mysql_num_rows($result); echo "number of rows returned: $n<br />"; /* for ($i = 0; $i < $n; $i++) { printf("Name: %s<br>\n", mysql_result($result,$i,"ProdName")); printf("Price: %s<br>\n", mysql_result($result,$i,"ProdPrice")); }*/ while ($row = mysql_fetch_row($result) { printf("Name: %s<br>\t", $row["ProdName"]); printf("Price: %s<br>\n", $row["ProdPrice"]); } mysql_free_result($result); mysql_close($db); } }

            Thanks.

              Darn it! That was the problem (missing ')' ). My code is hosted through a hosting company and probably they don't have the error reporting set to E_ALL.

              Thanks for your help.

                You're welcome 🙂
                Remember to mark this thread resolved (if it is).

                You might also considering installing a web server, MySQL and PHP on your computer for local testing. It certainly would save your bandwidth, and can be faster too.

                  Write a Reply...