G'd evening,

So I built this script and figured that I had it all figured out, styled it with css only to find out something is messed up.

The php script is set so that it retrieves a value from a mySQL database and if it is 0 the site is offline if it is 1 it is online. When it is offline it displays a div with a logo with a set of text, when it is online it displays the site. This is accomplished using an if and else setup. The problem is the div with the logo and text that is supposed to display when the site is offline is now displaying over top of the site that is supposed to display when it is online.

Here is the code

<html>
<head>
<title>Title Here</title>
	<link rel="stylesheet" href="/offline/maintenance.css" type="text/css" />
</head>
<body>
<?php 
// Make a MySQL Connection
mysql_connect('localhost', 'dbuser', "dbpass") or die(mysql_error());
mysql_select_db('dbname') or die(mysql_error());

// Retrieve all the data from the "example" table
$result = mysql_query('SELECT * FROM sitesettings')
or die(mysql_error());  
while($row = mysql_fetch_object($result)) {
    if ($row->site_on_off=='0')
	echo '<div id="maintenance">
		<img src="offline/logo.png" title="Logo" alt="Logo" />
		<br>
		<h5>
			To bring our clients the best possible service from time to time we will pull our website down for maintenance and upgrades.
			Thank you for your patience. 
			<br><br>
			If this is urgent please contact us at ***-***-**** or by email.
		</h5>
		</div>
		';
else
	include("subdirectory/index.php"); 
}
mysql_free_result($result);

?>
</body>
</html>

As you can see what my intentions are is that if the database says the site setting is 0 - the site is offline so it displays the logo + text if the database says anything else it is to include the site which is located within a subdirectory.

Any ideas where I've gone wrong?

Recommended Answers

All 7 Replies

I have changed a bit.

<html>
<head>
<title>Title Here</title>
	<link rel="stylesheet" href="/offline/maintenance.css" type="text/css" />
</head>
<body>
<?php 
// Make a MySQL Connection
mysql_connect('localhost', 'dbuser', "dbpass") or die(mysql_error());
mysql_select_db('dbname') or die(mysql_error());

// Retrieve all the data from the "example" table
$result = mysql_query('SELECT * FROM sitesettings')
or die(mysql_error());  
$row = mysql_fetch_object($result);
if($row->site_on_off=='0') // site is OFFLINE
{
	echo '<div id="maintenance">
		<img src="offline/logo.png" title="Logo" alt="Logo" />
		<br>
		<h5>
			To bring our clients the best possible service from time to time we will pull our website down for maintenance and upgrades.
			Thank you for your patience. 
			<br><br>
			If this is urgent please contact us at ***-***-**** or by email.
		</h5>
		</div>
		';
	exit;
}
mysql_free_result($result);
include("subdirectory/index.php"); 
?>
</body>
</html>
commented: Great help +1

Yay it worked, now please excuse me cause my coffee has not kicked in yet this morning. But I'd like to ask you why yours worked, no since asking a question and getting an answer if you don't learn from it.

First you removed while which may have been redundant but my understanding behind using while is because it functions as a simple loop the loop itself (mysql fetching row info) would just cease to work, maybe I don't quite understand the purpose itself?

Am I correct in thinking that you exited the if and moved the include past the mysql_free_result($result); is because free_result is supposed to clear the value returned from the database?

Sorry if these all seem like rookie questions, I've worked on plenty of cms backends before that were powered by php but I'm trying to drastically improve upon my php coding (as its beginner at best).

Yes, as you need only one row, a while loop is not necessary.
As for the other change, I prefer the original version. If the site is down now, the code will be invalid (the body and html closing tags are missing - you could also add them in the string you echo).
What would IMO be even cleaner is to but the php on top, put the error message in a seperate file, and decide in this file only which to include, but it doesn't really matter all that much.

commented: Huge help +1

That is the plan to get to that point eventually.

So then when is while() to be used?

Also according to what I have the body and html tags still do show up

Use while if you need more than one row from the database.

Thank you both for the help, it makes alot more sense hearing it on here then reading it on a tutorial or in a book for some reason.

Refer this code with comment.
You will understand it.

<? mysql_connect("localhost",'root','');
	mysql_select_db('test');
	
	$sql = "select * from student";
	$result = mysql_query($sql) or die(mysql_error());
	$row = mysql_fetch_object($result);
	echo '<pre>';
	print_r($row); // this will print first student
	// once you use mysql_fetch_object, first record from$result is removed. 
	// so when second time you use mysql_fetch_object it gives second record.
	
	$row = mysql_fetch_object($result);
	echo '<pre>';
	print_r($row); // this will print second student
	
	$row = mysql_fetch_object($result);
	echo '<pre>';
	print_r($row); // this will print third student
	
	// ============================================================
	// UFFF so many lines of code to fetch many data, better to use while
	// Here we begin smart way
	
	$sql = "select * from student";
	$result = mysql_query($sql) or die(mysql_error());
	while($row = mysql_fetch_object($result))
	{
		echo '<pre>';
		print_r($row); // Hurreeyy all records in just one line
	}
	
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.