2014-12-02 1 views
-1

임 다음 코드로 어려움을 겪고 있습니다. 내 마지막 목표는 데이터베이스의 테이블 정보를 표시하는 짧은 div를 만드는 것입니다. 그러나 그것은 작동하지 않습니다. 특정 데이터베이스에 연결해야합니까?데이터베이스에서 Select 문

<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_ * 함수의 사용은 권장하지 않습니다. 대신 pdo 또는 mysqli_ * 함수를 사용하십시오. – fortune

+5

이것을 버리고 [PDO] (http://php.net/pdo) 또는 [mysqli] (http://php.net/mysqli)를 사용하십시오. 그런 다음 다시 물어보십시오. –

+0

특정 데이터베이스에 연결하거나 테이블을 지정할 때 데이터베이스를 지정할 수 있습니다. 예 : 쿼리에서'FROM MyDatabase.MyTable' – Eilidh

답변

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

당신이 반환 mysql_select_db를 사용하여 데이터베이스를 선택해야이

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