2013-02-08 4 views
0

나는 간단한 PHP 코드를 작성하여 mySql 테이블의 데이터를 표시했습니다. "mysql_connect", "mysql_select_db"를 사용할 때이 기능이 작동하지만 "mysqli_connect", "mysqli_select_db"를 사용할 때는 작동하지 않습니다. 누구든지 그것에 빛을 비추어주세요.mysqli가 작동하지 않습니다

저는 아주 중학교 웹 개발자입니다.

이 내가 쓴 코드 :

<?php 
$con = mysqli_connect("localhost","root","abc123"); 
if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
} 

$db=mysqli_select_db("database_name",$con) or die("Could not select DB"); 

if (!$db) 
{ 
die('no database connected'); 
} 

$data = mysql_query("SELECT * FROM categories") 
or die(mysql_error()); 
Print "<table border cellpadding=3>"; 
while($info = mysql_fetch_array($data)) 
{ 
Print "<tr>"; 
Print "<th>Name:</th> <td>".$info['parentId'] . "</td> "; 
Print "<th>ID:</th> <td>".$info['title'] . "</td> </tr>"; 
} 
Print "</table>"; 
?> 

는하지만 다음 "MySQL은"그 작업과 함께 "mysqli"를 대체합니다.

+3

이 문제는 .... 당신이 사용중인 라이브러리에 대한 일관성입니다 ... 당신은 아직하지 않습니다, 나는이 코드를 복사하여 실행 mysqli 쿼리 방법 –

답변

2

mysql_ 기능을 mysqli_과 혼합합니다. 나는 documentation을 보길 권합니다. 는 mysql_query와 mysqli_connect 및 mysqli_select_db 혼합

<?php 
$link = mysqli_connect("localhost", "root", "abc123", "database_name"); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 

$query = "SELECT * FROM categories"; 

if ($result = mysqli_query($link, $query)) { 

echo "<table border cellpadding=3>"; 

    /* fetch associative array */ 
    while ($info = mysqli_fetch_assoc($result)) { 
    echo "<tr>"; 
    echo "<th>Name:</th> <td>".$info['parentId'] . "</td> "; 
    echo "<th>ID:</th> <td>".$info['title'] . "</td> </tr>"; 
    } 

    /* free result set */ 
    mysqli_free_result($result); 

echo "</table>"; 
} 

/* close connection */ 
mysqli_close($link); 
?> 
+0

를 사용할 필요가 작업. 모든 PHP config/ini 파일에서 아무것도 활성화해야합니까? 나는 그저 중학교 학생이며 전문가는 아니다. PHP 또는 MySQL 설치의 설정에서 잘못된 점이 있다고 생각합니다. Pleese가 알려 줬어. 미리 감사드립니다. – Isaac

+0

@ user2055743 ** ** 무엇이 작동하지 않습니까? 당신은'error_reporting'을 활성화 시켰습니까? – Kermit

관련 문제