I am creating a PHP application where they input their database information and my program will connect to the database and insert tables. I want to first ensure there are no current tables in the database and if there are I would like to throw them an error.
I have already tried "SELECT COUNT(*)" but when I try to run that it says:
"Object of class mysqli_result could not be converted to int".
$check = 'SELECT COUNT(*)';
$val = $connection->query($check);
if ($val > 0) {
header("Location: ../../db-setup.php?notclean");
}
else {
$sql = 'CREATE TABLE `test` (`name` VARCHAR(255) NOT NULL);';
$connection->query($sql) or die();
header('Location: ../../db-setup.php?success');
}
I tried this configuration and even if there is no tables in the database it still throws me my notclean created error.
So I tried...
$check = 'SELECT COUNT(*)';
$val = $connection->query($check);
if ($val > 1) {
header("Location: ../../db-setup.php?notclean");
}
else {
$sql = 'CREATE TABLE `test` (`name` VARCHAR(255) NOT NULL);';
$connection->query($sql) or die();
header('Location: ../../db-setup.php?success');
}
After trying this, if there is nothing in the database it will create the tables, as I wanted. But, if there is something in the database it throws a weird error:
"Notice: Object of class mysqli_result could not be converted to int".