0

I have the following MySQL table:

table_1
id | value_1 | value_2 | value_3
1    hehe      haha      stack
2    over      flow      me
3    123       abc       hello
4    hi        random    php
5    html      js        css

How can I select value_2 from 3rd row, which is "abc"?

Something like $rows[3]['value_2']

Here's the code I had tried with:

$sql=mysql_query("SELECT * FROM table_1 ORDER BY `table_1`.`id` ASC");
$rows=mysql_fetch_array($sql);
//And then I tried accessing it by "$rows[3]['value_2']" and $rows['value_2'][3]

I can't use SELECT * FROM table_1 WHERE id='3', because I need to access multiple lines (all of them). I can't use a WHILE loop also, because I need the values in very different places in the code.

ᗩИᎠЯƎᗩ
  • 2,122
  • 5
  • 29
  • 41
user3707440
  • 195
  • 1
  • 1
  • 8

2 Answers2

0

mysql_fetch_array only returns a row corresponding to the query. The proper use of mysql_fetch_array is as such:

$sql=mysql_query("SELECT * FROM table_1 ORDER BY `table_1`.`id` ASC");

while ($row = mysql_fetch_array($sql)) {
    printf("ID: %s  value_2: %s", $row[0], $row[2]);  
}

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

Zac Lozano
  • 778
  • 1
  • 4
  • 12
0

Found the answer. You can now access them how you wanted.

$sql=mysql_query("SELECT * FROM settings ORDER BY `settings`.`id` ASC");
$id=0;
while ($rows = mysql_fetch_array($sql)) {
    $settings[$id]['value_1']=$rows['value_1'];
    $settings[$id]['value_2']=$rows['value_2'];
    $settings[$id]['value_3']=$rows['value_3'];
    $id++;
    }

echo $settings[5]['value_3'];
echo $settings[5]['value_1'];
echo $settings[4]['value_2'];
echo $settings[0]['value_3'];
KaareZ
  • 615
  • 2
  • 10
  • 22