我想在PHP中编写一个脚本,它接受页面的当前URL,在数据库中搜索它(带有WAMP的phpMyAdmin)并打印带有该URL的行的ID。我已经写了这个,并且到了连接点,everythwing工作,与mysql的连接也工作正常,问题从这一行开始:
$query = "SELECT id FROM search WHERE link = '". $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 . "'";
while($row = mysql_fetch_array($query)) {
echo $row;
}
?>
</body>
</html>
答案 0 :(得分:1)
您没有使用mysql_query()
运行查询,也没有设置变量$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>
请注意,您使用的方法已从PHP 5.5.0中弃用。所以我建议你考虑mysqli或PDO。示例可以在下面的php手册链接中找到
答案 1 :(得分:1)
您可能想尝试这样的事情。
$query = "SELECT id FROM search WHERE link = '". curPageURL() . "'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
echo $row['id'];
}