无法显示我的DATABASE服务器中的数据

时间:2013-09-21 04:07:56

标签: php html mysql database pdo

<?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;

&GT;

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

4 个答案:

答案 0 :(得分:1)

你这里做了两件错事

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

答案 1 :(得分:0)

当您标记了 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
?>

号码评论

1设置字符集Manual

2仅针对开发的回应错误消息。在生产中,您应该对其执行某些操作或删除try / catch。

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

4 utf8 PHP 5.3.6

之前

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

答案 2 :(得分:-1)

<?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();
?>

答案 3 :(得分:-1)

试试这段代码

<?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();
?>