[英]Control Flow issue in SQL
In the php script, there is a variable named $partners_location
. 在php脚本中,有一个名为$partners_location
的变量。
In the SQL there is a column named plocation
. 在SQL中,有一列名为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
. 我想要做的是首先检查列plocation
是否有任何值与$partners_location
的值匹配。
If it does match then I want to go ahead and create a new table, from php script itself. 如果它确实匹配,那么我想从php脚本本身继续创建一个新表。
For this type of situation I know what we need is If else , but I don't know how to use it here 对于这种情况,我知道我们需要的是If if ,但我不知道如何在这里使用它
This is what I have been trying, by the way I am using PDO. 这就是我一直在尝试的方式,我正在使用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. 我认为这些if else语句不会起作用。
Can someone suggest me something that would work? 有人可以建议我一些可行的方法吗?
$CON->execute()
will return true
even if the query is successful but zero rows are returned. 即使查询成功,但返回零行, $CON->execute()
也会返回true
。
Use COUNT()
to check if rows exist. 使用COUNT()
检查行是否存在。
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 ")
您错过了")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.