Skip to content Skip to sidebar Skip to footer

I Can't Find The Mistake In My Php Script

I want to write a script in PHP that takes the current URL of the page, searches it in the database (phpMyAdmin with WAMP) and prints the ID of the row with that URL. I have writte

Solution 1:

You did not run the query with mysql_query() and you do not setou variable $curPageURL.

<!DOCTYPE html><html><body><?phpfunctioncurPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return$pageURL;
 }
 echo curPageURL();

 //connection$con= mysql_connect("localhost","root","") ordie ("Could not connect");
 mysql_select_db("search") ordie ("Could not select db");
 echo"connection succesful";

 $query = "SELECT id FROM search WHERE link = '". curPageURL() . "'";
 $result = mysql_query($query);

 while($row = mysql_fetch_array($result)) {
 echo$row;
 }
 ?></body></html>

Please note that the method you have used is deprecated from php 5.5.0. so i suggest you consider mysqli or PDO. examples can be found in below php manual links

http://www.php.net/manual/en/mysqli.query.php

http://www.php.net/manual/en/pdo.query.php

Solution 2:

You might want to try something like this.

$query = "SELECT id FROM search WHERE link = '". curPageURL() . "'";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {
    echo$row['id'];
}

Post a Comment for "I Can't Find The Mistake In My Php Script"