从数据库中选择语句

时间:2014-12-02 11:08:41

标签: php mysql

我正在努力使用以下代码。我的最终目的是创建一个短div,显示数据库中表的信息。然而,它不起作用。我是否还必须连接到特定的数据库?

<html>
<head>
</head>

<body>


<?php

$dbName = 'localhost';
$userName = 'root';
$passWord = 'mysql';


 $conn = mysql_connect($dbName, $userName, $passWord);

// Check connection
 if (!$conn) 

{
die("Connection failed: " . $conn->connect_error);
} 

echo "Connected successfully";
?>



 <div id="displayAuthor">

<?php

$sql_statement = "
SELECT ssn, lastname, firstname 
  FROM author 
 ORDER 
    BY lastname, firstname
";

$result = mysql_query($sql_statement);

$outputDisplay = "";

if(!$result)
{
    $outputDisplay .= "Error";
} else

{
    $outputDisplay = "<h3> Table author data </h3>";
    $outputDisplay .= "<tr><th>SSN</th> <th>Last name </th> <th> First name</th> </tr>";
    $numberResults = mysql_num_rows($result);

    for ($i=0; $i<$numberResults; $i++)
    {
        //Dit is een counter van hoeveel rijen het uiteindelijk was.

        $row = mysql_fetch_array($result);

        $ssn = $row['ssn'];
        $lastname = $row['lastname'];
        $firstname = $row['firstname'];

        $outputDisplay .="<td>".$ssn."</td>";   
        $outputDisplay .="<td>".$lastname."</td>";
        $outputDisplay .="<td>".$firstname."</td>";

        $outputDisplay .= "</tr>";
    }
$outputDisplay .="</table>";

}
print $outputDisplay;
?>

</div>

</body>
</html>

2 个答案:

答案 0 :(得分:1)

试试这个..

 <?php

         $conn = mysql_connect('localhost', 'root', 'mysql') or  die("Connection failed: " . $conn->connect_error);
         $db = mysql_select_db(DB_NAME) or die("Couldn't select database.");
        echo "Connected successfully";
        ?>

       <div id="displayAuthor">
    <?php

        $sql_statement = "SELECT ssn, lastname, firstname FROM author ORDER BY lastname ASC";
        $result = mysql_query($sql_statement);

        if(!$result)
           $outputDisplay .= "Error";
        else
        {
            $outputDisplay = "<h3> Table author data </h3>";
            $outputDisplay .= "<tr><th>SSN</th> <th>Last name </th> <th> First name</th> </tr>";
            $numberResults = mysql_num_rows($result);

          while ($row = mysql_fetch_array($result))
         {
                $outputDisplay .="<tr><td>".$row['ssn']."</td>";   
                $outputDisplay .="<td>".$row['lastname']."</td>";
                $outputDisplay .="<td>".$row['firstname']."</td></tr>";
            }
        $outputDisplay .="</table>";

        }
        echo $outputDisplay;
        ?>
     </div>

答案 1 :(得分:0)

试试这个

<html>
<head>
</head>

<body>


<?php

$dbName = 'localhost';
$userName = 'root';
$passWord = 'mysql';


 $conn = mysql_connect($dbName, $userName, $passWord);

// Check connection
 if (!$conn) 

{
die("Connection failed: " . $conn->connect_error);
} 

echo "Connected successfully";
?>



 <div id="displayAuthor">

<?php

$sql_statement = "SELECT ssn, lastname, firstname FROM author ORDER BY lastname, firstname ";

$result = mysql_query($sql_statement);
     = mysql_num_rows($result);
$outputDisplay = "";

if($numberResults==0)
{
    $outputDisplay .= "No Data";
} 
else
{
$outputDisplay="";
    $outputDisplay. = "<h3> Table author data </h3>";
    $outputDisplay .= "<table><tr><th>SSN</th> <th>Last name </th> <th> First name</th> </tr>";


    while ($row = mysql_fetch_array($result);)
    {
        //Dit is een counter van hoeveel rijen het uiteindelijk was.

       $ssn = $row['ssn'];
        $lastname = $row['lastname'];
        $firstname = $row['firstname'];

        $outputDisplay .="<tr><td>".$ssn."</td>";   
        $outputDisplay .="<td>".$lastname."</td>";
        $outputDisplay .="<td>".$firstname."</td>";

        $outputDisplay .= "</tr>";
    }
$outputDisplay .="</table>";

}
print $outputDisplay;
?>

</div>

</body>
</html>