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>
<?php
function curPageURL() {
$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","") or die ("Could not connect");
mysql_select_db("search") or die ("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
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"