我遇到了一个问题,当我试图从我的数据库显示数据,现在我从来没有遇到过这个问题,它不会显示我任何错误!有人能看出问题所在吗?任何和所有的帮助都是感激的。
<!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>
请看看你是否能找到为什么它不让我显示数据
您正在传递$pagename
而不是$recipeID
给您的查询。你的查询应该是:
$selectRecipeQuery="SELECT recipe_name,food_type_english FROM recipe WHERE recipe_id='$recipeID'";
通过向查询传递错误的值,您将不会得到没有错误的结果。
这个怎么样?
把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>