i got a code like this:
include 'dbconnect.php';
mysql_query("SET NAMES utf8;");
$sql = mysql_query("SELECT * From courseEnglish");
$courseInfo = array();
while ($row_course = mysql_fetch_assoc($sql))
$courseInfo[] = $row_course;
it gives me a php array from the db, but in order to present the data i need to use:
echo $courseInfo[2]['Course'];
i would like for the array to be so i can just type somthing like:
echo $courseInfo[2][2];
is there an option for changing the code so ill have an array i can loop on with just indexes?
I think that you're looking for mysql_fetch_row
, which does the same as mysql_fetch_assoc
, except for the fact that it returns a numerically indexed array. here's the link to the docs.
Having said that, you might want to consider ditching the deprecated mysql_*
extension, in favour of either mysqli_*
(the i
stands for improved), or the fully OO-style PDO
extension. Check the red box at the top of any mysql_*
function's manual page, it contains links to the preferred counterpart-function...
But all things considered, looking at your query (SELECT * FROM ...
), I'd not use a numerically indexed array at all to get the results. If your table contains more than 1 field, you'll hit trouble sooner or later, when trying to work out what piece of data is coming from what field. Seriously... just use an assoc array, or even better: use objects.
Suppose you change the table layout (add/remove certain fields?), it'll be hell to maintain your code in that case. Still, it's up to you to decide, but I thought I'd might mention this.
Maybe this..?
foreach ($courseInfo as $course_index = > $course_content)
foreach ($course_content as $field)
echo $field;
cheers
Try this :
while ($row_course = mysql_fetch_array($sql))