If you are retrieving data from mysql db and showing them to result page in raw php coding, then associative array retrieving is best way to be chosen. To achieve this, you have to write like:
$result = mysql_query($query);
$rows = mysql_fetch_assoc($result);
Then you can use it like:
echo $row["id"];
echo $row["name"];
I use associative array instead of normal 0-indexed array as they are more meaningful and understandable by seeing the code only. However I have seen an interesting thing regarding this today while working on a enhancement small project on an existing raw php application. This is a very simple issue and you will get a quick help if you are facing a problem like you are showing data using associative array to web page, but no data are being showed, although you are sure that spelling is correct.
The main fact was, the associative indexes are 'case sensitive'. If you are writing insert,update,delete query, then case won't matter, in any case will be accepted. however, when you are retrieving data using mysql_fetch_assoc($result), then the returned is a associative array and that array will be case sensitive indexed, that means, then $row["Id"] and $row["id"] will be different. Just by copy-pasting the index names with my incorrect code solved me the problem. SO, i had to write:
echo $row["Id"]
instead of
echo $row["id"]
as the database had column "Id".
I think, in these kind of case, it is best to copy paste the data column name from database table to your code to get rid of this kind of unexpected errors. Also, case sensitivity also be occurred in some sql queries, i found this problem also, where table structures are copy-pasted, but worked differently(local version is case insensitive and server version was case sensitive), so its better to use always the same case.
Subscribe to:
Post Comments (Atom)










0 comments:
Post a Comment