Page 1 of 1

variables from a database

Posted: Sun Aug 04, 2002 12:58 pm
by learning_php_mysql
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

Posted: Sun Aug 04, 2002 3:14 pm
by haagen
In your while loop try:

Code: Select all

$$myrow&#1111;"name"] = $myrow&#1111;"value"];

Posted: Sun Aug 04, 2002 3:29 pm
by learning_php_mysql
that didnt work.... ahhh being a n00b sucks

Posted: Mon Aug 05, 2002 2:18 am
by gnu2php
Don't worry about it. We're here to help. :wink:

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.

Posted: Mon Aug 05, 2002 3:57 am
by hob_goblin

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++)
&#123; 
$il = $i - 1;
$result = mysql_query("SELECT * FROM config LIMIT $il,$i"); 
$foo = mysql_fetch_assoc($result);
$myrow&#1111;"name"] = $foo&#1111;"value"]; 
&#125;
try something like that

Posted: Mon Aug 05, 2002 4:44 am
by Dale
haagen wrote:In your while loop try:

Code: Select all

$$myrow&#1111;"name"] = $myrow&#1111;"value"];
Erm i didnt know you could put $$ together...

Posted: Mon Aug 05, 2002 4:46 am
by twigletmac
Dale wrote:Erm i didnt know you could put $$ together...
It's called a variable variable - incredible fun to get your head around.

Mac

Posted: Mon Aug 05, 2002 9:00 am
by learning_php_mysql
hob_goblin wrote:

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++)
&#123; 
$il = $i - 1;
$result = mysql_query("SELECT * FROM config LIMIT $il,$i"); 
$foo = mysql_fetch_assoc($result);
$myrow&#1111;"name"] = $foo&#1111;"value"]; 
&#125;
try something like that

well that didnt work, but thanks for the help

Posted: Mon Aug 05, 2002 10:55 am
by learning_php_mysql
here's my solution

function variables($var) {
$result=mysql_query('SELECT * FROM config WHERE title="font"');
$config=mysql_fetch_array($result);
$font = $config['value'];
echo($font);
}

then whenever i need a variable i call on it like this

variables(varibale_name);

:D :D

Posted: Mon Aug 05, 2002 11:31 am
by twigletmac
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

Posted: Mon Aug 05, 2002 12:31 pm
by learning_php_mysql
i meant to post this

function variables($var) {
$result=mysql_query("SELECT * FROM config WHERE title='$var'");
$config=mysql_fetch_array($result);
$output = $config['value'];
echo($output);
}

and i will do taht twigletmac, thanks.