Page 1 of 1
printing the key and its value from mysql select query
Posted: Wed Jan 07, 2004 7:55 am
by pelegk2
how do i print from a select
the key and its value like this :
echo key1->val1
echo key2->val2
.
.
.
.
.
thnaks in advance
peleg
Posted: Wed Jan 07, 2004 7:55 am
by malcolmboston
what situation do you want to do it in and ill try and help you out
Posted: Wed Jan 07, 2004 8:20 am
by JayBird
A quick and dirty way would be to use the [php_man]print_r[/php_man] function.
Mark
Posted: Wed Jan 07, 2004 8:26 am
by pelegk2
i want to print a line to the screen from a query !
its only 1 line and what i want to print is the :
name of filed 1 and its value
name of filed 2 and its value
name of filed 3 and its value
thanks in advance
peleg
Posted: Wed Jan 07, 2004 8:28 am
by malcolmboston
i have read another post (somewhere on this forums) about this
have a look around
Posted: Wed Jan 07, 2004 8:33 am
by pelegk2
how do i check what is the length of an array?
Posted: Wed Jan 07, 2004 8:40 am
by malcolmboston
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
Posted: Wed Jan 07, 2004 8:43 am
by JayBird
to count elements is an array have a look at this function
http://uk.php.net/manual/en/function.count.php
Mark
Posted: Wed Jan 07, 2004 10:12 am
by Sarok
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>';
}
}
?>
Posted: Wed Jan 07, 2004 1:41 pm
by scorphus
And also an example
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)
cities.php:
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>
Above code's output:
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
====
Hope it helps,
Scorphus.
Posted: Thu Jan 08, 2004 2:36 am
by pelegk2
thanks alot all of u this is execlly what i wanted:)
Posted: Thu Jan 08, 2004 2:40 am
by pelegk2
scorphus that execlly the code ineeded!
sarok - what u wrote is assuming i know all the keys and in advance
and i want it dynamclly
thanks every one anyway!