2014-12-02 86 views
-1

Im正在努力处理以下代码。我的最终目标是创建一个简短的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> 
+1

需要使用mysql_select_db来选择你的数据库。但不建议使用mysql_ *函数。改用pdo或mysqli_ *函数。 – fortune 2014-12-02 11:10:38

+5

扔掉这个并使用[PDO](http://php.net/pdo)或[mysqli](http://php.net/mysqli);然后再问一次。 – 2014-12-02 11:10:40

+0

您可以连接到特定数据库,也可以在指定表格时指定数据库,即查询中的FROM FROM MyDatabase.MyTable – Eilidh 2014-12-02 11:11:06

回答

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