Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8227241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:03:42+00:00 2026-06-07T16:03:42+00:00

I managed to run the following code to insert into my table on first

  • 0

I managed to run the following code to insert into my table on first try. Then, I deleted that row in PHPMyAdmin to test my code further. I also noticed that it didn’t get deleted on the 1st try. Only after few try. This might be due to I didn’t set the $pdoHandle to NULL after I’m done with the query.

Then, unfortunately I couldn’t insert new row on subsequent run. I even tried to change the input value and to avail I was unable to insert new row. The following are my PHP codes:

public function CreateNewCustomer($userId,$password,$name,$email)
{
    $userId = filter_var($userId,FILTER_SANITIZE_STRING);
    $password = filter_var($password,FILTER_SANITIZE_STRING);
    $password = sha1($password);
    $name = filter_var($name,FILTER_SANITIZE_STRING);
    $email = filter_var($email,FILTER_SANITIZE_EMAIL);

    do{
        $customerId = hexdec(bin2hex(openssl_random_pseudo_bytes(4,$isStrong)));
        echo $customerId;
        $result = $this->connObject->exec("SELECT COUNT(id) FROM customer_tbl WHERE id=$customerId");
        var_dump($result);
    }while($result>0);

    $statement = $this->connObject->prepare("INSERT INTO customer_tbl (id,name,email) VALUES ($customerId,:name,:email)");
    $result = $statement->execute(array(':name'=>$name,':email'=>$email ));
    var_dump($result);

    $statement = $this->connObject->prepare("INSERT INTO login_tbl (username,password,customer_id) VALUES (:userName,PASSWORD(:password),$customerId)");
    $result = $statement->execute(array(':userName'=>$userId,':password'=>$password ));
    var_dump($result);
}

I used the following code to access the above method.

function Test($userName,$password,$name,$email)
{
try
{
    $dbConnect = new DbConnect();
    $pdoHandle = $dbConnect->Connect();
    $userAccess = new UserAccess($pdoHandle);
    $userAccess->CreateNewCustomer($userName,$password,$name,$email);
}
catch(PDOException $e)
{
    $pdoHandle = null;
    var_dump($e);
}
$pdoHandle = null;
}

Test('tester','password','TestX','test@example.com');

The var_dump of results is always false.

Is there any problem with my codes or is it something wrong with the database?

UPDATE/SOLUTION:

I just read through the PHP document on PDO::exec() and one of the user contributed notes mentioned that you can’t use any SELECT statements (even thou the above only returns the count value) and any statements which might return a rows. The return value of PDO::exec() is the number of affected rows (integer), so the PDOStatement::closeCursor() can’t be used to solve it. Even when I set the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY=>true, it still doesn’t work.

So, don’t use PDO::exec() for any SELECT. I changed my code to PDO::query() instead as below,

   do{
        $customerId = hexdec(bin2hex(openssl_random_pseudo_bytes(4,$isStrong)));
        $statement = $this->connObject->query("SELECT COUNT(id) FROM customer_tbl WHERE id=$customerId");
        $statement->execute();
    }while($statement->fetchColumn(0)>0);

Hope this would be helpful to anyone looking for a solution with similar problem and always remember to read the PHP document first including the user contributions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:03:43+00:00Added an answer on June 7, 2026 at 4:03 pm

    Maybe not the answer but here are some things that you can do if you cannto see an obvious error:

    If execute returns false, you can get more information about the error that happened by:

    $arr = $statement->errorInfo(); 
    print_r($arr);
    

    or you can set different error reporting modes (e.g. throw an exception instead of the defaultsilent mode):

    $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
    $user = 'dbuser';
    $password = 'dbpass';
    $dbh = new PDO($dsn, $user, $password);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    This should help you to find the “real” error.

    As it turned out (see comments below question), in this case the real error was:

    “Cannot execute queries while other unbuffered queries are active.
    Consider using PDOStatement::fetchAll(). Alternatively, if your code
    is only ever going to run against mysql, you may enable query
    buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY”

    In this case you have 2 options:

    you can set the option to use buffered queries

    $dbh = new PDO(’mysql:host=localhost;dbname=test’, ‘root’, ” ,array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true)) 
    

    or change your code and close an open cursor (may depend on the db driver you are using). You always should read the documentation which covers a lot of default problems.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that I've managed to narrow down to the following smallest-code
If I run the following command: >python manage.py test Django looks at tests.py in
Im using outproc session that is managed by aspnet_state. Sometimes I get run time
So, my JFrame becomes unresponsive when I run this code. I managed to trace
I've managed to condense my confusion to the following code: template<typename T> class BTI
I've managed to get into a linux machine to try the HotKey library suggested
I have a fairly old unmanaged C++ program that called managed c# code (vs2003)
The included C# unit test and C code file attempts to pass a managed
I'm getting the following error when I run ./manage.py reset app1 : Error: Error:
I managed to run cvs2git several months ago and created a git repository for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.