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

时间:2018-07-23 03:56:21

标签: php mysql database

我正在使用自定义Web搜寻器从网站(获得许可)中提取信息,并将所有信息存储到单独的列表中。然后,我想将该信息存储在我在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);

?>

2 个答案:

答案 0 :(得分:0)

如果您在connect_mysql.php中添加了mysql连接,则无法在不将其作为参数传递的情况下访问该函数内部的MYSQL连接变量。

所以您需要像下面那样更改代码,

<?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;

?>

答案 1 :(得分:0)

我必须在这一行中添加变量$ 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')";