douyujun0152 2016-06-01 16:15
浏览 59

为什么PHP中的mySQL返回null结果?

I am trying to migrate ~55K records from a mySQL server over to MongoDB. I can't do this via any of the easily accessible methods like JSON/CSV importing because the data storage method (the way it is structured) will be very different. Because of this, I have created a script in php that is designed to do this.

The issue I have been running into with this is that over large sets of data (not reproducible using smaller data sets even when the smaller set includes problem entries) the queries will occasionally report no data despite the entry existing. It absolutely exists because when php accesses that specific entry directly or it is included in a smaller data set, it works just fine. For instance, in the import to a text file, i only received ~42k/54k records.

In the echo I am receiving through the url the php file is called through, I am showing that the query is called the correct number of times, but there are many records that are showing as not existing, thus the answering echo is blank. The code is included below:

//Makes a connection to the database
$conn = makeConnection();

$filename = '/home/dbserverdownload.txt';
$file = fopen($filename, 'a');

$sql = "SELECT * FROM maintable ORDER BY ID DESC LIMIT 1";
$resultID = mysqli_query($conn, $sql);
$ID = mysqli_fetch_object($resultID);
echo $ID->ID;
//loops through the database and appends the data to the file as it goes
for($var=2; $var <= $ID->ID; $var++){
    $sql1 = "SELECT * FROM servertable WHERE ID = '$var'";
    $result1 = mysqli_query($conn, $sql1);
    $values = mysqli_fetch_object($result1);

    $id =  $values->ID;
    $ip = $values->IP;
    $port = $values->port;
    $running = $values->running;
    $afk = $values->afk;
    $gamemode = $values->gamemode;
    $maxplayers = $values->maxplayers;
    $spawnprotection = $values->spawnprotection;
    $whitelist = $values->whitelist;
    $enablequery = $values->enablequery;
    $enablercon = $values->enablercon;
    $rconpassword = $values->rconpassword;
    $motd = $values->motd;
    $achievements = $values->announceplayerachievements;
    $allowflight = $values->allowflight;
    $spawnanimals = $values->spawnanimals;
    $spawnmobs = $values->spawnmobs;
    $forcegamemode = $values->forcegamemode;
    $hardcore = $values->hardcore;
    $pvp = $values->pvp;
    $difficulty = $values->difficulty;
    $generatorsettings = $values->generatorsettings;
    $levelname = $values->levelname;
    $levelseed = $values->levelseed;
    $leveltype = $values->leveltype;
    $autosave = $values->autosave;

if($ip == "148.57.44.10"){
  //if the server is server1
  $servername = "server1".$port;
} else if ($ip == "165.108.22.199"){
  //if the server is server2
  $servername = "server2".$port;
} else{
  $servername = "";
}



//Adds all content that was already gained to the JSON string
$startingContent = "{\"_id\":\"$servername\",
                    \"ip\":\"$ip\",
                    \"port\":\"$port\",
                    \"running\":\"$running\",
                    \"afk\":\"$afk\",
                    \"gamemode\":\"$gamemode\",
                    \"maxplayers\":\"$maxplayers\",
                    \"spawnprotection\":\"$spawnprotection\",
                    \"whitelist\":\"$whitelist\",
                    \"enablequery\":\"$enablequery\",
                    \"enablercon\":\"$enablercon\",
                    \"rconpassword\":\"$rconpassword\",
                    \"motd\":\"$motd\",
                    \"announceplayerachievements\":\"$achievements\",
                    \"allowflight\":\"$allowflight\",
                    \"spawnanimals\":\"$spawnanimals\",
                    \"spawnmobs\":\"$spawnmobs\",
                    \"forcegamemode\":\"$forcegamemode\",
                    \"hardcore\":\"$hardcore\",
                    \"pvp\":\"$pvp\",
                    \"difficulty\":\"$difficulty\",
                    \"generatorsettings\":\"$generatorsettings\",
                    \"levelname\":\"$levelname\",
                    \"levelseed\":\"$levelseed\",
                    \"leveltype\":\"$leveltype\",
                    \"autosave\":\"$autosave\"
                  }";



echo $startingContent."<br/>";


//This is the JSON data that will be passed to mongo

if(strlen($ip)>6){
    if (fwrite($file, $startingContent) === FALSE) {
        echo "Cannot write to file ($filename) with $startingContent";
        exit;
    }
}
}

I have also tried this with a query that pulls a significant number(all, half, a quarter, etc) of the results in one chunk instead of tons of individual queries. The end result of that experiment was that a variable number of records were updated (usually a seemingly random number between 400 and 4000) each time it was run. Does anyone have any idea why this might be occurring? If not, should I just make my own program to iterate over the CSVs that I can export from mySQL?

  • 写回答

1条回答 默认 最新

  • duancheng3342 2016-06-01 16:56
    关注

    First of all, replace your query $sql = "SELECT * FROM maintable ORDER BY ID DESC LIMIT 1"; with this $sql = "SELECT MAX(ID) as ID FROM maintable"; for better performance.

    Your problem here is that you probably has a query that return false, then your application dies. Let's say, for example, your loop tries to query ID=3, but there is no row in your database with this ID. Then the next lines throws a exception, that you are not seeing. You could use set display_errors=On in you dev machine php.ini.

    Use this to check if your have results to fetch:

    ....
    for($var=2; $var <= $ID->ID; $var++){
        $sql1 = "SELECT * FROM servertable WHERE ID = '$var'";
        $result1 = mysqli_query($conn, $sql1);
    
        if($result) {
            $values = mysqli_fetch_object($result1);
    
            $id =  $values->ID;
            $ip = $values->IP;
            ....
    
    评论

报告相同问题?

悬赏问题

  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。