?
Solved

Select *.... WHERE $PHP_SELF

Posted on 2005-05-02
13
Medium Priority
?
291 Views
Last Modified: 2013-12-12
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!
0
Comment
Question by:gettica
  • 6
  • 6
13 Comments
 
LVL 32

Expert Comment

by:Batalf
ID: 13912050
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
 

Author Comment

by:gettica
ID: 13912140
Hi--Thank you for the tip--I've changed that.  Do I have to change every variable to get it to work?
0
 

Author Comment

by:gettica
ID: 13912162
Or, would it be smart for me to enable register_globals?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 32

Expert Comment

by:Batalf
ID: 13912173
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
 
LVL 32

Expert Comment

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

Batalf
0
 

Author Comment

by:gettica
ID: 13912241
...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
 
LVL 32

Expert Comment

by:Batalf
ID: 13912334
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
 

Author Comment

by:gettica
ID: 13912744
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
 
LVL 32

Accepted Solution

by:
Batalf earned 1200 total points
ID: 13912760
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
 
LVL 27

Expert Comment

by:Diablo84
ID: 13913286
>>    $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
 
LVL 32

Expert Comment

by:Batalf
ID: 13913305
Yes of course. Forgot the basename() function :-) Thank you!

Batalf
0
 

Author Comment

by:gettica
ID: 13913492
Thank you so very much!  
0
 

Author Comment

by:gettica
ID: 13920560
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month15 days, 5 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question