第二个查询没有在php mysql中执行

时间:2014-10-15 05:01:48

标签: php mysql wordpress

我是PHPWordPress的新手。这种方法在ASP.NET中对我来说很好,但是这两个查询都不起作用。当我评论第一个时,第二个(插入)工作正常。

$dbhostname="111.1.11.111"; 
$dbusername="db_userName";
$dbpassword="mypassword";

$con=mysqli_connect($dbhostname,$dbusername,$dbpassword,"db_name");

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

检查电子邮件是否存在?

$sql="CALL Select_ConfirmEmailExistance('abc@abc.com')";

  $containsResult=0;
 if ($result=mysqli_query($con,$sql))
 {
 // Get field information for all fields
 while ($fieldinfo=mysqli_fetch_assoc($result))
 {
    if (isset($fieldinfo)) {
        $containsResult=1;// Email Exists 
    }
}
mysqli_free_result($result);

if ($containsResult==0) { // In case email does not exists enter it.

    $sql="CALL insert_Userinfo('abc','def','abc@abc.com','mnop')";
    if ($result=mysqli_query($con,$sql))
    {
        $data;
        while ($fieldinfo=mysqli_fetch_assoc($result))
        {
            $data[]=$fieldinfo;
        }
    }
  }
  print_r($data);
  }

mysqli_close($con);

First Store Procdure

BEGIN
    SELECT 1 as emailstatus FROM userinfo WHERE email= p_email;
END

第二个存储过程

INSERT INTO `userinfo` (
    `first_name`,
    `last_name`,
    `email`,
    `password`
)
VALUES
    (
        `FName`,
        `LName`,
        `Email`,
        `Pass`
    );

SELECT
    user_id
FROM
    userinfo
ORDER BY
    user_id DESC
LIMIT 1;

2 个答案:

答案 0 :(得分:2)

当我说创建一个查询类来获取数据时,我正在讨论这个问题。这只是一个简单的,但它非常有效,你可以把它构建得非常强大。

class   DBEngine
    {
        public  $con;
        public  function __construct($host="111.1.11.111",$db = "dbname",$user="db_userName",$pass="mypassword")
            {
                try {
                        $this->con  =   new PDO("mysql:host=$host;dbname=$db",$user,$pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
                    }
                catch (Exception $e) {
                      return 0;
                    }
            }

        // Simple fetch and return method
        public  function Fetch($_sql)
            {
                $query  =   $this->con->prepare($_sql);
                $query->execute();

                if($query->rowCount() > 0) {
                        while($rows = $query->fetch(PDO::FETCH_ASSOC)) {
                                $array[]    =   $rows;
                            }
                    }

                return (isset($array) && $array !== 0 && !empty($array))? $array: 0;
            }

        // Simple write to db method
        public  function Write($_sql)
            {
                $query  =   $this->con->prepare($_sql);
                $query->execute();
            }
    }

// Create an instance of the engine
$query          =   new DBEngine();
// Query 1 will return an array or false (0)
$call1          =   $query->Fetch("CALL Select_ConfirmEmailExistance('abc@abc.com')");
// Assign your true/false
$containsResult =   ($call1 !== 0)? 1:0;
// Run second query and return array or false (0)
if($containsResult == 0)
    $data   =   $query->Fetch("CALL insert_Userinfo('abc','def','abc@abc.com','mnop')");
// Display returned result
print_r($data);

答案 1 :(得分:1)

这很简单。你的代码很好,但你只需创建两个separete函数,只需直接调用这些函数而不是代码。