問(wèn)題描述
我正在嘗試將一些 json 數(shù)據(jù)從文件解析到 mysql 數(shù)據(jù)庫(kù)中.我的原始代碼如下:
I am trying to parse some json data, into a mysql database, from a file. My original code was as follows:
<?php
$response = array();
$res=array();
$json = file_get_contents('C:UsersRichardDesktop est.json');
if($json!=null){
$decoded=json_decode($json,true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];
if(is_array($decoded["configurationItems"]))
{
foreach($decoded["configurationItems"] as $configurationItems)
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)
{
$configurationItemVersion=$configurationItems["configurationItemVersion"];
echo "<br />","configurationItemVersion:",$configurationItemVersion,"<br />";
$configurationItemCaptureTime=$configurationItems["configurationItemCaptureTime"];
echo "configurationItemCaptureTime:",$configurationItemCaptureTime,"<br />";
$configurationStateId=$configurationItems["configurationStateId"];
echo "configurationStateId:",$configurationStateId,"<br />";
$result = mysql_query("INSERT INTO configuration_item(configurationItemVersion,configurationItemCaptureTime,configurationStateId)
VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId')")or die("Insert Failed ".mysql_error());;
}// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully stored";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
?>
由于被貶低而失敗;因此,我沒(méi)有更改錯(cuò)誤處理程序以忽略這一點(diǎn)(非常糟糕的做法),而是選擇使用此處提供的便捷轉(zhuǎn)換工具轉(zhuǎn)換為 mysqli:https://github.com/philip/MySQLConverterTool
which failed due to being depricated; so rather than altering the error handler to ignore this (really bad practice), I opted to convert to mysqli using a handy conversion tool sourced here : https://github.com/philip/MySQLConverterTool
我在上述代碼上運(yùn)行轉(zhuǎn)換器并生成以下內(nèi)容:
I ran the converter on the aforementioned code and it generated the following:
<?php
$response = array();
$res=array();
$json = file_get_contents('C:UsersRichardDesktop est.json');
if($json!=null){
$decoded=json_decode($json,true);
//$decode= var_dump($decoded);
//$ss=$decode["array"];
//echo $decoded['number'];
if(is_array($decoded["configurationItems"]))
{
foreach($decoded["configurationItems"] as $configurationItems)
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)
{
$configurationItemVersion=$configurationItems["configurationItemVersion"];
echo "<br />","configurationItemVersion:",$configurationItemVersion,"<br />";
$configurationItemCaptureTime=$configurationItems["configurationItemCaptureTime"];
echo "configurationItemCaptureTime:",$configurationItemCaptureTime,"<br />";
$configurationStateId=$configurationItems["configurationStateId"];
echo "configurationStateId:",$configurationStateId,"<br />";
$result = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO configuration_item(configurationItemVersion,configurationItemCaptureTime,configurationStateId)
VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId')")or die("Insert Failed ".((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error(
"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;
}// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["code"] = 1;
$response["message"] = "successfully stored";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["code"] = 2;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}
}
?>
在運(yùn)行此代碼時(shí),我收到標(biāo)題中的錯(cuò)誤消息(未定義索引:___mysqli_ston)并且不知道如何修復(fù)它,任何幫助將不勝感激.
upon running this code I get the error message in the title (Undefined index: ___mysqli_ston) and have no idea how to fix it, any help would be much appreciated.
ps 我正在使用 laravel 框架,如果這有所作為或開(kāi)辟了其他解決方案.
ps I am using the laravel framework if that makes a difference or opens up other solutions.
我現(xiàn)在知道錯(cuò)誤與我沒(méi)有數(shù)據(jù)庫(kù)連接字符串有關(guān),即 $GLOBALS["___mysqli_ston 是由生成器生成的.
I now know that the error relates to the fact that I have no database connection string ie $GLOBALS["___mysqli_ston is generated by the generator.
據(jù)我了解,laravel 負(fù)責(zé)在其 mvc 架構(gòu)中定義數(shù)據(jù)庫(kù)連接,因此不需要重新定義.考慮到這一點(diǎn),我的代碼會(huì)是什么樣子?
it was my understanding that laravel took care of defining the database connection in its mvc architecture and therefore this does not need to be redefined. with that in mind what would my code look like ?
推薦答案
你一定已經(jīng)用這樣的東西連接到 mysql
you must have connected to mysql using something like this
$con=mysqli_connect("localhost","my_user","my_password","my_db");
替換你的這一行
$result = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO configuration_item(configurationItemVersion,configurationItemCaptureTime,configurationStateId)
有了這個(gè)
$result = mysqli_query($con, "INSERT INTO configuration_item(configurationItemVersion,configurationItemCaptureTime,configurationStateId)
這篇關(guān)于未定義索引:___mysqli_ston的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!