Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在数据库中插入preg_match_all获得的数组_Php_Mysql_Sql_Arrays_Preg Match All - Fatal编程技术网

Php 如何在数据库中插入preg_match_all获得的数组

Php 如何在数据库中插入preg_match_all获得的数组,php,mysql,sql,arrays,preg-match-all,Php,Mysql,Sql,Arrays,Preg Match All,我刚刚得到了一个使用preg_match_all的数组,并尝试使用以下代码将其插入到表中: $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="(.*?)" dat

我刚刚得到了一个使用preg_match_all的数组,并尝试使用以下代码将其插入到表中:

$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')";
$casturl=”https://www.themoviedb.org/movie/353491-the-dark-tower/cast";
$cast=file\u get\u contents($casturl);
预赛#

但我在数据库中什么都没有,我想可能是因为它不是一个常规数组,我不知道,我对php不是很有经验。

下面是一个如何在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();
?>

下面是一个如何在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();
?>

您需要php.ini中的扩展PDO(我不知道默认情况下它是取消注释的)尝试使用phpinfo();并查看它在php中已启用:PDO_mysqlYou need on extension PDO in php.ini(我不知道默认情况下它是取消注释的)尝试使用phpinfo();并查看它在php中的启用情况:pdo_mysqlyou不应该使用正则表达式来解析HTML使用DOMDocument作为HTML解析器。您不应该使用正则表达式来解析HTML使用DOMDocument作为HTML解析器。