printing the key and its value from mysql select query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
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

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

what situation do you want to do it in and ill try and help you out
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

A quick and dirty way would be to use the [php_man]print_r[/php_man] function.

Mark
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post 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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

i have read another post (somewhere on this forums) about this

have a look around
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

how do i check what is the length of an array?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

not sure what you mean

if the array has a value (which it should)

Code: Select all

$array = "$result, MYSQL_ASSOC";
then possibly

Code: Select all

print_r $array;
may work

i have never done this so dont take my word for it
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

to count elements is an array have a look at this function http://uk.php.net/manual/en/function.count.php

Mark
Sarok
Forum Newbie
Posts: 9
Joined: Fri Jan 02, 2004 12:20 pm
Location: Oslo, Norway

Post 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>';
	
     }

}

?>
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

And also an example ;)

cities table of my teaching db:

Code: Select all

mysql&gt; 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 =&gt; 1
name =&gt; Belo Horizonte
pop =&gt; 2500000
tmp =&gt; 23
press =&gt; 1020
alt =&gt; 980
====

city_id =&gt; 2
name =&gt; Curitiba
pop =&gt; 1800000
tmp =&gt; 18
press =&gt; 1130
alt =&gt; 510
====

city_id =&gt; 3
name =&gt; Sao Paulo
pop =&gt; 20000000
tmp =&gt; 20
press =&gt; 1070
alt =&gt; 640
====
Hope it helps,
Scorphus.
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post by pelegk2 »

thanks alot all of u this is execlly what i wanted:)
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

Post 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!
Post Reply