为什么它不对我有用

时间:2015-03-15 20:13:47

标签: php mysql

我想得到名称headerBackgroud的背景颜色并且它没有打印任何东西,你能帮帮我吗?

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "DB#1";

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT backgroundColor FROM background WHERE Name = 'headerBackground'";
$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)) {
    echo $row['backgroundColor'];
}

1 个答案:

答案 0 :(得分:0)

你混淆了mysqli和mysql api,所以只需按照给定的方式统一使用PDO:

$dbc = new PDO('mysql:host=localhost;dbname='.$database, $user, $password);
$sql = "SELECT backgroundColor FROM background WHERE Name = :n"; 
$stmt = $dbc->prepare($sql);
$stmt->bindParam(':n', "headerBackground");
$stmt->execute();
if($stmt->rowCount() > 0){ 
  $data = $stmt->fetch(PDO::FETCH_ASSOC);
  $headerB = $data['headerBackground'];
}