我现在已经玩了几个小时。我是PHP的新手,想要设置一些函数,通过选择一个字段来从数据库表中提取信息,如果另一个字段的名称被检出。
database example 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();
答案 0 :(得分: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();
?>