Jump to content


Photo

$_SERVER['PHP_SELF'] problems


  • Please log in to reply
2 replies to this topic

#1 bertieboy_93

bertieboy_93
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 13 August 2006 - 03:52 PM

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.

<?
//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);
?>

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.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 13 August 2006 - 04:02 PM

Give this a try:

<?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);
?>

Legend has it that reading the manual never killed anyone.
My site

#3 bertieboy_93

bertieboy_93
  • Members
  • PipPip
  • Member
  • 13 posts

Posted 13 August 2006 - 04:12 PM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users