How to insert in a database an array got by preg_match_all

时光毁灭记忆、已成空白 提交于 2019-12-24 06:03:21

问题


I've just got an array using preg_match_all and i tried to insert it to a table using this code:

$casturl = "https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
$cast = file_get_contents($casturl);
preg_match_all('#<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="#' , $cast , $castimg );    
print_r($castimg[1]);

$escaped_values = array_map('mysql_real_escape_string', array_values($castimg));
$values  = implode(", ", $escaped_values);
$sql = "INSERT INTO test (content) VALUES ('$values')";

But i'm getting nothing in my db, i think it maybe because it is not a regular array, i don't know, i'm not very experienced with php.


回答1:


Here is an example how to add data to table in mysql:

<?php
session_start();
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);


// mysql settings change here !!!
$mdatabase = 'sun';
$muser = 'root';
$mpass = 'toor';
$mhost = 'localhost';
$mport = 3306;


$casturl = "https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
$cast = file_get_contents($casturl);
preg_match_all('#<img class="profile lazyload fade" data-sizes="auto" data-src="(.*?)" data-srcset="#' , $cast , $castimg );    
echo "<pre>";
print_r($castimg[1]);

try{
    // Connect to database
    $db = Conn();

    // Add all links
    foreach ($castimg[1] as $key => $val) {
        // save to db
        saveLink($val);
    }
}catch(Exception $e){
    print_r($e);
}

// Add link function
function saveLink($link){
    global $db;
    if (!empty($link)) {
        $link = htmlentities($link,ENT_QUOTES,'UTF-8');
        $r = $db->query("INSERT INTO test(content) VALUES('$link')");
        // last inserted id if you had auto increment primary key id (int or bigint)
        $id = $db->lastInsertId();
        return $id;
    }else{
        return 0;
    }
}

// connect to mysql with PDO function
function Conn(){
    global $mhost,$mport,$muser,$mpass,$mdatabase;
    $connection = new PDO('mysql:host='.$mhost.';port='.$mport.';dbname='.$mdatabase.';charset=utf8', $muser, $mpass);
    // don't cache query
    $connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    // show warning text
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    // throw error exception
    $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // don't colose connecion on script end
    $connection->setAttribute(PDO::ATTR_PERSISTENT, false);
    // set utf for connection utf8_general_ci or utf8_unicode_ci 
    $connection->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8' COLLATE 'utf8_general_ci'");
    return $connection;
}

// end script
die();
?>

See php info extension:

<?php
    phpinfo();
?>


来源:https://stackoverflow.com/questions/46378769/how-to-insert-in-a-database-an-array-got-by-preg-match-all

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!