White_Lily Posted September 9, 2012 Share Posted September 9, 2012 Hi, I made a bit of code that would get a pages title and content from a database, however every time i put this bit of code into the file, and upload that file, i then get an "Internal Server Error (500)". Whereas if i take the code out, and uplaod the file - the webpage is fine? <?php $sql = "SELECT * FROM pages WHERE title = '$page'"; $res = mysql_query($sql); if($row = mysql_fetch_assoc($res) { $title = $row["title"]; $content = $row["content"]; echo "<h1>".$title."</h1>"; echo $content; } else { echo "<h1>Oops...</h1>"; echo "<p>This page doesn't appear to exsist!</p>"; echo "<p>Use the 'Quick Contact' box to contact the webmaster and tell them about the page you are requesting.</p>"; } ?> Not sure where ive gone wrong. Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/ Share on other sites More sharing options...
dodgeitorelse3 Posted September 9, 2012 Share Posted September 9, 2012 where does it connect to the database? Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/#findComment-1376468 Share on other sites More sharing options...
premiso Posted September 9, 2012 Share Posted September 9, 2012 On this line: if($row = mysql_fetch_assoc($res) You are missing the closing if ) it should be: if($row = mysql_fetch_assoc($res)) If you had display_errors on, it should have alerted you of the error vs just 500ing. You could also check the apache error logs, if php is set to log to it. Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/#findComment-1376469 Share on other sites More sharing options...
White_Lily Posted September 9, 2012 Author Share Posted September 9, 2012 the connection is established in "connect.php". thanks premiso the page is now displaying however its is now echoing the "else" statement - the rows that were specified do actually exsist in the database. and they have content. http://tutorials.janedealsart.co.uk <-- This is the "else" statement im getting. Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/#findComment-1376472 Share on other sites More sharing options...
darkfreaks Posted September 9, 2012 Share Posted September 9, 2012 cleaned up your code snippet let us know if it works any better. <?php $page= $_POST['page']; $sql = 'SELECT * FROM pages WHERE title = "'.$page.'"'; $res = mysql_query($sql); while($row = mysql_fetch_assoc($res)){ $title = $row["title"]; $content = $row["content"]; echo "<h1>".$title."</h1>"; echo $content; } if(mysql_num_results($row)===0) { echo "<h1>Oops...</h1>"; echo "<p>This page doesn't appear to exsist!</p>"; echo "<p>Use the 'Quick Contact' box to contact the webmaster and tell them about the page you are requesting.</p>"; } Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/#findComment-1376485 Share on other sites More sharing options...
White_Lily Posted September 9, 2012 Author Share Posted September 9, 2012 wont "$_POST["page"]" only work if there was a form prior to this line? Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/#findComment-1376487 Share on other sites More sharing options...
darkfreaks Posted September 9, 2012 Share Posted September 9, 2012 i defined page because it was not defined do you have it defined somewhere else Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/#findComment-1376488 Share on other sites More sharing options...
White_Lily Posted September 9, 2012 Author Share Posted September 9, 2012 my $page variable is always defined before the doctype, this way i can use it in the pages <title> tags. Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/#findComment-1376489 Share on other sites More sharing options...
darkfreaks Posted September 9, 2012 Share Posted September 9, 2012 as long is it is defined the code should work. Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/#findComment-1376490 Share on other sites More sharing options...
White_Lily Posted September 9, 2012 Author Share Posted September 9, 2012 Anyway - a few errors poped up with your code, however i fixed them and my problem is solved. Fixed code: <?php include "sql/conf.php"; include "sql/connect.php"; $page = "Welcome"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[url=http://www.w3.org/TR/html4/loose.dtd]http://www.w3.org/TR/html4/loose.dtd[/url]"> <html> <head> <title><?php echo $GLOBALS["siteName"]." - ".$page; ?></title> <link rel="stylesheet" type="text/css" href="css/main.css"> <link rel="stylesheet" type="text/css" href="css/sub-css.css"> <link rel="stylesheet" type="text/css" href="css/tags.css"> </head> <body> <div class="wrapper"> <div class="header"> <div class="head"> <img src="images/logo.png" alt="HTML Bites Logo"> <> <> <div class="sub-wrapper"> <div class="navigation"> <div id="navigation"> <a href="index.php">Home</a> <a href="#">News</a> <a href="#">Gallery</a> <a href="#">Tutorials</a> <a href="#">Downloads</a> <a href="#">Contact</a> <> <> <div class="content"> <div class="center-column"> <?php //$page_name = $_POST['page']; $sql = 'SELECT * FROM pages WHERE title = "'.$page.'"'; $res = mysql_query($sql); while($row = mysql_fetch_assoc($res)) { $title = $row["title"]; $content = $row["content"]; echo "<h1>".$title."</h1>"; echo $content; } if(mysql_num_rows($res) == 0) { echo "<h1>Oops...</h1>"; echo "<p>This page doesn't appear to exsist!</p>"; echo "<p>Use the 'Quick Contact' box to contact the webmaster and tell them about the page you are requesting.</p>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/268185-getting-500-internal-server-error-not-sure-why/#findComment-1376491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.