0

The current error when running this from the command line is "Call to a member function bindParam() on a non-object" which I've worked out to being a problem with the variable $orderPO. Something does not like non-numeric characters which led me to the bindParam PARAM_STR business which does not work either. The database fields are both varchar 50.

My search skills are failing me. I know this must be posted somewhere about a million times but I can't seem to find it. I am completely open to doing this another way if someone has a better idea.

Current attempt code:

try
{
    $orderNum = '123456';
    $orderPO = '123456-A';


    $dbh = new PDO("mysql:host=localhost;dbname=dbname", 'someuser', 'somepass');
    $stm = $dbh->prepare("insert into some_table (order_number, order_po)");
    $stm->bindParam(':order_number', $orderNum, PDO::PARAM_STR);
    $stm->bindParam(':order_po', $orderPO, PDO::PARAM_STR);
    $stm->execute();
    print_r($stm);
    print_r($dbh);
    $arr = $stm->errorInfo();
    print_r($arr);
    $stm->closeCursor();
    $dbh = null;
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
3
  • Are you sure that $stm has been initialised properly? It's always worth adding in some error checking to make sure that your variables have the values that you think they do. Commented Jun 21, 2012 at 16:21
  • var_dump $dbh and $stm variables , at $stm query , add : at the insert query before order_number and order_po Commented Jun 21, 2012 at 16:22
  • 1
    The problem with your code we cannot see - most likely it is a syntax error in your actual SQL statement. Although if the above is your actual query, the problem is probably that you forgot to prefix your placeholders with :. Either way, the call to $dbh->prepare() is failing and returning FALSE, you need to test for this. You can find out why it failed by inspecting PDO:: errorInfo() Commented Jun 21, 2012 at 16:26

3 Answers 3

2

In order to bind parameters using PDO, you will need to use placeholders, like this:

$stm = $dbh->prepare("
    INSERT INTO `some_table` SET
        `order_number` = :order_number,
        `order_po` = :order_po
");
$stm->bindParam(':order_number', $orderNum, PDO::PARAM_STR);
$stm->bindParam(':order_po', $orderPO, PDO::PARAM_STR);

Notice the inclusion of the : character before the named placeholder. I also added column names to your query.

Read further and see examples: PDO bindParam

Sign up to request clarification or add additional context in comments.

Comments

0

The correct syntax is

$stm = $dbh->prepare("insert into some_table (order_number, order_po) VALUES (?, ?)");
$stm->bindParam(1,$orderNum);
$stm->bindParam(2,$orderPO);

include the questions marks, the numbers in the bindParam call refer to which question mark you're binding the parameter to

Comments

0

You are trying to use bindparam, but bind param matches ? not cursors :. You have not included any parameters or values.

Also, you are missing your VALUES statement within the query, which is causing the query to fail. This is why you get the "Call to a member function bindParam() on a non-object"

To use the :value syntax, use bindValue, not bindParam. to use bindParam, switch the :value to ? in your query and number them in order is your execute array.

try
{

    $orderNum = '123456';
    $orderPO = '123456-A';


    $dbh = new PDO("mysql:host=localhost;dbname=dbname", 'someuser', 'somepass');
    $stm = $dbh->prepare("insert into some_table (order_number, order_po) VALUES (:order_number, :order_po)");
    $stm->bindvalue(':order_number', $orderNum, PDO::PARAM_STR);
    $stm->bindvalue(':order_po', $orderPO, PDO::PARAM_STR);
    $stm->execute();
    print_r($stm);
    print_r($dbh);
    $arr = $stm->errorInfo();
    print_r($arr);
    $stm->closeCursor();
    $dbh = null;
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.