count function in IF statement

[php]
$table = ‘contactTable’
$rowCounterString = ‘SELECT * FROM $table’;
$rowCounter = mysql_query($rowCounterString);
$numRows = mysql_num_rows($rowCounter);
echo “$numRows RowsBefore\n”;

This brings back the right amount of rows.
I've tried $numRows == 0 and the page fails to load when i push the submit button
if ( $numRows = 0 or $numRows = 1 ) { echo "$numRows RowsAfter\n";
This doesn't bring back the right amount of rows. Always brings back a 1.
} echo "$numRows End\n";
This doesn't even show up on the screen.
[/php]

Case:
1.) Find out how many rows are in the database table.
2.) If there are no rows or just 1 row in the database, I want to tell it to do something. Not just echo but that will do for now to keep it simple.

Problem:
1.) Before the count rows function enters the IF statement, it works. Once it enters the IF statement, it always brings back the number 1 as how many rows are in the database table.

[php]$table = ‘contactTable’
$rowCounterString = ‘SELECT * FROM $table’;
$rowCounter = mysql_query($rowCounterString);
$numRows = mysql_num_rows($rowCounter);
echo “$numRows RowsBefore\n”; // This brings back the right amount of rows.
// I’ve tried $numRows == 0 and the page fails to load when i push the submit button
if ( $numRows = 0 or $numRows = 1 )
{
echo “$numRows RowsAfter\n”; // This doesn’t bring back the right amount of rows. Always brings back a 1.
}
echo “$numRows End\n”; // This doesn’t even show up on the screen.
[/php]

If you want to know why I need to be able to put the row count in an IF statement, it’s because I’m writing a Primary Key Override script. The Auto Increment feature that MySql offers continues the numbers from 1 to whatever the maximum size is. You can reset the Auto Increment to 1 but if there are rows in the table, it will reset it to the last row in the table. Rows in tables are constantly being deleted and when you look at the Primary Key you may see gaps like if there are 10 rows in a table and number 5 gets deleted. MySql doesn’t automatically fill that slot. This php script will change all the primary keys to reorganize and fill the empty slots. Then it will add a row when it’s done. I’ve tested the MySql code on the table and it works. Translating it to PHP took a long time but I think I am finished. The only problem is that it wont Count Rows in the IF Statement and my logic is completely based on that ability.

[php]

<?php $user = "Enter_Username_Here"; $password = "Enter_Password_Here"; $database = "Enter_Database_Name_Here"; $host = "Enter_Host_Name_Here_Or_localhost"; $table = "Enter_Table_Name_Here"; $primaryKeyID = "Enter_Primary_Key_Field_Name_Here"; $allRows = "SELECT * FROM $table"; $name=$_POST['textboxName']; $email=$_POST['textboxEmail']; $phone=$_POST['textboxPhoneNumber']; $insert = "INSERT INTO $table VALUES('$primaryKey','$name','$email','$phone')"; // Connect to database mysql_connect($host,$user, $password); @mysql_select_db($database) or die( "Unable to select database"); $counter = 1; $rowID = mysql_query($primaryKey); $rowCounter = mysql_query($allRows); $numRows = mysql_num_rows($rowCounter); echo "$numRows BEFORE\n"; if ( $numRows = 0 or $numRows = 1 ) {mysql_query($insert) or die ("error updating database");} echo "$numRows AFTER\n"; else {$currentRow = 2; $lastRow = mysql_num_rows($allRows); While ($currentRow != $lastRow) {$counter = $counter + 1; $tempIncrement = 2; $primaryKey = ("SELECT * FROM $table WHERE primaryKeyID = $tempIncrement"); While ($rowID != $counter) {$rowID = mysql_query($primaryKey); $tempIncrement = $tempIncrement + 1; } mysql_query("UPDATE $table SET ID=$counter WHERE primaryKeyID = $rowID"); $currentRow = $currentRow + 1; } $reset = "ALTER TABLE $table AUTO_INCREMENT = 1"; mysql_query($reset); mysql_query($insert) or die ("error updating database"); } mysql_close(); ?>

[/php]

use the below code:
[php]

if ($numRows ==0 || $numRows == 1)
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service