如何从数据库中获取数据? [关闭]
问题描述:
A guy did some php/database work for me a while back. He linked the pages to a menu (which had a separate function/table). I'm a little lost on how it's beneficial to link everything to a menu like that.
Here's the code I'm trying to figure out:
function writeContent($page){
$parentQ = "select * from cdi_content where page=\"$page\"";//query to obtain content for this page
$parentResult = mysql_query($parentQ);//run query
$link = mysql_fetch_assoc($parentResult);//result
echo $link['content'];//write content`
}
With <?php writeContent("DDesign/index.php");?>
on the index.php page he created.
From what I understand it runs through the menu function, but I'm just trying to get it to print what's in the "content" field in the database.
答
First check how many number of rows are there in the query. Replace that piece if code with this.
function writeContent($page){
$parentQ = "select * from cdi_content where page=\"$page\"";//query to obtain content for this page
$parentResult = mysql_query($parentQ);//run query
$total = mysql_num_rows($parentResult);
if($total > 1) {
while($rows = mysql_fetch_array($parentResult) {
echo "Content: ".$row['content'] . "<br/>";
}
}else{
$rows = mysql_fetch_assoc($parentResult);//result
echo $rows['content'];//write content`
}
}
答
Replace
select * from cdi_content where page=\"$page\"
For
select content from cdi_content where page=\"$page\"
Or whatever is the field name
答
To get the content of variable $link
use:
print_r($link);