无法将数据从数据库显示到我的网站

时间:2014-03-20 14:41:22

标签: php mysql

当我尝试显示数据库中的数据时,我遇到了一个问题,我之前从未遇到过这个问题,它不会向我显示任何错误!任何人都可以看到这个问题?任何和所有的帮助表示赞赏。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Styles.css"/>
<?php
    error_reporting(E_ALL);
    $pagename=$_GET['Recipe'];
    $recipeID=$_GET['id'];
?>
<title><?php echo"$pagename"?></title>
</head>
<body>
<?php
    $mySqlHost='localhost';
    $mySqlUsername='';
    $mySqlPassword='';
    $mySqlDatabase='';
    $mySqlConnect=mysql_connect($mySqlHost,$mySqlUsername,$mySqlPassword) or die("Error Connecting to Database");
    mysql_select_db($mySqlDatabase,$mySqlConnect) or die ("Could not Select Database".mysql_error());

    $selectRecipeQuery="SELECT recipe_name,food_type_english FROM recipe WHERE recipe_id='$pagename'";
    $result=mysql_query($selectRecipeQuery) or die ("could not select data from table".mysql_error());

$tableRow=mysql_fetch_array($result);
        $recipe_name=$tableRow['recipe_name'];
        $food_type_english=$tableRow['recipe'];


    echo "<p>$recipe_name</p>";
    echo "<p>$food_type_english</p>";
?>
</body>
</html>

请查看是否可以找出不让我显示数据的原因

2 个答案:

答案 0 :(得分:1)

您将$pagename而不是$recipeID传递给您的查询。您的查询应该是:

$selectRecipeQuery="SELECT recipe_name,food_type_english FROM recipe WHERE recipe_id='$recipeID'";

通过将错误的值传递给您的查询,您将得到没有错误的结果。

答案 1 :(得分:0)

这个怎么样?

只需将myDatabase更改为您的数据库名称

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Styles.css"/>
<?php
    error_reporting(E_ALL);
    $pagename=$_GET['Recipe'];
    $recipeID=$_GET['id'];
?>
<title><?php echo $pagename ?></title>
</head>
<body>
<?php
try {
    $conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);   

    $stmt = $conn->prepare('SELECT recipe_name,food_type_english FROM recipe WHERE recipe_id = :id');
    $stmt->execute(array('id' => $recipeID));

    while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo "<p>".$row['recipe_name']."</p>";
        echo "<p>".$row['food_type_english']."</p>";
    }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>
</body>
</html>