将列表中的数据插入mysql数据库

pn9klfpd  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(475)

我使用一个自定义的网络爬虫从一个网站(有权限)拉信息,并存储到单独的列表中的所有信息。然后我想将这些信息存储在mysql中创建的表中。我已经成功地连接到数据库,我知道表名和插入的位置是正确的。
每当我看到从php插入数据的例子时,它只显示一个或两个插入,但是我有5个信息列表,每个列表最多有50个条目。如何在列表中插入数据存储?这是我用php编写的第一个程序,所以请记住我并不熟悉它的所有细节。

<?php
// connect to mysql
include('connect_mysql.php');
// make sure the webservice returns plain text
header( 'Content-Type: text/plain; charset=utf-8' );
// include 3rd party parsing utility
require './vendor/querypath/qp.php';
$gidURL = $_SERVER['REQUEST_URI'];

// figure out which guild to scrape info for
// MAKE SURE TO CHANGE TO EXACT URLS ONCE PAGES ARE COMPLETE
if(strpos($gidURL, 'guild_members') !== false) {
    $url = 'https://swgoh.gg/g/21284/gid-1-800-druidia/';
}else if(strpos($gidURL, 'liquid-schwartz') !== false) {
    $url = 'https://swgoh.gg/g/19528/gid-liquid-schwartz/';
}else if(strpos($gidURL, 'spaceballsdeep') !== false) {
    $url = 'https://swgoh.gg/g/14280/gid-spaceballsdeep/';
}else if(strpos($gidURL, 'chapter-eleven') !== false) {
    $url = 'https://swgoh.gg/g/41958/gid-chapter-eleven/';
}else if(strpos($gidURL, 'my-own-best-friend') !== false) {
    $url = 'https://swgoh.gg/g/4736/gid-my-own-best-friend/';
}else if(strpos($gidURL, 'gone-to-plaid') !== false) {
    $url = 'https://swgoh.gg/g/363/gid-gone-to-plaid/';
}else if(strpos($gidURL, 'my-schwartz-is-bigger') !== false) {
    $url = 'https://swgoh.gg/g/27827/gid-my-schwartz-is-bigger/';
}else if(strpos($gidURL, 'ludicrousspeed') !== false) {
    $url = 'https://swgoh.gg/g/19538/gid-ludicrousspeed/';
}else if(strpos($gidURL, 'lone-starr') !== false) {
    $url = 'https://swgoh.gg/g/32650/gid-lone-starr/';
}else {
    echo "No guild was found in the URL.\n";
}

// get the HTML page into the parser
$guild_page = htmlqp( $url );
// $guild_page = htmlqp( './test-druidia.html' ); // for testing if you don't want to hit swgoh.gg
function guild_info_data() {
    // init empty array that will hold our data later
    $guild_members = [];
    global $guild_page;
    // loop through table rows
    foreach ( $guild_page -> find( '.character-list tbody tr' ) as $row ) {
        // init empty array for individual player
        $guild_member = [];
        $member_name = [];
        $member_gp = [];
        $member_cs = [];
        $member_rank = [];
        $member_rank_avg = [];

        // store data in temporary array
        $guild_member[ 'name' ] = preg_replace( "/\r|\n/", "", trim( $row -> branch( 'td' ) -> eq( 0 ) -> text() ) );
        $guild_member[ 'gp' ] = $row -> branch( 'td' ) -> eq( 1 ) -> text();
        $guild_member[ 'collection_score' ] = $row -> branch( 'td' ) -> eq( 2 ) -> text();
        $guild_member[ 'arena_rank' ] = $row -> branch('td') -> eq(3) -> text();
        $guild_member[ 'arena_average' ] = $row -> branch( 'td' ) -> eq( 4 ) -> text();

        // add player array to full response array
        $guild_members[] = $guild_member;
    }

    foreach ($guild_members as $guild_member) {
        $member_name[] = $guild_member['name'];
    }
    foreach ($guild_members as $guild_member) {
        $member_gp[] = $guild_member['gp'];
    }
    foreach ($guild_members as $guild_member) {
        $member_cs[] = $guild_member['collection_score'];
    }
    foreach ($guild_members as $guild_member) {
        $member_rank[] = $guild_member['arena_rank'];
    }
    foreach ($guild_members as $guild_member) {
        $member_rank_avg[] = $guild_member['arena_average'];
    }

}

// check what page is loaded 
if(strpos($gidURL, 'guild_members') !== false) {
    $name_insert = "INSERT INTO gid-1-800-druidia (name, galactic_power, collection_score, arena_rank, arena_average) VALUES ('$member_name', '$member_gp', '$member_cs', '$member_rank', '$member_rank_avg')";

    if(!mysqli_query($dbcon, $name_insert)) {
        die('error inserting new record');
    }else {
        echo 'successfully added info to DB';
    }

}else if(strpos($gidURL, 'liquid-schwartz') !== false) {
    // insert LSD data
}else if(strpos($gidURL, 'spaceballsdeep') !== false) {
    // insert SBD data
}else if(strpos($gidURL, 'chapter-eleven') !== false) {
    // insert C11 data
}else if(strpos($gidURL, 'my-own-best-friend') !== false) {
    // insert MOBF data
}else if(strpos($gidURL, 'gone-to-plaid') !== false) {
    // insert GtP data
}else if(strpos($gidURL, 'my-schwartz-is-bigger') !== false) {
    // insert SCH data
}else if(strpos($gidURL, 'ludicrousspeed') !== false) {
    // insert LS data
}else if(strpos($gidURL, 'lone-starr') !== false) {
    // insert LST data
}else {
    echo "No guild info was pulled.";
}

// let's get out of here
exit;

?>

我还被告知,mysqli是更好的使用,所以如果有人可以继续解释什么是做不同的,它也会非常感激。

<?php

// Connect to DB
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PSWD', 'root');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'guild_info');

$dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);

?>
okxuctiv

okxuctiv1#

如果在connect\u mysql.php中添加了mysql connection,那么如果不将mysql connection变量作为参数传递,就无法访问函数中的该mysql connection变量。
所以你需要像下面这样修改你的代码,

<?php
// connect to mysql
include('connect_mysql.php');
// make sure the webservice returns plain text
header( 'Content-Type: text/plain; charset=utf-8' );
// include 3rd party parsing utility
require './vendor/querypath/qp.php';
$gidURL = $_SERVER['REQUEST_URI'];

// figure out which guild to scrape info for
// MAKE SURE TO CHANGE TO EXACT URLS ONCE PAGES ARE COMPLETE
if(strpos($gidURL, 'guild_members') !== false) {
    $url = 'https://swgoh.gg/g/21284/gid-1-800-druidia/';
}else if(strpos($gidURL, 'liquid-schwartz') !== false) {
    $url = 'https://swgoh.gg/g/19528/gid-liquid-schwartz/';
}else if(strpos($gidURL, 'spaceballsdeep') !== false) {
    $url = 'https://swgoh.gg/g/14280/gid-spaceballsdeep/';
}else if(strpos($gidURL, 'chapter-eleven') !== false) {
    $url = 'https://swgoh.gg/g/41958/gid-chapter-eleven/';
}else if(strpos($gidURL, 'my-own-best-friend') !== false) {
    $url = 'https://swgoh.gg/g/4736/gid-my-own-best-friend/';
}else if(strpos($gidURL, 'gone-to-plaid') !== false) {
    $url = 'https://swgoh.gg/g/363/gid-gone-to-plaid/';
}else if(strpos($gidURL, 'my-schwartz-is-bigger') !== false) {
    $url = 'https://swgoh.gg/g/27827/gid-my-schwartz-is-bigger/';
}else if(strpos($gidURL, 'ludicrousspeed') !== false) {
    $url = 'https://swgoh.gg/g/19538/gid-ludicrousspeed/';
}else if(strpos($gidURL, 'lone-starr') !== false) {
    $url = 'https://swgoh.gg/g/32650/gid-lone-starr/';
}else {
    echo "No guild was found in the URL.\n";
}

// get the HTML page into the parser
$guild_page = htmlqp( $url );
// $guild_page = htmlqp( './test-druidia.html' ); // for testing if you don't want to hit swgoh.gg
function guild_info_data($dbcon) {
    // init empty array that will hold our data later
    $guild_members = [];
    global $guild_page;
    // loop through table rows
    foreach ( $guild_page -> find( '.character-list tbody tr' ) as $row ) {
        // init empty array for individual player
        $guild_member = [];
        $member_name = [];
        $member_gp = [];
        $member_cs = [];
        $member_rank = [];
        $member_rank_avg = [];

        // store data in temporary array
        $guild_member[ 'name' ] = preg_replace( "/\r|\n/", "", trim( $row -> branch( 'td' ) -> eq( 0 ) -> text() ) );
        $guild_member[ 'gp' ] = $row -> branch( 'td' ) -> eq( 1 ) -> text();
        $guild_member[ 'collection_score' ] = $row -> branch( 'td' ) -> eq( 2 ) -> text();
        $guild_member[ 'arena_rank' ] = $row -> branch('td') -> eq(3) -> text();
        $guild_member[ 'arena_average' ] = $row -> branch( 'td' ) -> eq( 4 ) -> text();

        // add player array to full response array
        $guild_members[] = $guild_member;
    }

    foreach ($guild_members as $guild_member) {
        $member_name[] = $guild_member['name'];
    }
    foreach ($guild_members as $guild_member) {
        $member_gp[] = $guild_member['gp'];
    }
    foreach ($guild_members as $guild_member) {
        $member_cs[] = $guild_member['collection_score'];
    }
    foreach ($guild_members as $guild_member) {
        $member_rank[] = $guild_member['arena_rank'];
    }
    foreach ($guild_members as $guild_member) {
        $member_rank_avg[] = $guild_member['arena_average'];
    }

}

// check what page is loaded 
if(strpos($gidURL, 'guild_members') !== false) {
    $name_insert = "INSERT INTO gid-1-800-druidia (name, galactic_power, collection_score, arena_rank, arena_average) VALUES ('$member_name', '$member_gp', '$member_cs', '$member_rank', '$member_rank_avg')";
 try{
      mysqli_query($dbcon, $name_insert);
 }catch (mysqli_sql_exception $e) {
      echo $e->getMessage();
 }

}else if(strpos($gidURL, 'liquid-schwartz') !== false) {
    // insert LSD data
}else if(strpos($gidURL, 'spaceballsdeep') !== false) {
    // insert SBD data
}else if(strpos($gidURL, 'chapter-eleven') !== false) {
    // insert C11 data
}else if(strpos($gidURL, 'my-own-best-friend') !== false) {
    // insert MOBF data
}else if(strpos($gidURL, 'gone-to-plaid') !== false) {
    // insert GtP data
}else if(strpos($gidURL, 'my-schwartz-is-bigger') !== false) {
    // insert SCH data
}else if(strpos($gidURL, 'ludicrousspeed') !== false) {
    // insert LS data
}else if(strpos($gidURL, 'lone-starr') !== false) {
    // insert LST data
}else {
    echo "No guild info was pulled.";
}

// let's get out of here
exit;

?>
cedebl8k

cedebl8k2#

我必须在这行中添加变量$dbcon:

if(!mysqli_query($dbcon, $name_insert)) {
    die('error inserting new record');
}

以及将表名放在像user3783243这样的反标记中,例如:

$name_insert = "INSERT INTO `gid-1-800-druidia` (name, galactic_power, collection_score, arena_rank, arena_average) VALUES ('$member_name', '$member_gp', '$member_cs', '$member_rank', '$member_rank_avg')";

相关问题