The second query is not executing in php mysql

I’m newbie in PHP and WordPress. This approach was working fine for me in ASP.NET but here both queries are not working. When I comment the first one, the second one(Insertion) is working fine.

$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();
}

Check wether the email exists or not ?

Read More
$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

Second Stored Procedure

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;

Related posts

Leave a Reply

2 comments

  1. Here is what I was talking about when I said create a query class to fetch data. This is just a simple one, but it works pretty effectively and you can build it out to be pretty powerful.

    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);
    
  2. It is quite simple. Your code is fine but you only have to create two separete functions and simply call those functions instead of the code directly.