2013-01-06 3 views
1

스포츠, 영화 등의 관심 카테고리 인 사용자가 변수 $ ask를 사용하면 해당 카테고리가 존재하는지 데이터베이스를 검사하고,없는 경우 추가합니다 데이터베이스에. 데이터베이스 메모리, 테이블 - 'interestcategories'는 현재 IID, Category 및 Comment의 3 개의 열만 있습니다. 아무것도 넣지 도착으로php - 출력 할 쿼리의 결과를 가져올 수 없습니다. (출력이있는 경우)

while ($row = mysqli_fetch_assoc($result)) { 
    printf ("%s (%s)\n", $row["Category"], $row["Comment"]); 
    } 
    /* free result set */ 
    mysqli_free_result($result); 

: 거기 작동하지 않습니다에 무엇을 인쇄, 데이터베이스에있는 경우 데이터베이스에 추가

문제는 대부분이 라인에있다 .... 그러나 작동 오류 메시지조차도 화면에 표시하지 않습니다. 범주는 한 번만 테이블에 나타나므로 한 줄만 인쇄해야합니다. 아이디어가 있으십니까?

<?php 
    error_reporting(E_ALL); 
    $link = mysqli_connect("localhost", "root", "", "memory"); 
    /* check connection */ 
    if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
    } 

function notAnInterest($ask, $link) 
{ 
$query2 = "INSERT into interestcategories (Category, Comment) 
     VALUES ('$ask', 'Added by user') "; 

$result2 = mysqli_query($link, $query2); 
echo "<pre>Debug: $query2</pre>\n"; 
if (false===$result2) { 
    printf("error: %s\n", mysqli_error($link)); 
    } 
echo 'added ' . $ask; 
} 

if(isset($_POST['ask']) === true && empty($_POST['ask']) === false) { 
$ask = trim($_POST['ask']); 
$query = " 
SELECT * 
FROM `interestcategories` 
WHERE `interestcategories`.`Category` = '$ask' "; 

if ($result = mysqli_query($link, $query)) { 
if(($row = mysqli_fetch_assoc($result)) === null) { 
notAnInterest($ask, $link); 
} 
/* fetch associative array */ 
    printf('here1'); 
while ($row = mysqli_fetch_assoc($result)) { 
    printf('here2'); 
    //printf ("%s (%s)\n", $row["Category"], $row["Comment"]); 
} 

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

} 

/* close connection */ 
    mysqli_close($link); 

    ?> 
+0

시도로 오류보고 사용하도록 설정하는 "는 error_reporting (E_ALL를);" –

+0

어떤 라인을 처음에 추가합니까? 나는이 부분으로 문제를 좁혔다. * * 연관 배열 가져 오기 */ \t printf ('here1'); while ($ row = mysqli_fetch_assoc ($ result)) { printf ('here2'); } '여기 1'은 printd가 아니라 'here2'가됩니다. –

+0

error_reporting (E_ALL); 상단에 오류가있을 경우 표시됩니다 .. –

답변

0

변경 :

if ($result = mysqli_query($link, $query)) 
{ 
    if (($row = mysqli_fetch_assoc($result)) === null) 
    { 
     notAnInterest($ask, $link); 
    } 

    /* fetch associative array */ 
    while ($row = mysqli_fetch_assoc($result)) 
    { 
     printf ("%s (%s)\n", $row['Category'], $row['Comment']); 
    } 

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

사람 :

if ($result = mysqli_query($link, $query)) 
{ 
    if (0 == mysqli_num_rows($result)) 
    { 
     notAnInterest($ask, $link); 
    } 
    else 
    { 
     /* fetch associative array */ 
     while ($row = mysqli_fetch_assoc($result)) 
     { 
      printf ("%s (%s)\n", $row['Category'], $row['Comment']); 
     } 
    } 

    /* free result set */ 
    mysqli_free_result($result); 
} 
+0

notAnInterest ($ ask, $ link)는 $ row = mysqli_fetch_assoc ($ result) === null 인 경우에만 실행된다는 점을 잘 모르겠다. –

+0

@LindsayW, 위의 코드를 실행하고 출력을 말해 브라우저, 당신은 편지를 받아야한다. 실패한 곳을 우리에게 알려줍니다. –

+0

동물 (데이터베이스에없는 항목) : cdea 디버그 : INSERT INTO'interestcategories' ('Category','Comment') VALUES ('animals', '사용자가 추가 함') animalsg 데이터베이스에서 다음을 얻습니다 : cdg –

관련 문제