Maky-chan
Maky-chan

Reputation: 39

PHP : Select from Database

thank you for reading. I'm having a little problem with the next sentence:

$nats = mysql_query("SELECT id,name FROM reportSatus WHERE reportId = ". $_POST['id']);
$rnat = mysql_fetch_array($nats);

With print_r($rnat) :

Array(
[0] => 1
[id] => 1
[1] => Poca contaminacion
[name] => Poca contaminacion
[2] => 1
[reportId] => 1)

But in the database with the same sentence is:

id            name
1       Poca contaminacion
2       Mucha contaminacion

Any idea what can it be? Thank you in advance ~

Upvotes: 0

Views: 64

Answers (2)

jx12345
jx12345

Reputation: 1670

try :

echo '<table><tr><th>id</th><th>name</th></tr>';
while ($row = mysql_fetch_assoc($nats)) {
    echo "<tr><td>{$row['id']}</td><td>{$row['name']}</td></tr>";
}
echo '</table>';

Upvotes: 1

Fabio
Fabio

Reputation: 23480

From documentation

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).

That's actually the way it works, if you need more rows to be returned you will need to loop throw the records

while($rnat = mysql_fetch_array($nats))
{
    //print here
}

Upvotes: 0

Related Questions