【问题标题】:Having trouble displaying data from the database to my website [closed]无法将数据库中的数据显示到我的网站[关闭]
【发布时间】:2014-04-27 11:59:26
【问题描述】:

我在尝试显示数据库中的数据时遇到了一个问题,我以前从未遇到过这个问题,它不会显示任何错误!谁能看到这个问题?感谢您提供任何和所有帮助。

<!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>

请看看你是否能找出为什么它不让我显示数据

【问题讨论】:

  • 我会第一个这么说,因为总是有人这么说。在继续开发之前,请考虑切换到最新的 mysql 库。推荐的是 pdo,但也有其他的。
  • 您在这一行缺少分号: 但这是我能看到的唯一问题,除了@Chevi 已经提到的问题。您的主机是否升级了 PHP 和/或更改了 php.ini?
  • 有人删除了你的数据库吗?也许使用 SQL 注入? en.wikipedia.org/wiki/SQL_injection
  • @Chevi 我的 sql 已经过时了?我是二年级的网络学生,这是我被教导的方式,我使用为我们提供的内部服务器。你能解释一下你所说的mysql库是什么意思吗?
  • @Website_Crazy 关于这个主题有很多资源,这个问题是我发现的第一个问题:stackoverflow.com/questions/12097245/php-mysql-v-mysqli-v-pdo。基本上 mysql 库从一开始就存在,缺乏许多有用和必要的元素。

标签: php mysql


【解决方案1】:

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

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

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

【讨论】:

  • 哇我怎么没看到这个谢谢你指出这一点我已经尝试解决这个问题一个多小时了!
  • 这不是错字问题,而是否定答案(并要求投票结束)?
【解决方案2】:

这个呢?

只需将 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>

【讨论】:

    猜你喜欢
    • 2019-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 2016-01-18
    • 2013-02-21
    • 1970-01-01
    相关资源
    最近更新 更多