-1

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:\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);
}

}
}

?>

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:\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); 
} 

} 
} 



?> 

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 I am using the laravel framework if that makes a difference or opens up other solutions.

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.

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 ?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Richard Hewitt
  • 345
  • 1
  • 5
  • 22
  • What error message? Please get into detail so that we can help you. Also, hava look at http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php because of a MySQL injection vulnerability. – Jelmer Jan 14 '15 at 10:56
  • Error is defined in the title – Utkarsh Dixit Jan 14 '15 at 11:05
  • @Jelmer I have modified the question with further information which may help you to answer my question thanks – Richard Hewitt Jan 14 '15 at 12:21

1 Answers1

4

you must have connected to mysql using something like this

$con=mysqli_connect("localhost","my_user","my_password","my_db");

replace your this line

$result = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO     configuration_item(configurationItemVersion,configurationItemCaptureTime,configurationStateId) 

with this

$result = mysqli_query($con, "INSERT INTO     configuration_item(configurationItemVersion,configurationItemCaptureTime,configurationStateId) 
Kuldeep Dangi
  • 4,126
  • 5
  • 33
  • 56