Blogger Information
Blog 48
fans 3
comment 1
visits 36981
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
mysql数据操作实例(一个简单的彩票自动采集)——2018年4月23日
JackBlog
Original
1845 people have browsed it

知识点:

1、mysql数据连接:mysqli_connect(host,username,password,dbname,port,socket)

2、返回最后一个错误代码:mysqli_errno(connection)

3、返回最后一个错误描述:mysqli_error(connection)

4、执行查询:mysqli_query(connection,query)

5、执行一个或多个查询:mysqli_multi_query(connection,query)

6、返回结果集中行的数量:mysqli_num_rows(result)

7、返回前一次操作所影响的记录行数:mysqli_affected_rows(connection)

8、检查一个多查询是否有更多的结果:mysqli_more_results(connection)

9、从结果集中取得一行,并作为枚举数组返回:mysqli_fetch_row(result)

10、关闭数据库连接:mysqli_close(connection)


今天学习了mysql操作,制作了一个简单的彩票开奖自动采集工具(采集并写入数据库)。

效果图如下:

QQ截图20180423234811.png

QQ截图20180423235345.png

cj.html实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数据采集</title>
    <style type="text/css">
        .container{
            width: 100%;
        }
        .main{
            width: 1000px;
            margin: auto;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="main"><table>
    <tr>
        <td>北京快乐8</td>
        <td><textarea name="bjkl8" id="bjkl8" cols="100" rows="3"></textarea></td>
    </tr>
    <tr>
        <td>北京PK10</td>
        <td><textarea name="bjpk10" id="bjpk10" cols="100" rows="3"></textarea></td>
    </tr>
    <tr>
        <td>重启时时彩</td>
        <td><textarea name="cqssc" id="cqssc" cols="100" rows="3"></textarea></td>
    </tr>
    <tr>
        <td>澳洲快乐8</td>
        <td><textarea name="aukeno" id="aukeno" cols="100" rows="3"></textarea></td>
    </tr>
    <tr>
        <td>加拿大卑斯快乐8</td>
        <td><textarea name="cakeno" id="cakeno" cols="100" rows="3"></textarea></td>
    </tr>
</table></div>
</div>



</body>
</html>

<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<script type="text/javascript">
    window.setInterval(function () {
            $.get('cj.php','gamecode=bjkl8',function (res) {
                $('#bjkl8').text(res)
            })
            $.get('cj.php','gamecode=bjpk10',function (res) {
                $('#bjpk10').text(res)
            })
            $.get('cj.php','gamecode=cqssc',function (res) {
                $('#cqssc').text(res)
            })
            $.get('cj.php','gamecode=aukeno',function (res) {
                $('#aukeno').text(res)
            })
            $.get('cj.php','gamecode=cakeno',function (res) {
                $('#cakeno').text(res)
            })
    }
        ,5000)



</script>

运行实例 »

点击 "运行实例" 按钮查看在线实例


cj.php实例

<?php
require 'inc/function.php';
$gamecode = $_GET['gamecode'];
if (isset($gamecode)){
    opencj('http://f.apiplus.net/'.$gamecode.'.json',$gamecode);
}

运行实例 »

点击 "运行实例" 按钮查看在线实例


inc/function.php实例

<?php

function opencj($cjurl, $gamecode='bjkl8')
{
    $url=$cjurl;
    $fh= file_get_contents($url);

if (strpos($fh,'频率过快') >0){
    echo 'error:'.$fh;
}else{

    $data = getSubstr($fh,'[',']');
    $data = '{'.getSubstr($data,'{','}').'}';
    $data = json_decode($data,true);
    $expect = $data['expect'];
    $opencode = substr($data['opencode'],0,59);
    $opentime = $data['opentime'];
    //判断游戏code重定义期号,部分游戏期号从01开始,给这些期号加上日期,防止重复。
    switch ($gamecode){
        case 'aukeno':
            $expect = '20'.date('ymd',time()).$expect;
            break;
    }
    //连接数据库
    include_once ('inc/mysql.php');
    //定义sql语句insert
        $sql = "INSERT IGNORE into `cj_$gamecode` (`id`, `result`, `datetime`) VALUES ('$expect', '$opencode', '$opentime');";
    //执行sql语句
        if (mysqli_query($mysql,$sql)){
            if (mysqli_affected_rows($mysql)>0){
                echo 'expect:'.$expect.' opencode:'.$opencode.' opentime:'.$opentime.' actualtime:20'.date('y-m-d h:i:s',time());
            }else{
                echo '已更新 expect:'.$expect.' opencode:'.$opencode.' opentime:'.$opentime.' actualtime:20'.date('y-m-d h:i:s',time());
            }
    }
    mysqli_close($mysql);
}
}

function getSubstr($str, $leftStr, $rightStr)
{
    $left = strpos($str, $leftStr);
    //echo '左边:'.$left;
    $right = strpos($str, $rightStr,$left);
    //echo '<br>右边:'.$right;
    if($left < 0 or $right < $left) return '';
    return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr));
}

?>

运行实例 »

点击 "运行实例" 按钮查看在线实例


inc/mysql.php实例

<?php
define('dbhost','127.0.0.1');
define('dbuser','caipiao');
define('dbpass','123456');
define('dbname','caipiao');
define('dbchar','utf8');
$mysql = mysqli_connect(dbhost,dbuser,dbpass,dbname) or die('连接失败'.mysqli_connect_error($mysql));

mysqli_set_charset($mysql,dbchar);

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!