<?php
//Sessions starten
session_start();
//Include stuff
include('conf.php');
//'Shortcuts' maken
$sname = $site['name'];
$shost = $site['host'];
$shostb = $site['hostb'];
//Verbinding maken met DB
$link = mysql_connect($database['host'], $database['username'], $database['password']) or die (mysql_error());
mysql_select_db($database['db'],$link) or die(mysql_error());
//Gegevens uit db halen
if(isset($_GET['page'])){
$page['title'] = mysql_query("SELECT title FROM pages WHERE page = $_GET[page]");
}
else{
$page['title'] = mysql_query("SELECT title FROM pages WHERE page = 'index.php'");
}
?>
<!doctype html>
<html lang="nl">
<head>
<meta charset="utf-8" />
<title><?php echo $sname;?>•<?php echo $page['title']?></title>
有人知道为什么这不起作用?我的数据库结构是:
page | title
的index.php |索引
抱歉,我无法理解这一点。类型为$page['title'] = resource(5)
的var 转储(mysql结果)。我怎样才能解决这个问题?谢谢你的帮助。
答案 0 :(得分:7)
mysql_query
返回资源,您需要在资源上使用mysql_fetch_array
(或类似的,例如mysql_fetch_assoc
)来获取标题值。
例如:
$res = mysql_query("SELECT title FROM pages WHERE page = 'index.php'");
$page = mysql_fetch_assoc($res);
// $page['title'] now contains the value (assuming there's an index.php page in the DB).
警告:您的查询容易受到SQL注入攻击。
答案 1 :(得分:0)
这是由mysql_query
返回的资源ID,在资源上使用mysql_fetch_assoc
来获取标题值。
进行以下更改:
if(isset($_GET['page'])){
$result = mysql_query("SELECT title FROM pages WHERE page = $_GET[page]");
}
else{
$result = mysql_query("SELECT title FROM pages WHERE page = 'index.php'");
}
$row= mysql_fetch_assoc($result);
$page['title'] =$row['title'];
答案 2 :(得分:0)
您需要使用mysql_fetch_assoc()
或mysql_result()
等功能来获取实际数据。
if(isset($_GET['page'])){
$result = mysql_query("SELECT title FROM pages WHERE page = '" . mysql_real_escape_string($_GET[page]) . "'");
$page['title'] = mysql_result($result, 0, 'title');
}
else{
$result = mysql_query("SELECT title FROM pages WHERE page = 'index.php'");
$page['title'] = mysql_result($result, 0, 'title');
}
答案 3 :(得分:0)
这是 4年后 strong>,但不推荐使用mysql_ *。您应该使用mysqli_ *或PDO函数