tags:

views:

28

answers:

3

Hi all,

I'm not exactly new to PHP but I haven't used MySQL that much, so I'll ask you guys about this one.

I have a database with one row of data, which consists of a date and two integers. However, when I query the database with this:

$query = "SELECT * FROM history";
$theQuery = mysql_query($query, $connect);
$array = mysql_fetch_array($theQuery);

... and I do a print_r, I get the following:

Array
(
    [0] => 2010-08-17
    [date] => 2010-08-17
    [1] => 17454
    [posts] => 17454
    [2] => 1058
    [members] => 1058
)

Am I doing something wrong? I plan on having many rows with a ton of data and printing it to a table, THEN how would I go about sorting through it?

+1  A: 

first : You did everything right

u can use

mysql_fetch_assoc

or

mysql_fetch_array  ( resource $result  , MYSQL_ASSOC)

the default is to return MYSQL_BOTH (associative array + numeric array)

read at :

http://www.php.net/manual/en/function.mysql-fetch-array.php

Haim Evgi
Thanks for the answer. ;)
esqew
YOUR WELCOME :)
Haim Evgi
A: 

The second argument to mysql_fetch_array is optional but default is MYSQL_BOTH and will give you values both for indices and keys. You can use MYSQL_ASSOC instead.

The PHP manual on mysql_fetch_array:

Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. The type of returned array depends on how result_type is defined. By using MYSQL_BOTH (default), you'll get an array with both associative and number indices. Using MYSQL_ASSOC, you only get associative indices (as mysql_fetch_assoc() works), using MYSQL_NUM, you only get number indices (as mysql_fetch_row() works).

gustaf
A: 

With mysql_fetch_array, you can run it in a loop ,

ex: while($array = mysql_fetch_array($theQuery)) { echo $array["date"]; echo $array["posts"]; echo $array["members"];

}

viv