PHP代码没有获得MySQL表值

时间:2015-02-23 20:39:39

标签: php html mysql

我已经看过很多类似这样的问题,但没有一个能真正帮助我。我试图编写一些代码来从MySQL表中获取值并将其打印出来。每当我尝试运行代码时,它既不会运行它,只是显示原始代码,或者它返回MySQL错误。我的PHP如下:

 <?php
 $servername = "localhost";
 $username = "root";
 $password = "toughtohackpassword";
 $dbname = "weirddb";

$con = mysqli_connect($servername, $username, $password, $dbname);


$sql = "SELECT * FROM `class1`";


$result = mysql_query($sql, $con);


if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " . mysql_error();
    exit;
}


$data = "";
while ($row = mysql_fetch_assoc($result)) {
    $data .= "<tr>
                <td>" . $row['name'] . "</td>
                <td>" . $row['score'] . "</td>


              </tr>";
 }

exit;

 ?>

顺便说一句,错误信息是“无法成功运行查询(SELECT * FROM class1 from DB:&#39;。

我知道这个问题非常笼统,所以请告诉我是否需要添加任何内容!

5 个答案:

答案 0 :(得分:0)

$con = mysqli_connect($servername, $username, $password, $dbname);
            ^----mysql **I**

$result = mysql_query($sql, $con);
               ^---mysql **WITHOUT** I

如果您对代码的错误处理几乎完全没有问题,而不是盲目地假设没有任何错误,那么您会被告知您的查询失败,因为没有与数据库的连接:

$result = mysql_query($sql, $con) or die(mysql_error());
                                 ^^^^^^^^^^^^^^^^^^^^^^

答案 1 :(得分:0)

基本上看起来你正在将mysql与mysqli混合

    <?php
 $servername = "localhost";
 $username = "root";
 $password = "toughtohackpassword";
 $dbname = "weirddb";

$con = mysqli_connect($servername, $username, $password, $dbname);


$sql = "SELECT * FROM `class1`";


$result = mysqli_query($con,$sql) or die(mysqli_error($con));



$data = "";
while ($row = mysqli_fetch_assoc($result)) {
    $data .= "<tr>
                <td>" . $row['name'] . "</td>
                <td>" . $row['score'] . "</td>


              </tr>";
 }

exit();

 ?>

答案 2 :(得分:0)

<?php
 $servername = "localhost";
 $username = "root";
 $password = "toughtohackpassword";
 $dbname = "weirddb";

$con = mysqli_connect($servername, $username, $password, $dbname);


$sql = "SELECT * FROM `class1`";


$result = mysqli_query($sql, $con);


if (!$result) {
    echo "Could not successfully run query ($sql) from DB: " .     mysql_error();
   exit;
   }


$data = "";
while ($row = mysqli_fetch_assoc($result)) {
    $data .= "<tr>
               <td>" . $row['name'] . "</td>
              <td>" . $row['score'] . "</td>


             </tr>";
 }

exit;

 ?>

您使用MySQLi连接数据库,因此您必须使用&#34; MySQLi&#34;而不是&#34; MySQL&#34;在功能

答案 3 :(得分:0)

Java Java Thunder Man,

<?php
     $servername = "localhost";
     $username = "root";
     $password = "toughtohackpassword";
     $dbname = "weirddb";

     $con = mysql_connect($servername, $username, $password, $dbname);
     //---Make sure those variables for connecting DB are correct
     //And please using one DB connection method (PDO/mysql/mysqli so on)

     $sql = mysql_query("SELECT * FROM `class1`") or die(mysql_error());

     $data = "<html>";
     $data .= "<body>";
         $data .= "<table>";
         if(mysql_num_rows($sql) > 0) {//list the result if rows of $sql > 0
              while ($rows = mysqli_fetch_assoc($sql)) {
                   $data .= "<tr>";
                   $data .= "<td>" . $row['name'] . "</td>";
                   $data .= "<td>" . $row['score'] . "</td>";
                   $data .= "</tr>";
              }
         }
         $data .= "</table>";
     $data .= "</body>";
     $data .= "</html>";
     echo $data; //Show the results by using echo

 ?>

我也是PHP新手,我不确定错误出现在哪一行,除了你没有回应$ data。 此外,你能说清楚错误吗?感谢。

答案 4 :(得分:0)

运行此选项,如果您尝试查询的表和列名称中没有拼写错误,它应该适用于您。

<?php
 $servername = "localhost";
 $username = "root";
 $password = "toughtohackpassword";
 $dbname = "weirddb";

$con = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT * FROM class1";

$result = $con->query($sql);

if ($con->error) {

    die("Could not connect to database");

} else {

    $data = "";

    while ($row = mysqli_fetch_assoc($result)) {

        $data .= "<tr>
                    <td>" . $row['name'] . "</td>
                    <td>" . $row['score'] . "</td>
                  </tr>";

     }

    echo $data;
    exit;
}

 ?>