0

Theres's a similar question here, but actually that doesn't give me the answer:
PHP + PDO: Bind null if param is empty

I need my statement work in a loop, with only changing the binded variables.
Like:

$this->array = array(
    "cell1" => "",
    "cell2" => "",
);

$this->sth = $db->prepare("INSERT INTO `table`
    (`coloumn1`, `coloumn2`)
    VALUES (:coloumn1, :coloumn2)");
$this->sth->bindParam(:coloumn1, $this->array['cell1'], PDO::PARAM_STR);
$this->sth->bindParam(:coloumn2, $this->array['cell2'], PDO::PARAM_STR);

//Data proccessing...

foreach($data as $value){
    $this->array['cell1'] = $value['cell1'];
    $this->array['cell2'] = $value['cell2'];
    try {
        this->sth->execute();
        print_r($this->sth->errorInfo());
    }
    catch(PDOException $e){
        echo 'sh*t!';
    }
}

Everything works well until either of the values is an empty string. My problem is when 'cell1' is an empty string, the bound parameter is a nullreference, and it won't work. But I need the referenced binding because of the loop, so bindValue isn't a solution.

And I need the loop very bad, because of the huge data I want to process.

Any suggestion?

I tried right before execute:

foreach($this->array as $value){
   if(!$value) {
        $value = "";
    }
}

It doesn't work. The only way that solved my problem is modifying to this:

    $this->array['cell1'] = !empty($value['cell1']) ? $value['cell1'] : "";
    $this->array['cell2'] = !empty($value['cell2']) ? $value['cell2'] : "";

But this seems too rubbishy...

3
  • What's the use for $this->array array variable? Commented Jul 4, 2013 at 8:08
  • It's for preparing the statements. I'll have the structure, like this: $data = array( array( "value1", "value2", ), array( "value1", "value2", ) ); I'll turn it into the array which is bound to the statement from element to element and then execute. Commented Jul 4, 2013 at 9:24
  • if yyou have your array already - why another array and even a class variable? Commented Jul 4, 2013 at 9:55

2 Answers 2

0

I know its necroposting, but maybe will help to someone. You trying to check if the variable false, but not null. And not reapplying values back to array.

foreach($this->array as $value){
   if(!$value) {
        $value = "";
    }
}

Try to check for null in loop

foreach($this->array as $index => $value)
{
   $this->array[$index] = !empty($value) ? $value : '';
}
Sign up to request clarification or add additional context in comments.

Comments

-1

Your question has nothing to do with PDO but with basic PHP. When there is no variable available - you can't use it at all. So, you have to create it somehow. The way you are using at the moment is not "rubbishy" but quite acceptable. I'd rather call whole code "rubbishy" as it's twice as big as as it should be.

But I need the referenced binding because of the loop, so bindValue isn't a solution.

This assumption is wrong too. Why do you think you can't use bind by value?

$sql = "INSERT INTO `table` (`coloumn1`, `coloumn2`) VALUES (?, ?)";
$sth = $db->prepare($sql);
foreach($data as $value)
{
    $value['cell1'] = !empty($value['cell1']) ? $value['cell1'] : "";
    $value['cell2'] = !empty($value['cell2']) ? $value['cell2'] : "";
    $sth->execute($value);
}

as simple as this

And I need the loop very bad, because of the huge data I want to process.

I don't think it's really huge, as it fits for the PHP process memory. However, consider to use LOAD DATA INFILE query for the real huge amounts.

9 Comments

Thanks for your time! Actually the data is huge as i sequentially pull it from the database. The code is big, because readability, of course i reduced it. So my idea was preparing the statements then processing the data to arrays then loop through the array with the inserts. Can I somehow declare those arrays for preparing the statements? If I bind as a value, I can't execute with the changed variable value again. That's why I said bindValue is not a solution.
Who said you can't? Did you try? All you need is to move bindvalue inside of a loop. Or you can just pass it in execute as in my example.
The concept was to make the fastest solution. Actually I don't know what actually bindValue does step by step, but my logic tells that giving value to a variable is less needy than this binding process. That's why, I didn't want to place into the loop.
I see. Speed optimization fever. No further questions.
I see your sarcasm, but you didn't see my code running for half an hour. Anyway, thanks for your time. You helped me a lot.
|

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.