2013-12-16 1 views
-1

메신저 PHP에 익숙하지 않아 데이터베이스를 코딩하려고합니다. 전체 코드는 잘 작동하지만이 부울 오류가 발생합니다. 나는 $ 결과가 부울을 기다리고 있다고 생각한다. 우스운 일은 비디오를 정확히 따라 가면서 오류가 발생했습니다. 바라건대 누군가가 그 오류가 무엇을 의미하는지 그리고 어떻게 해결할 수 있는지 설명해 줄 수 있기를 바랍니다.Mysql이 작동하지만 부울을 기대합니다

오류 : 성공!

경고 : \ XAMPP \ htdocs를 \ introducingphp \ DataBaseTut3.php 라인 66

경고 : mysqli_fetch_assoc()는 부울 C에 주어진 파라미터 1 mysqli_result되기를 기대 mysqli_free_result()가 파라미터 1 mysqli_result 될 것으로 예상 부울 C에 주어진 : \ XAMPP \ htdocs를 \ introducingphp \ DataBaseTut3.php 라인에 77

내 코드 :

<?php 
//1.Create database connection 
$dbhost = "localhost"; 
$dbuser = "weekeong"; 
$dbpass = "alexsage"; 
$dbname = "fyp"; 
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); 
//Test if connection occurred. 
if(mysqli_connect_errno()) { 
    die("Database connection failed: ". 
    mysqli_connect_error() . 
    " (" . mysqli_connect_errno(). ")" 
    ); 
} 
?> 

<?php 
//2. Perform database query 

// these are often form values in $_POST 
$id = 5; 
$name = "John xan"; 
$age = 30; 
$birthdate = "19/03/1994"; 
$medical_allergies = ""; 
$phone = 88888888; 
$address = "lame street"; 
$doctor_assigned = "Dr Lee"; 


$query = "INSERT INTO patients ("; 
$query .= " id, name, age, birthdate, medical_allergies, phone, address, doctor_assigned"; 
$query .= ") VALUES ("; 
$query .= " {$id}, '{$name}', {$age}, '{$birthdate}', '{$medical_allergies}', {$phone}, '{$address}', '{$doctor_assigned}'"; 
$query .= ")"; 



//query is not only to get something but to get sqli to do something for us so we still use query 
$result = mysqli_query($connection, $query); 
//Test for query error 
if ($result) { 
    echo "SUCCESS!"; 
    //success 
    //redirect_to ("somepage.php"); 
} else { 
    //Failure 
    //$message = "Creation failed"; 
    die("Database query failed." .mysqli_error($connection));  
} 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Database 3</title> 
</head> 

<body> 

<ul> 

<?php 
//3 Use returned data (if any) 
while($doc = mysqli_fetch_assoc($result)) { // only creates row if there are results 
// cannot use for each with fetch_row as it is not a PHP array, it is a result set from MY SQL. For Each will try to increment pointer at the end but MYSQL doesnt have array pointer so use while. 
    //output data from each row 
?> 
    <li><?php echo $doc["name"]; ?></li> 
    <?php 
} 
?> 
</ul> 
<?php 
//4. Release returned data 
mysqli_free_result($result); 
?> 
</body> 
</html> 
<?php 
//5. Close database connection 
mysqli_close($connection); 
?> 
+0

삽입 쿼리를 반복하려고합니까? 나는 당신이 필요 * SELECT * 거기에 –

답변

0

$queryINSERT 문입니다,하지만 당신은 입술을 통해 루프 시도 ult. INSERT 문 다음에 루프 할 행이 없습니다. 당신이 다른 것을 달리고 싶은 것처럼 들리 겠지만, SELECT을 먼저 분리하십시오.

the manual page regarding mysqli_query() (강조 추가)에서 :이 근무하고 false없는 경우 경우 경우

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

, $resulttrue입니다. 어느 쪽이든 반복 할 행이 없습니다.

+0

귀하의 조언을 주셔서 감사합니다, 나는 루프를 삭제하고 지금은 잘 작동합니다. 고맙습니다. 이 스레드를 닫거나 닫히는 방법이 있습니까? – user3098046

+0

도와 드리겠습니다! 스레드를 닫거나 삭제할 필요가 없습니다. 해결 방법을 표시하는 방법은 도움이된다면이 대답을 수락하는 것입니다. :) –