In the php script, there is a variable named $partners_location
.
In the SQL there is a column named plocation
.
What I want to do is first check if there is any value in the column plocation
which matches the value of $partners_location
.
If it does match then I want to go ahead and create a new table, from php script itself.
For this type of situation I know what we need is If else, but I don't know how to use it here
This is what I have been trying, by the way I am using PDO.
$dsn= "mysql:dbname=mydatabase";
$name= "root";
$password="****";
$conn = new PDO ($dsn, $name, $password);
...
if($CON = $conn->prepare("SELECT FROM CIDB WHERE plocation='$partners_location'");
$CON->execute();)
{
$tbl=$conn->prepare("CREATE TABLE chat (id INT NOT NULL);
}
else
{
$tbl=$conn->prepare("CREATE TABLE xyz (id INT NOT NULL);
}
I don't think that these if else statements are going to work.
Can someone suggest me something that would work?
$CON->execute()
will return true
even if the query is successful but zero rows are returned.
Use COUNT()
to check if rows exist.
Like this:
$CON = $conn->prepare("SELECT COUNT(*) as cnt FROM CIDB WHERE plocation = '$partners_location'");
$CON->execute();
$row = $CON-> fetch();
if($row[0] != '0' )
$tbl=$conn->prepare("CREATE TABLE chat (id INT NOT NULL)");
else
$tbl=$conn->prepare("CREATE TABLE xyz (id INT NOT NULL)");
Try this:
$CON = $conn->prepare("SELECT COUNT(*) as cnt FROM CIDB WHERE plocation = '$partners_location'");
$CON->execute();
$row = $CON-> fetch();
if($row[0] != '0' )
{
$tbl=$conn->prepare("CREATE TABLE chat (id INT NOT NULL)");
}
else
{
$tbl=$conn->prepare("CREATE TABLE xyz (id INT NOT NULL)");
}
You missed ")