未定义的索引:___ mysqli_ston

时间:2015-01-14 10:53:32

标签: php mysqli

我试图从文件中将一些json数据解析成mysql数据库。我的原始代码如下:

<?php
$response = array();
$res=array();
$json = file_get_contents('C:\Users\Richard\Desktop\test.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);
}

}
}

&GT;

由于被诽谤而失败;所以我没有改变错误处理程序来忽略这个(非常糟糕的做法),而是选择使用这里提供的方便的转换工具转换为mysqli:https://github.com/philip/MySQLConverterTool

我在前面提到的代码上运行转换器,它生成了以下内容:

<?php 
$response = array(); 
$res=array(); 
$json = file_get_contents('C:\Users\Richard\Desktop\test.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)并且不知道如何修复它,任何帮助都将非常感激。

ps我正在使用laravel框架,如果这会产生影响或打开其他解决方案。

我现在知道错误与我没有数据库连接字符串的事实有关,即$ GLOBALS [&#34; ___ mysqli_ston由生成器生成。

我的理解是laravel负责在其mvc架构中定义数据库连接,因此不需要重新定义。考虑到这一点,我的代码会是什么样子?

1 个答案:

答案 0 :(得分:4)

你必须使用类似的东西连接到mysql

$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)