View Full Version : extreme beginner question
thetakerfan
Sun 13th Aug '00, 5:51pm
I want to create a DB to store several layout options, and call them from the dif pages on my site
Things like bgcolor, linkcolor, alinkcolor, tableborder, tableheader, etc. Things like that, so that when I want to change my site around, I can just change the DB instead of all the files in my site, I of course have no idea how to do this.
Would this be the right direction:
<?php
$DB_Connect = mysql_connect("localhost","progtest_thetake","**********");
$DB_Connect = mysql_select_db ("progtest_layout");
$DB_Connect = "CREATE TABLE colors ( " .
"bgcolor TEXT, " .
"linkcolor TEXT, " .
"alinkcolor TEXT, " .
"tableborder TEXT, " .
"tableheader TEXT, " .
")";
if ( mysql_query($DB_Connect) ) {
echo("<P>colors table successfully created!</P>");
} else {
echo("<P>Error creating colors table: " .
mysql_error() . "</P>");}
$result = mysql_query ("INSERT INTO colors (bgcolor, linkcolor, alinkcolor, tableborder, tableheader)
VALUES ('$#cc9999', '$#66ffcc', '$#33cccc', '$#ff33ff', '$#999933')");
if(!$result)
{
echo "<b>Something not added:</b> ", mysql_error();
exit;
}
if($result)
{
mysql_close($connection);
print "Something added sucessfully!";
}
?>
[Edited by thetakerfan on 08-13-2000 at 05:16 PM]
Stallion
Sun 13th Aug '00, 11:19pm
A good start, but there are some errors. Also, for a simple site color scheme, there's probably a better way to do it which I'll explain below.
1) Instead of mysql_connect... (its better to use mysql_pconnect, since I take it that it caches db connections for faster connections)
2) Don't use TEXT as a table field unless its a LOT of text. In your case, I'd use VARCHAR(10) which allows for a string up to ten characters in length.
3) Drop that , (comma) after the last field which you're creating in the CREATE TABLE query.
4) You don't want to prefix the hex color values with $, since they're not really variables, and thats the text you'll be inserting straight into the database.
Other than that, the code looks good. However, I'd recommend simply stating the variables in a PHP file, since a database would be overkill. Simple create a colors.inc file, and in it, place:
<?
$bgcolor = "#cc9999";
$linkcolor = "#66ffcc";
$alinkcolor = "#33cccc";
$tableborder = "#ff33ff";
$tableheader = "#999933";
?>
Then, for the rest of your site, just be sure the pages are parsed for PHP, and you can do something like:
<?
require "colors.inc";
?>
<html>
<head>
</head>
<body bgcolor="<? echo $bgcolor; ?>" linkcolor="<? echo $linkcolor; ?>" alinkcolor="<? echo $alinkcolor; ?>">
</body>
</html>
Then just change colors.inc when you want to change your colors...
Hope it helps! :)
thetakerfan
Mon 14th Aug '00, 12:20am
thanks for the help Stallion
the color thing is just so I can familiarize myself with the basics, before I try anything major
Once school starts up again I will be the webmaster for our school paper, and I plan on storing the articles in a DB, the colors DB is just so I can get a feel for things
thetakerfan
Mon 14th Aug '00, 12:40am
I get this error now
Warning: Supplied argument is not a valid MySQL-Link resource in /home/progtest/public_html/layout.php on line 28
Something added sucessfully!
thats after it says the table was created, "mysql_close($DB_Connect);" is line 28
thetakerfan
Mon 14th Aug '00, 2:02am
I tried using what Ed posted earlier to pull data from a DB, but I get an error, this is my code
<?php
$DB_Connect = mysql_pconnect("localhost","progtest_thetake","*******");
$DB_Connect = mysql_select_db("progtest_layout");
$query = "select bgcolor, linkcolor, vlinkcolor, tableborder, tableheader from colors";
$result = mysql_query ($query);
{ global $result;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY BGCOLOR="<?php echo $result[bgcolor] ?>" LINK="<?php echo $result[linkcolor] ?>" VLINK="<?php echo $result[vlinkcolor] ?>">
<table bgcolor="<?php echo $result[tableborder] ?>">
<tr bgcolor="<?php echo $result[tableheader] ?>">
<td>Test</td>
</tr>
</table>
<?php
}
?>
Mike Sullivan
Mon 14th Aug '00, 2:19am
<?php
$DB_Connect = mysql_pconnect("localhost","progtest_thetake","*******");
$DB_use = mysql_select_db("progtest_layout",$DB_Connect);
$query = "SELECT bgcolor,linkcolor,vlinkcolor,tableborder,tablehead er FROM colors";
$result = mysql_fetch_array(mysql_query($query));
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY BGCOLOR="<?php echo $result[bgcolor]; ?>" LINK="<?php echo $result[linkcolor]; ?>" VLINK="<?php echo $result[vlinkcolor]; ?>">
<table bgcolor="<?php echo $result[tableborder] ?>">
<tr bgcolor="<?php echo $result[tableheader]; ?>">
<td>Test</td>
</tr>
</table>
[Edited by Ed Sullivan on 08-14-2000 at 01:20 AM]
thetakerfan
Mon 14th Aug '00, 2:35am
I'm getting this error
Warning: Supplied argument is not a valid MySQL result resource in /home/progtest/public_html/testlayout.php on line 6
which is what I was getting before, until I removed the mysql_fetch_array, if I remove "mysql_fetch_array" I simply get "Test", but no colors
[I'm off to bed, I'll deal with this tomorrow]
Mike Sullivan
Mon 14th Aug '00, 2:42am
ok, ditch the fetch array part, and see if this works:
$result = $result[0];
I have no clue if it will or not...
thetakerfan
Mon 14th Aug '00, 4:29pm
This is what I have now, but I still just get "Text", with no color
<?php
$DB_Connect = mysql_pconnect("localhost","progtest_thetake","oops");
$DB_use = mysql_select_db("progtest_layout",$DB_Connect);
$query = "SELECT bgcolor,linkcolor,vlinkcolor,tableborder,tablehead er FROM colors";
$result = $result[0];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY BGCOLOR="<?php echo $result[bgcolor]; ?>" LINK="<?php echo $result[linkcolor]; ?>" VLINK="<?php echo $result[vlinkcolor]; ?>">
<table bgcolor="<?php echo $result[tableborder] ?>">
<tr bgcolor="<?php echo $result[tableheader]; ?>">
<td>Test</td>
</tr>
</table>
</body></html>
[Edited by thetakerfan on 08-14-2000 at 03:29 PM]
Mike Sullivan
Mon 14th Aug '00, 7:40pm
<?php
$DB_Connect = mysql_pconnect("localhost","progtest_thetake","*******");
$DB_use = mysql_select_db("progtest_layout",$DB_Connect);
$query = "SELECT bgcolor,linkcolor,vlinkcolor,tableborder,tablehead er FROM colors";
$results = mysql_query($query);
$result = $results[0];
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY BGCOLOR="<?php echo $result[bgcolor]; ?>" LINK="<?php echo $result[linkcolor]; ?>" VLINK="<?php echo $result[vlinkcolor]; ?>">
<table bgcolor="<?php echo $result[tableborder] ?>">
<tr bgcolor="<?php echo $result[tableheader]; ?>">
<td>Test</td>
</tr>
</table>
No clue if this'll work. I need to work more on not using John's MySQL class...
thetakerfan
Tue 15th Aug '00, 12:45am
still no luck
thetakerfan
Wed 23rd Aug '00, 4:46pm
anyone?
Stallion
Wed 23rd Aug '00, 4:56pm
<?php
mysql_connect("localhost","progtest_thetake","*******");
mysql_select_db("progtest_layout");
$query = "SELECT bgcolor,linkcolor,vlinkcolor,tableborder,tablehead er FROM colors";
$results = mysql_query($query);
$result = mysql_fetch_array($results);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY BGCOLOR="<?php echo $result['bgcolor']; ?>" LINK="<?php echo $result['linkcolor']; ?>" VLINK="<?php echo $result['vlinkcolor']; ?>">
<table bgcolor="<?php echo $result['tableborder'] ?>">
<tr bgcolor="<?php echo $result['tableheader']; ?>">
<td>Test</td>
</tr>
</table>
Try that, if it doesn't work -- tell us what the error is.
thetakerfan
Wed 23rd Aug '00, 7:23pm
"Warning: Supplied argument is not a valid MySQL result resource in /home/progtest/public_html/testlayout.php on line 7"
which would be in here "$result = mysql_fetch_array($results);"
Stallion
Thu 24th Aug '00, 5:20am
That code should work. Check your table/field names to be sure that they're 100% correct.
Powered by vBulletin™ Version 4.0.0 Beta 4 Copyright © 2009 vBulletin Solutions, Inc. All rights