-1

I'm trying to insert in each row of database the variable $num which is an Array. I want to insert each array position in each row of database.

That's the code I have, and in the moment it is inserting the word "Array":

foreach($html2->find('small[class=menu-item__label__count]') as $aholder) {

$holderdes=$aholder->outertext;
}
preg_match_all('!\d+!', $holderdes, $num);
//preg_match_all('/(\([0-9]+)\)/',  $holderdes,$num);

echo print_r($num); 
echo("<script>console.log('PHP: ".$num."');</script>");
$insert="INSERT into spec_insert(designers) VALUES ('".$num."');";

$sql=mysql_query($insert);

if ($sql === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " ;
}
}
3
  • 1
    most likely $num is an array, and the result (number) you want is inside of it, you already have print_r($num), just check whats inside and point to it directly. and stop using mysql_*. learn to use mysqli_* or PDO with prepared statements, now Commented Nov 4, 2014 at 1:49
  • Inside of print_r($num) it's several: Array ( [0] => Array ( [0] => 34 ) ) Commented Nov 4, 2014 at 1:52
  • then just point to it directly, echo $num[0][0], its just nested. Commented Nov 4, 2014 at 1:53

1 Answer 1

1

If you want to insert each array value to a specific column of DB table spec_insert then can use foreach() to retrieve the index value of array. Example

foreach($num as $val):
    if(is_array($val)):
        foreach($val as $v):
            $insert="INSERT into spec_insert(designers) VALUES ('".$v."');";
            $sql=mysql_query($insert);
        endforeach;
    else:
        $insert="INSERT into spec_insert(designers) VALUES ('".$val."');";
        $sql=mysql_query($insert);
    endif;
endforeach;
Sign up to request clarification or add additional context in comments.

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.