从数据库表中提取字段数据的简单函数


Simple function to pull field data from a database table

我已经玩了几个小时了。我是PHP的新手,我想设置一些函数,通过在另一个字段的名称被检出时选择一个字段,从数据库表中提取信息。

数据库示例http://img825.imageshack.us/img825/3770/pn4.png

这就是我一直在玩的东西。我不确定我现在在做什么。我已经尝试了太多不同的代码组合,以至于我不确定什么会继续工作。

function getSiteTitle() {
    include("imp/connect.php");
    $siteTitle = "SiteTitle";
    $query = $db->prepare("SELECT Value FROM settings WHERE Name = ?");
    $query->bind_param('s', $siteTitle);
    $query->execute();
    $setting = $query->fetch();
    echo $setting;
}

如果有任何帮助,我将不胜感激。

更新:我做到了,但真的不知道是怎么做到的。这是更新后的代码:

function getSiteTitle() {
    include("load.php");
    $siteTitle = "SiteTitle";
    $query = $db->prepare('SELECT `Value` FROM `Settings` WHERE `Name` = ?') or die (mysqli_error($db));
    $query->bind_param('s', $siteTitle);
    $query->execute();
    $query->bind_result($Value);
    $query->fetch();
    echo $Value;        
}
echo getSiteTitle();
please try with the following ,check your connection variables are correct.
<?php
function fetch_data(){
$user="root";
$pass="";
$dsn='mysql:dbname=mydata;host=localhost;';
try{
    $db =new PDO($dsn,$user,$pass);
}
catch(PDOException $e){
    echo "connection failed ".$e->getMessage();
}
//replace the code block from $user to end of catch statement because that might be within //your included file connect.php
$query=$db->prepare("SELECT Value FROM settings WHERE Name = ?");
$name="SiteSlogan";//or SiteTitle
$query->bindParam(1,$name, PDO::PARAM_STR);
$query->execute();
$query->bindColumn("Value",$value);
$query->fetch();
echo $value;
// or you can add more columns in bincolumn like 
//$query->bindColumn("id",$id);
//echo $id;
}
fetch_data();
?>