Select *.... WHERE $PHP_SELF

Hi--
I'm trying to set up a query so that the page finds its own ID from a MySQL table.  
<?php
$reported_filename = $PHP_SELF;
      include("dbconnect.php");
      $dbconn = mysql_connect($host, $username, $password) or die ("Cannot connect to database server");
      $db = mysql_select_db("HP") or die ("Cannot connect to database");
      $Page_ID_result = mysql_query("SELECT * FROM Page_Table WHERE Page_HTML like \"%$reported_filename%\"") or die(mysql_error());


      $Page_Table_row = mysql_fetch_array( $Page_ID_result ); // Print out the contents of each row into a table
      echo'<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
            <meta name="keywords" content="'.$Page_Table_row['Page_Meta'].'">
            <title>Highland Park Market--'.$Page_Table_row['Page_Name'].'</title>
            <link href="test.css" rel="stylesheet" type="text/css"></head>';
echo 'ID'.$Page_ID_result['Page_ID'];
?>


I've seen hundreds of ways to set up the select query in PHP and in MySQL but none of them seems to allow me to insert the variable $PHP_SELF in the WHERE condition, or any variable for that matter.  

This particular set up gives me nothing at all and I'm STUMPED!  Please help!

Thank you!
getticaAsked:
Who is Participating?
 
BatalfConnect With a Mentor Commented:
I think you got the $sql variable wrong now. I few quotes is missing

 $sql = "SELECT * FROM Page_Table WHERE Page_Table.Page_HTML like $reported_filename";

should be

 $sql = "SELECT * FROM Page_Table WHERE Page_Table.Page_HTML like '%$reported_filename%'";

or

 $sql = "SELECT * FROM Page_Table WHERE Page_Table.Page_HTML = '$reported_filename'";

if you only allow an accurate match.

Batalf
0
 
BatalfCommented:
If you're using a newer version of PHP with register_globals =off(which is default), then you should use

$_SERVER['PHP_SELF'] instead of $PHP_SELF;

i.e.

$reported_filename = $_SERVER['PHP_SELF'];
0
 
getticaAuthor Commented:
Hi--Thank you for the tip--I've changed that.  Do I have to change every variable to get it to work?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
getticaAuthor Commented:
Or, would it be smart for me to enable register_globals?
0
 
BatalfCommented:
In the script above, it's enough to change

$reported_filename = $PHP_SELF;

to

$reported_filename = $_SERVER['PHP_SELF'];

All occurences of $PHP_SELF in your scripts should be changed to $_SERVER['PHP_SELF'].

Batalf
0
 
BatalfCommented:
No, it's not very smart to set register_globals to off. The default state is set to off for security reasons.

Batalf
0
 
getticaAuthor Commented:
...But it's still not working for me...

<?php
      $reported_filename = $_SERVER['PHP_SELF'];
      include("dbconnect.php");
      $dbconn = mysql_connect($host, $username, $password) or die ("Cannot connect to database server");
      $db = mysql_select_db("cait_HPMySQL") or die ("Cannot connect to database");
      $Page_ID_result = mysql_query("SELECT * FROM Page_Table WHERE Page_HTML like \"%$reported_filename%\"") or die(mysql_error());
      $Page_Table_row = mysql_fetch_array( $Page_ID_result ); // Print out the contents of each row into a table
      echo $Page_Table_row['Page_ID'];
?>
0
 
BatalfCommented:
Try to echo the query to make sure that it is correct.

example:

     $reported_filename = $_SERVER['PHP_SELF'];
     $sql = "SELECT * FROM Page_Table WHERE Page_HTML like \"%$reported_filename%\"";
     echo $sql;
     include("dbconnect.php");
     $dbconn = mysql_connect($host, $username, $password) or die ("Cannot connect to database server");
     $db = mysql_select_db("cait_HPMySQL") or die ("Cannot connect to database");
     $Page_ID_result = mysql_query($sql) or die(mysql_error());
     $Page_Table_row = mysql_fetch_array( $Page_ID_result ); // Print out the contents of each row into a table
     echo $Page_Table_row['Page_ID'];

also make sure that the field "Page_ID" is in that accurate format(case sensitive).

Also notice that $_SERVER['PHP_SELF'] returns "/fileName.php" (i.e. a slash at the start) if it's a file on the web root.

If the file is in the format "fileName.php" in your database, maybe you should replace

$reported_filename = $_SERVER['PHP_SELF'];

with
     $reported_filenameArray = explode("/",$_SERVER['PHP_SELF']);
     $reported_filename = $reported_filenameArray[count($reported_filenameArray)-1];

in your script.

Batalf
0
 
getticaAuthor Commented:
Okay--you've been most helpful, but I'm still having difficulty.  (I think I'm maxing out on PHP today!!!!)


<?php
      $reported_filenameArray = explode("/",$_SERVER['PHP_SELF']);
    $reported_filename = $reported_filenameArray[count($reported_filenameArray)-1];
      include("dbconnect.php");
      $dbconn = mysql_connect($host, $username, $password) or die ("Cannot connect to database server");
      $db = mysql_select_db("cait_HPMySQL") or die ("Cannot connect to database");
      $sql = "SELECT * FROM Page_Table WHERE Page_Table.Page_HTML like $reported_filename";
    $Page_ID_result = mysql_query($sql) or die(mysql_error());
     $Page_Table_row = mysql_fetch_array($Page_ID_result); // Print out the contents of each row into a table
     echo $Page_Table_row['Page_ID'];
?>


I get the following error: Unknown table 'coventry' in where clause

('coventry' is the name of the file, but it is strangely stripped of '.php')  
So confused...
0
 
Diablo84Commented:
>>    $reported_filenameArray = explode("/",$_SERVER['PHP_SELF']);
 >>    $reported_filename = $reported_filenameArray[count($reported_filenameArray)-1];

Interesting approach, the basename function would simplify it though...

$reported_filename = basename($_SERVER['PHP_SELF']);

Diablo84
0
 
BatalfCommented:
Yes of course. Forgot the basename() function :-) Thank you!

Batalf
0
 
getticaAuthor Commented:
Thank you so very much!  
0
 
getticaAuthor Commented:
Hi--
I have posted another question at http://www.experts-exchange.com/Web/Web_Languages/PHP/PHP_Databases/Q_21410266.html and am having trouble getting anyone to look at it.  Are you familiar with Joins?

Sorry the point value is a little low--I will offer my undying gratitude in leu of points if you can help...

Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.