variables from a database
Moderator: General Moderators
-
learning_php_mysql
- Forum Commoner
- Posts: 27
- Joined: Sun Aug 04, 2002 12:58 pm
- Location: WA
variables from a database
ok, so i a database with id, name, value. i want to take name and set it equal to the value of it. then i want to take value and set that equal to its value. then i wanna set "name" = "value" so my database will automatically create the config file for my websites. i just started using mysql last night and im really stumped. heres what i have.
<?php
$db = mysql_connect("localhost", "database", "password");
mysql_select_db("database", $db);
$result=mysql_query("SELECT * FROM config", $db);
while($myrow = mysql_fetch_array($result))
{
$myrow["name"] = $myrow["value"];
}
?>
i know this is prolly super simple and im makin a n00b mistake, but any help will be appreciated. thanks
<?php
$db = mysql_connect("localhost", "database", "password");
mysql_select_db("database", $db);
$result=mysql_query("SELECT * FROM config", $db);
while($myrow = mysql_fetch_array($result))
{
$myrow["name"] = $myrow["value"];
}
?>
i know this is prolly super simple and im makin a n00b mistake, but any help will be appreciated. thanks
In your while loop try:
Code: Select all
$$myrowї"name"] = $myrowї"value"];-
learning_php_mysql
- Forum Commoner
- Posts: 27
- Joined: Sun Aug 04, 2002 12:58 pm
- Location: WA
Don't worry about it. We're here to help.
Although I don't have the solution--since I've never used mysql--I did notice one problem here:
while($myrow = mysql_fetch_array($result))
{
$myrow["name"] = $myrow["value"];
}
Since $myrow gets reset each time through the loop, setting $myrow["name"] to anything won't work.
Although I don't have the solution--since I've never used mysql--I did notice one problem here:
while($myrow = mysql_fetch_array($result))
{
$myrow["name"] = $myrow["value"];
}
Since $myrow gets reset each time through the loop, setting $myrow["name"] to anything won't work.
- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact:
Code: Select all
$db = mysql_connect("localhost", "database", "password");
mysql_select_db("database", $db);
$num = mysql_query("SELECT * FROM config");
$num = mysql_num_rows($num);
for($i = 1; $i <= $num; $i++)
{
$il = $i - 1;
$result = mysql_query("SELECT * FROM config LIMIT $il,$i");
$foo = mysql_fetch_assoc($result);
$myrowї"name"] = $fooї"value"];
}Erm i didnt know you could put $$ together...haagen wrote:In your while loop try:
Code: Select all
$$myrowї"name"] = $myrowї"value"];
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
It's called a variable variable - incredible fun to get your head around.Dale wrote:Erm i didnt know you could put $$ together...
Mac
-
learning_php_mysql
- Forum Commoner
- Posts: 27
- Joined: Sun Aug 04, 2002 12:58 pm
- Location: WA
hob_goblin wrote:try something like thatCode: Select all
$db = mysql_connect("localhost", "database", "password"); mysql_select_db("database", $db); $num = mysql_query("SELECT * FROM config"); $num = mysql_num_rows($num); for($i = 1; $i <= $num; $i++) { $il = $i - 1; $result = mysql_query("SELECT * FROM config LIMIT $il,$i"); $foo = mysql_fetch_assoc($result); $myrowї"name"] = $fooї"value"]; }
well that didnt work, but thanks for the help
-
learning_php_mysql
- Forum Commoner
- Posts: 27
- Joined: Sun Aug 04, 2002 12:58 pm
- Location: WA
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You might want to check out the stuff on variable variables, they're a bit fiddly but can be very useful for what you're trying to do. Glad you found your own solution to the problem though, thanks for posting it too since it'll be useful for someone else coming along. The only thing I'd add to your code is a bit of error handling so that if what you pass across as $var doesn't exist (is perhaps misspelt) then it doesn't cause problems with the rest of your program.
Mac
Mac
-
learning_php_mysql
- Forum Commoner
- Posts: 27
- Joined: Sun Aug 04, 2002 12:58 pm
- Location: WA