Jump to content


Photo

MySQL Fetch Array


  • Please log in to reply
8 replies to this topic

#1 Cypher_489

Cypher_489
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationEngland

Posted 01 July 2006 - 11:15 AM

Hi,

Just a simple question.

$sql = mysql_query("SELECT * FROM dt_directories WHERE category = '$category'") or die(mysql_error()); 
  
 while ( $dirs = mysql_fetch_array($sql) ) { 
	 $name = $dirs['name']; 
	 echo "$name, ";
	 }

This echos out the names of directories which are from the category stored in the variable $category like this (category is "general"): Dmoz, Yahoo, Best of the Web, and so on...

What I want to do is, rather then echo it, is to store them in a variable with the formatting like in the example.

TIA
Cypher_489

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 July 2006 - 11:25 AM

Yes you can do that. Just git rid of the echo statement. But the $name variable can only be used on the page it was created on. It cannot be used on another page. So if you want to use $name over multiple pages you might want to store it in a session/cookie.

#3 Cypher_489

Cypher_489
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationEngland

Posted 01 July 2006 - 11:31 AM

I've tried echoing the $name variable outside while() { } before and I only got the last directory in the array.
Cypher_489

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 01 July 2006 - 11:33 AM

Also you would need to set it like this to make the variable hold all the values comma seperated


$sql = mysql_query("SELECT * FROM dt_directories WHERE category = '$category'") or die(mysql_error());
 
while ( $dirs = mysql_fetch_array($sql) ) {
$name = $name."$dirs[name], ";
}

to make name 1 list like it echo's if i'm right in how i interprited what you wnat?


Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 July 2006 - 11:35 AM

In that case you'll want to make the $name variabled an array which you can do by adding [] at the end of variable name like so:
$name[] = $dirs['name'];

What will happen now $name will store the results as in an array.

#6 Cypher_489

Cypher_489
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationEngland

Posted 01 July 2006 - 11:42 AM

Brilliant, thats exactly what I wanted. Thanks for the quick replies.
Cypher_489

#7 Cypher_489

Cypher_489
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationEngland

Posted 01 July 2006 - 01:40 PM

Just one more thing on this topic, I've found the last element in the array:

$last_sql = mysql_query("SELECT * FROM dt_directories WHERE category = '$category' ORDER BY name DESC LIMIT 1") or die(mysql_error());

while ( $last = mysql_fetch_object($last_sql) ) {
$last_element = "$last->name";
}

How will I incorporate this into the $name variable so that the last element doesn't have the comma after it.

I've tried a simple
if( $name != $last_element) {
$name = $name."$dirs[name], ";
else {
$name = $name."$dirs[name]"; }

but that doesn't work, any ideas?
Cypher_489

#8 Cypher_489

Cypher_489
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationEngland

Posted 01 July 2006 - 10:22 PM

Anyone?
Cypher_489

#9 Cypher_489

Cypher_489
  • New Members
  • Pip
  • Newbie
  • 6 posts
  • LocationEngland

Posted 02 July 2006 - 01:36 PM

NVM, I've fixed it now.

while ( $dirs = mysql_fetch_array($sql) ) {
   $name = $name."$dirs[name], ";
   $names = rtrim($name, ',');
 }

Didn't need the last element info.
Cypher_489




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users