問題描述
我正在嘗試將一些 json 數據從文件解析到 mysql 數據庫中.我的原始代碼如下:
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);
}
}
}
?>
由于被貶低而失敗;因此,我沒有更改錯誤處理程序以忽略這一點(非常糟糕的做法),而是選擇使用此處提供的便捷轉換工具轉換為 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
我在上述代碼上運行轉換器并生成以下內容:
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);
}
}
}
?>
在運行此代碼時,我收到標題中的錯誤消息(未定義索引:___mysqli_ston)并且不知道如何修復它,任何幫助將不勝感激.
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 框架,如果這有所作為或開辟了其他解決方案.
ps I am using the laravel framework if that makes a difference or opens up other solutions.
我現在知道錯誤與我沒有數據庫連接字符串有關,即 $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.
據我了解,laravel 負責在其 mvc 架構中定義數據庫連接,因此不需要重新定義.考慮到這一點,我的代碼會是什么樣子?
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 ?
推薦答案
你一定已經用這樣的東西連接到 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)
有了這個
$result = mysqli_query($con, "INSERT INTO configuration_item(configurationItemVersion,configurationItemCaptureTime,configurationStateId)
這篇關于未定義索引:___mysqli_ston的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!