2012-06-22 40 views
1

調用的函數,如: -無法讀取行使用PHP使用PHP函數插入MySQL數據庫,調用該函數時剛過

$setmessage = setlatestmessage($sessionid, $sendersuserid, $message1); 

功能是文件的functions.php,我已經列入在這個文件中 功能是: -

function setlatestmessage($sessionid, $userid, $message, $status = 0) { 
    $dbc = mysqli_connect(hostname, username, password, database) 
      or die("Error connecting database"); 

    // there was no row, so just add a row 
    $query2 = "insert into table(session_id, user_id, message, status) values ($sessionid, $userid, '$message', $status)"; 
    mysqli_query($dbc, $query2); 

    // if a row was affected, close connection and send true 
    if (mysqli_affected_rows($dbc) == 1) { 
     mysqli_close($dbc); 
     return TRUE; 
    } 
    mysqli_close($dbc); 
    return FALSE; 
} 

現在,我不得不調用此函數剛過,劇本是這樣

if (!$setmessage) { 
       echo '<status>0</status>'; 
      } else { 
       // fetch id of the row just inserted 
       $query2 = "select id from game_messages where session_id = $sessionid and message = '$message1' and user_id = $sendersuserid order by datetime desc limit 1"; 
       $result2 = mysqli_query($dbc, $query2); 

       if (mysqli_num_rows($result2) == 0) { 
        // if no row was returned 
        // case never should happen 
        echo '<status>10</status>'; 
       } 
       else{echo'done';} 

輸出我得到的是10

但是,如果我只是把睡眠(50)函數調用下,還是我只是代碼什麼功能是做,而不是函數調用,我得到正確的輸出。不知道爲什麼會發生這種情況。

回答

0

如果您使用mysqli_insert_id()mysqli_insert_id()返回使用setlatestmessage()函數中最後一個插入查詢生成的ID,或者返回錯誤,則會更加容易和簡單。那麼你只需檢查:

$messageID = setlatestmessage($sessionid, $sendersuserid, $message1); 

if ($messageID !== false) 
{ 
    echo '<status>0</status>'; 
} 
else 
{ 
    echo '<status>10</status>'; 
} 
+0

Thanx man !!它像一個魅力。但它會更有幫助,如果你可以告訴,爲什麼我的代碼不工作... –

+0

我不知道,還沒有測試它 – slash197

+0

鋁右.. thanx –

相關問題