Jump to content

Archived

This topic is now archived and is closed to further replies.

bertieboy_93

$_SERVER['PHP_SELF'] problems

Recommended Posts

Hi. I'm a newbie to these forums and to PHP, so please forgive me if the answer is something obvious.

I am using a mysql database to store text which will be loaded onto my web pages. I want to use $_SERVER['PHP_SELF'] in order to identify the page that is currently open, and then match it up to the correct entry in the database. (by identifying the current page URL, and then matching that up to the URL field in the database). The problem is that I am receiving a Parse error, and it always identifies the line which contains $_SERVER['PHP_SELF'] as the culprit. The exact error is: Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING...

My PHP code is below.

[code]
<?
//Define variables
$username="XXX";
$password="XXX";
$database="XXX";
$URL="$_SERVER['PHP_SELF']";
//Connect to database
mysql_connect(localhost,$username,$password);
@mysql_select_db($database)or die(mysql_error());
//Select information from database
$query = "SELECT PageHeading FROM Main WHERE PageURL=$URL";
$result = mysql_query($query);
if (!$result) {
  die('Could not query:' . mysql_error());
}
//Display text
echo mysql_result($result,0);
?>
[/code]

I tried manually typing in the URL rather than using $_SERVER['PHP_SELF'] but this returned the MySQL error:
Could not query:Unknown column 'index.php' in 'where clause'

The only thing that does work is manually typing in the Page_ID (primary key).

Does anyone know a solution where I can use a script to determine the current page and then retrieve the correct entry from a MySQL database.

I'd be incredibly grateful to anyone that could help.

Share this post


Link to post
Share on other sites
Give this a try:

[code]<?php
//Define variables
$username="XXX";
$password="XXX";
$database="XXX";
$URL=$_SERVER['PHP_SELF']; // edited
//Connect to database
mysql_connect(localhost,$username,$password);
mysql_select_db($database)or die(mysql_error()); // edited
//Select information from database
$query = "SELECT PageHeading FROM Main WHERE PageURL='$URL'"; // edited
$result = mysql_query($query) or die("error: ". mysql_error(). " with query ". $query); // edited
/* ignore redundant code
if (!$result) {
die('Could not query:' . mysql_error());
}
*/
//Display text
echo mysql_result($result,0);
?>[/code]

Share this post


Link to post
Share on other sites
Great! That worked perfectly. It looks like there was quite a lot wrong with my code! I haven't got much knowledge of PHP or MySQL, I've just been following through numorous online tutorials. Thanks for such a speedy response, your help has been very valuable to me.

Thanks again.
Rob.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.