调用表行时出现Mysql PHP错误
我有一个非常简单的请求,不知道为什么它是错误的:调用表行时出现Mysql PHP错误,php,mysql,Php,Mysql,我有一个非常简单的请求,不知道为什么它是错误的: if ($_GET['linklabel'] !== '') { $query = "SELECT templateid FROM pages WHERE linklabel = {$_GET['linklabel']}"; $result=mysql_query($query); $templateid = $result['templateid']; echo $templateid;
if ($_GET['linklabel'] !== '')
{
$query = "SELECT templateid FROM pages WHERE linklabel = {$_GET['linklabel']}";
$result=mysql_query($query);
$templateid = $result['templateid'];
echo $templateid;
if ($result !== 0)
{
include($templateid.'.php');
}
else
{
include('404error');
}
}
表中的templateid的VARCHAR值为test。浏览器说它找不到test.php文件
我打错什么了吗
回显变量$templateid
也不会输出任何内容,因此我认为$templateid=$result['templateid']有问题代码>?mysql\u query()返回语句句柄,而不是实际数据。您必须先获取数据行,然后才能从查询结果中访问各个字段:
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$templateid = $result['templateid'];
还请注意,您非常容易受到sql注入攻击,如果这种攻击发生在面向公众的网站上,您可能会在很短的时间内遭到攻击。mysql\u query()返回一个语句句柄,而不是实际数据。您必须先获取数据行,然后才能从查询结果中访问各个字段:
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$templateid = $result['templateid'];
还要注意的是,您非常容易受到sql注入攻击,如果这种攻击发生在面向公众的网站上,您可能会在很短的时间内遭到攻击。尝试将$query行更改为:
$query = "SELECT templateid FROM pages WHERE linklabel = '{$_GET['linklabel']}'";
请注意附加的引号。尝试将$query行更改为:
$query = "SELECT templateid FROM pages WHERE linklabel = '{$_GET['linklabel']}'";
请注意附加的引号