printing the key and its value from mysql select query
Moderator: General Moderators
- pelegk2
- Forum Regular
- Posts: 633
- Joined: Thu Nov 27, 2003 5:02 am
- Location: Israel - the best place to live in after heaven
- Contact:
printing the key and its value from mysql select query
how do i print from a select
the key and its value like this :
echo key1->val1
echo key2->val2
.
.
.
.
.
thnaks in advance
peleg
the key and its value like this :
echo key1->val1
echo key2->val2
.
.
.
.
.
thnaks in advance
peleg
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
not sure what you mean
if the array has a value (which it should)
then possibly
may work
i have never done this so dont take my word for it
if the array has a value (which it should)
Code: Select all
$array = "$result, MYSQL_ASSOC";Code: Select all
print_r $array;i have never done this so dont take my word for it
to count elements is an array have a look at this function http://uk.php.net/manual/en/function.count.php
Mark
Mark
A quick method:
Code: Select all
<?php
$query = "SELECT field, value from table";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)){
echo 'Field is: ' . $row['field'] . ' and value is ' . $row['value'] . '<br>';
}
}
?>- scorphus
- Forum Regular
- Posts: 589
- Joined: Fri May 09, 2003 11:53 pm
- Location: Belo Horizonte, Brazil
- Contact:
And also an example 
cities table of my teaching db:
cities.php:
Above code's output:
Hope it helps,
Scorphus.
cities table of my teaching db:
Code: Select all
mysql> select * from cities limit 3;
+---------+----------------+----------+------+-------+------+
| city_id | name | pop | tmp | press | alt |
+---------+----------------+----------+------+-------+------+
| 1 | Belo Horizonte | 2500000 | 23 | 1020 | 980 |
| 2 | Curitiba | 1800000 | 18 | 1130 | 510 |
| 3 | Sao Paulo | 20000000 | 20 | 1070 | 640 |
+---------+----------------+----------+------+-------+------+
3 rows in set (0.00 sec)Code: Select all
<pre><?php
mysql_connect('localhost', 'root');
mysql_select_db('teaching');
$sql = "select * from cities limit 3";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) { // PHP Manual: [php_man]mysql_fetch_assoc[/php_man]
foreach ($row as $key => $value) // PHP Manual: [php_man]foreach[/php_man]
echo "$key => $value\n";
echo "====\n\n";
}
?></pre>Code: Select all
city_id => 1
name => Belo Horizonte
pop => 2500000
tmp => 23
press => 1020
alt => 980
====
city_id => 2
name => Curitiba
pop => 1800000
tmp => 18
press => 1130
alt => 510
====
city_id => 3
name => Sao Paulo
pop => 20000000
tmp => 20
press => 1070
alt => 640
====Scorphus.