简体   繁体   中英

How can I get the number of rows in a MySQL table with PHP?

Why is my code returning a 500 Internal Server error on the line $result = mysql_query("SELECT * FROM institutions"); Am I doing something horrifically wrong? All I am trying to do is count the number of rows in a MySQL table (called 'institutions') after I have just added a row to that table.

    $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

    }

$result = mysql_query("SELECT count(*) FROM institutions");

This will directly return the number of rows.

This link can detail you

http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html

your problem is that you mixing MYSQLI with MYSQL

rewrite your code using mysqli

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

you are connecting via mysqli and then you use mysql in your code.

Use

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

Or for plain old mysql

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

Try this:

$result = mysql_query("SELECT count(*) FROM institutions");

MySQL documentation: http://dev.mysql.com/doc/refman/5.0/en/select.html

Also this: http://www.w3schools.com/sql/sql_func_count.asp

SQL COUNT(*) Syntax

The COUNT(*) function returns the number of records in a table:

...also, that should be:

VALUES ('" . nz($_POST['TempInstitutionName']) . "', 74

Note the single quotes [unless the 'nz' function takes care of that].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM