【问题标题】:Can't display data from my DATABASE Server无法显示来自我的数据库服务器的数据
【发布时间】:2013-09-21 04:07:55
【问题描述】:
<?php
//connection to the database 
try {     
  $pdo = new PDO('mysql:host=localhost;dbname=frostedc_movies;charset=utf8',
                    frostedc_user, 'pass'); 
 echo "connected"; 
}  
catch(PDOException $e) {  
 echo $e; //2 
} 

// select everything from the news table
$query = "SELECT * FROM movie";// Table name NOT database name 

foreach ($pdo->query($query) as $row) {
    echo "<table border='1'>";
    echo "<tr>";
        echo "<td width='150'>".$row['movietitle']."</td>";
        echo "<td width='150'>".$row['genre']."</td>";
        echo "<td width='150'>".$row['LastViewed']."</td>";
        echo "<td width='150'>".$row['Location']."</td>";
    echo "</tr>";

}
echo "</tr>";
echo "</table>";
echo "<br>";
echo "
    <form>
        <p>Please Enter a Movie Title</p>
        <input type='text' name='new_movie' />
        <input type='submit' value='Submit' />
    </form>";
echo "
    <form>
        <p>Please Enter the Genre</p>
        <input type='text' name='movie_genre' />
        <input type='submit' value='Submit' />
    </form>";
echo "
    <form>
        <p>Please Enter the Last View Date</p>
        <input type='text' name='last_view' />
        <input type='submit' value='Submit' />
    </form>";
echo "
    <form>
        <p>Please Enter the Location</p>
        <input type='text' name='movie_loca' />
        <input type='submit' value='Submit' />
    </form>";




$pdo = null;

?>

这是新的更新代码。我正在尝试使用输入将数据输入到我的数据库中。 我已经研究过如何做到这一点,但到目前为止我还没有任何工作。有什么想法吗?我是否更容易使用包含并在 html 中进行输入?如果可以,我可以使用它们将数据输入数据库吗?

【问题讨论】:

标签: php html mysql database pdo


【解决方案1】:

你在这里做错了两件事

第一:混合 PDOMySQL
第二: $query = "SELECT * FROM myDB"; 您无法从数据库中进行选择。您需要从 TABLE 中进行选择!(您确定 myDB 是您的表吗?)

【讨论】:

    【解决方案2】:

    正如您标记了您的问题 PDO,我已从您的示例中删除了所有不需要的代码。

    <?php
    //connection to the database 
    try {     
        $pdo = new PDO('mysql:host=localhost;dbname=frostedc_movies;charset=utf8',
                        user, 'password'); //1
        echo "connected"; 
    }  
    catch(PDOException $e) {  
        echo $e; //2 
    } 
    // select everything from the news table
    $query = "SELECT * FROM myTable";// Table name NOT database name 
    echo "<table>";
    echo "<tr>";
    foreach ($pdo->query($query) as $row) {//3
        echo "<td>".$row['movietitle']."</td>";
        echo "<td>".$row['genre']."</td>";
        echo "<td>".$row['LastViewed']."</td>";
        echo "<td>".$row['Location']."</td>";
    }
    echo "</tr>";
    echo "</table>";
    // disconnect from the database
    $pdo = null;//5
    ?>
    

    数字cmets

    1 设置字符集Manual

    2 仅用于开发的回显错误消息。在生产中,您应该对其进行处理或删除 try/catch。

    3 由于没有参数使用query()

    4 对于 utf8PHP 5.3.6之前

    5 将mysql_close();(mysql_) 更改为$pdo = null; (PDO)

    【讨论】:

    • 谢谢!我正在尝试学习 PHP 和 PDO 和 MYSQL 来访问我的数据库,但正如你所知道的那样,我并没有那么热......
    • 我收到一个错误警告:mysql_close(): no MySQL-Link resource provided in /home/frostedc/public_html/test/index.php on line 24
    • @NathanielMiller 对不起,我错了。没有提供关闭数据库连接的正确代码。见编辑
    • 没关系,非常感谢您的帮助!有什么建议可以让我学习如何更好地执行上述代码吗?我试图能够使用用户输入添加和更改表中的信息。到目前为止,我对我看过的所有景点都感到困惑
    • @NathanielMiller 这是一个很好的tutorial。如果您觉得我的回答有用,您可以接受。您可以随时提出新问题,只要确保您提供代码、任何错误等。
    【解决方案3】:
    <?php // connect to the database
      $host = 'localhost';
      $username = 'user';
      $pass = 'password';
      $conn=mysql_connect($host,$username,$pass)or die(mysql_error());
      mysql_select_db("myDB");
      // select everything from the news table
      $query = "SELECT * FROM news";
      $result = mysql_query($query);
    
      while($row = mysql_fetch_array($result)){
    
        echo "<table>
                <tr>
                  <td>".$row['movietitle']."</td>
                  <td>".$row['genre']."</td>
                  <td>".$row['LastViewed']."</td>
                  <td>".$row['Location']."</td>
                </tr>
              </table>";
      }
    
      // disconnect from the database
      mysql_close();
    ?>
    

    【讨论】:

    • 你有额外的(on while :-)
    【解决方案4】:

    试试这个代码

    <?php // connect to the database
    $host = 'localhost';
    $username = 'user';
    $pass = 'password';
    mysql_connect($host,$username,$pass);
    mysql_select_db("myDB");
    // select everything from the news table
    $query = "SELECT * FROM `tablename`";
    $result = mysql_query($query);
    echo "<table>";
    echo "<tr>";
    while( ($row = mysql_fetch_array($result)))
    {
    echo "<td>".$row['movietitle']."</td>";
    echo "<td>".$row['genre']."</td>";
    echo "<td>".$row['LastViewed']."</td>";
    echo "<td>".$row['Location']."</td>";
    }
    echo "</tr>";
    echo "</table>";
    // disconnect from the database
    mysql_close();
    ?>
    

    【讨论】:

      猜你喜欢
      • 2017-06-13
      • 2016-10-05
      • 2020-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多