1

我已经玩了几个小时了。我是 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();
4

1 回答 1

0
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();
?>
于 2013-07-29T03:49:11.607 回答