variables from a database

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

variables from a database

Post 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
User avatar
haagen
Forum Commoner
Posts: 79
Joined: Thu Jul 11, 2002 3:57 pm
Location: Sweden, Lund

Post by haagen »

In your while loop try:

Code: Select all

$$myrow&#1111;"name"] = $myrow&#1111;"value"];
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

Post by learning_php_mysql »

that didnt work.... ahhh being a n00b sucks
gnu2php
Forum Contributor
Posts: 122
Joined: Thu Jul 11, 2002 2:53 am

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

Post 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...
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

Post 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
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
learning_php_mysql
Forum Commoner
Posts: 27
Joined: Sun Aug 04, 2002 12:58 pm
Location: WA

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