Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
319 views
in Technique[技术] by (71.8m points)

mysql - Insert array content into database - php

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: " ;
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...