2013-10-11 86 views
0

爲什麼我的代碼在行$result = mysql_query("SELECT * FROM institutions");上返回500內部服務器錯誤我做了一件可怕的錯誤?我所要做的就是計算一個MySQL表中的行數(稱爲'院校'),然後我剛剛向該表中添加了一行。我怎樣才能獲得MySQL表中的行數與PHP?

$institution_sql = " 
    INSERT INTO `institutions` 
    (`InstitutionName`, `HeaderPictureID`, `Description`, `DevicesInfo`, `DoingInfo`, `FacebookPage`, `Location`, `TwitterHandle`, `Website`, `CreatedAt`) 
    VALUES 
    (" . nz($_POST['TempInstitutionName']) . ", 74, 'N/A', 'N/A', 'N/A', 'N/A', 'On the Internet', 'N/A', 'N/A', NOW()) 
    "; 

    $mysqli->query($institution_sql); 
    if ($mysqli->errno) { 
     $dbreturn['status'] = "PASSWORD_FAILURE"; 
    } else { 
     $dbreturn['status'] = "EXEC_SUCCESS"; 

     $result = mysql_query("SELECT * FROM institutions"); 
     $rows = mysql_num_rows($result); 
     echo "There are " . $rows . " rows in my table."; 

     $insert_sql = " 
     INSERT INTO `users` 
     (`Handle`, `Email`, `FirstName`, `LastName`, `InstitutionID`, `TempInstitutionName`, `TwitterHandle`, `ProfilePictureID`, `HeaderPictureID`, `AccountType`, `CreatedAt`) 
     VALUES 
     (" . nz($_POST['Handle']) . ", " . nz($_POST['Email']) . ", " . nz($_POST['FirstName']) . ", " . nz($_POST['LastName']) . ", $num_rows, " . nz($_POST['TempInstitutionName']) . ", " . nz($_POST['TwitterHandle']) . ", " . nz('75') . ", " . nz('74') . ", " . nz($_POST['AccountType']) . ",NOW()) 
     "; 

     $mysqli->query($insert_sql); 
     if ($mysqli->errno) { 
      $dbreturn['status'] = "EXEC_FAILURE"; 
     } else { 
     $dbreturn['status'] = "EXEC_SUCCESS"; 

     $insertid = $mysqli->insert_id; 

     $password_sql = " 
     INSERT INTO `passwords` 
     (`UserID`) 
     VALUES 
     ('$insertid') 
     "; 

     $mysqli->query($password_sql); 
     if ($mysqli->errno) { 
      $dbreturn['status'] = "PASSWORD_FAILURE"; 
     } else { 
      $dbreturn['status'] = "EXEC_SUCCESS"; 
     } 

     } //todo: use a transaction here 

    } 
+2

嘗試,如果你想要的行數使用'COUNT(*)',而不是*,你真的不想要讓你的整個表中的所有數據不知不覺 – InSane

+1

甚至'count(1)',儘管我認爲MySQL可能相當聰明,這些天 –

+1

'SELECT COUNT(*)FROM institutions;' – Elen

回答

1

您的問題是,你混合MYSQLIMYSQL

使用重寫代碼的mysqli

$result = $mysqli->query("SELECT * FROM institutions"); 
    $rows = $result->num_rows ; 
    // and so on ... 

您通過mysqli的,然後連接你在代碼中使用mysql。

+1

真的downvote?不是他混合mysqli和mysql?downvoter可以解釋爲什麼? –

+0

+1 echo_Me。OP混合mysqli和mysql。即使它不是原因,它仍然是一個問題。 – euantorano

1

使用

$result = $mysqli->query($institution_sql); 
$result->num_rows; 

或者對於普通舊版本的MySQL

$result = mysql_query($institution_sql); 
mysql_num_rows($result); 
0

...還,這應該是:

VALUES ('」。 nz($ _ POST ['TempInstitutionName'])。 「74」,74

注單引號[除非‘NZ’功能需要照顧那個。