PHP Simple XML Parser, blank response.
The main problem with the code below is that without the parser it will work fine but as soon as the XML parser is added, so it can connect to the other server from the client, it will not return with the results, just a blank page?
Also this is a project, so before anyone ask's, why do you not have any SQLi protection. I have not as of yet implemented it yet.
Here is the code below:
Client side XML parser code: hinphp.php
<?php
$hin = $_GET["hin"];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "http://192.168.0.12/hinbuy.php?hin=");
curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);
curl_setopt($connection,CURLOPT_HEADER, 0);
$response = curl_exec($connection);
$xml = simplexml_load_string($response);
for($index=0; $index < count($xml->songs); $index++)
{
echo $xml->songs[$index]->ID . "<br />";
echo $xml->songs[$index]->song . "<br />";
echo $xml->songs[$index]->artist . "<br />";
echo $xml->songs[$index]->year . "<br />";
echo $xml->songs[$index]->genre . "<br />";
echo $xml->songs[$index]->quantity . "<br />";
$ID = $xml->songs[$index]->ID;
echo "<a href='http://192.168.0.12/buyclihin.php?ID=$ID'>buy</a>";
}
curl_close($connection);
?>
Server side PHP select code: hinbuy.php
<?php
header("Content-type: text/xml");
echo "<?xml version='1.0' encoding='ISO-8859-1'?>";
echo "<hit>";
$hin = $_GET["hin"];
$conn = new PDO("mysql:host=localhost;dbname=xxxxx;","xxxxx","xxxxx");
$results = $conn->query("SELECT * FROM `ghit` WHERE `artist`='$hin'");
while($row=$results->fetch())
{
echo "<songs>";
echo "<id>$row[ID]</id>";
echo "<song>$row[song]</song>";
echo "<artist>$row[artist]</artist>";
echo "<year>$row[year]</year>";
echo "<genre>$row[genre]</genre>";
echo "<quantity>$row[quantity]</quantity>";
echo "</songs>";
}
echo "</hit>"
?>
Is it probably because you are not passing $hin to hibbuy.php?
Try:
$hin = $_GET["hin"];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "http://192.168.0.12/hinbuy.php?hin=" . $hin);
I have been tasked with trying to map a json file to a database a sample of the json is given below:
configurationItems":[
{
"configurationItemVersion":"1.0",
"configurationItemCaptureTime":"2014-12-05T10:22:51.751Z",
"configurationStateId":1,
"relatedEvents":[ ],
"awsAccountId":"val",
"configurationItemStatus":"v",
"resourceId":"fg",
"ARN":"ggggg",
"awsRegion":"us-east-1",
"availabilityZone":"us-east-1b",
"configurationStateMd5Hash":"ggd45",
"resourceType":"AWS::EC2::Instance",
"resourceCreationTime":"2014-01-06T10:37:37.000Z",
"tags":{
"Name":"",
"cirrushq_id":""
},
using the following code I have been able to map the entries down to resource creation time but now wish to map the tags to a different table called tag
<?php
$con=mysqli_connect("localhost","root","","json_map");
$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 />","configuration_Item_Version:",$configurationItemVersion,"<br />";
$configurationItemCaptureTime=$configurationItems["configurationItemCaptureTime" ];
echo "configurationItemCaptureTime:",$configurationItemCaptureTime,"<br />";
$configurationStateId=$configurationItems["configurationStateId"];
echo "configurationStateId:",$configurationStateId,"<br />";
$awsAccountId=$configurationItems["awsAccountId"];
echo "awsAccountId:",$awsAccountId,"<br />";
$configurationItemStatus=$configurationItems["configurationItemStatus"];
echo "configurationItemStatus:",$configurationItemStatus,"<br />";
$resourceId=$configurationItems["resourceId"];
echo "resourceId:",$resourceId,"<br />";
$ARN=$configurationItems["ARN"];
echo "ARN:",$ARN,"<br />";
$awsRegion=$configurationItems["awsRegion"];
echo "awsRegion:",$awsRegion,"<br />";
etc etc
$result = mysqli_query($con, "INSERT INTO configuration_item(configuration_item_version,configuration_item_capture_time,configuration_state_id, aws_account_id, configuration_item_status, resource_id, arn, aws_region, availability_zone,configuration_state_md5_hash, resource_type, resource_creation_time)
VALUES('$configurationItemVersion','$configurationItemCaptureTime','$configurationStateId','$awsAccountId','$configurationItemStatus','$resourceId','$ARN','$awsRegion','$availabilityZone','$configurationStateMd5Hash','$resourceType','$resourceCreationTime' )")or die("Insert Failed ".((is_object($con)) ? mysqli_error($con) : (($___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 configuration items ";
// 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);
}
}
}
?>
I tried duplicating the code into a separate file and altering the insert statement to
if(is_array($decoded["configurationItems"]))
{
foreach($decoded["configurationItems"] as $configurationItems)
//for($i=0;$i>sizeof($decoded["configurationItems"]);$i++)
{
$Name=$configurationItems["Name"];
echo "<br />","Name:",$Name,"<br />";
$cirrushq_id=$configurationItems["cirrushq_id"];
echo "<br />","cirrushq_id:",$cirrushq_id,"<br />";
$result = mysqli_query($con, "INSERT INTO tag(name, cirrushq_id)
VALUES('$Name','$cirrushq_id' )")or die("Insert Failed ".((is_object($con)) ? mysqli_error($con) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));;
but to no avail it just returns unknown index Name
From what you wrote, I'm not completely sure to understand what's the cause of your problem.
However, let me point out a few inconsistencies I spotted:
1) Since you speak about tags (plural), I'm guessing you'd like to have more than one tag associated to each configurationItems element. In this case your JSON should be:
...
"tags":[{
"Name":"",
"cirrushq_id":""
},{
"Name":"",
"cirrushq_id":""
}]
...
2) According your JSON, to access tag name and tag cirrushq_id you should do:
$Name=$configurationItems["tags"]["Name"];
$cirrushq_id=$configurationItems["tags"]["cirrushq_id"];
and in the case you take into consideration my point 1), you'd have to make another loop through the tags:
foreach($decoded["configurationItems"] as $configurationItems)
{
...
foreach($decoded["configurationItems"]["tags"] as $tags)
{
$Name=$tags["Name"];
echo "<br />","Name:",$Name,"<br />";
$cirrushq_id=$tags["cirrushq_id"];
echo "<br />","cirrushq_id:",$cirrushq_id,"<br />";
// clearly, the INSERT query should also be inside this nested loop
}
}
3) From what you wrote in the tag INSERT query, you have a lowercase name column, while in your JSON, the index Name is uppercase...I know it's trivial but sometimes, when there's not another pair of eyes that can double-check your code, it's just simple stuff like this that make you waste a lot of time.
Hope to have been of help!
I have an xml file and now it's been inserted to MySQL database, but there was a problem in the xml file which meant some data may be deleted from the site, so I updated the xml file and I wrote code for deleting data from database table which deleted from xml but the records are still in the database.
from another way I want to compare between xml file and database and delete data not found.
<?php
ini_set('display_errors','On');
//connecting to database.
echo "connected to DB<br /><br />";
$url = "test.xml";
$xmlfgc = file_get_contents($url);
$xmlitem = new SimpleXMLElement($xmlfgc);
echo "xml loaded<br /><br />";
foreach ($xmlitem->property as $xml) {
//$id = mysql_real_escape_string($xml->id);
$mls_id = mysql_real_escape_string($xml->ref);
//echo "$mls_id";
echo "xml parsed<br /><br />";
$result = mysql_query("SELECT mls_id FROM ezrealty");
//$select = "SELECT mls_id FROM ezrealty";
//$storeArray = Array();
//while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
// $storeArray[] = $row['mls_id'];
//}
if ($result != $mls_id){
$query = "DELETE FROM ezrealty WHERE mls_id = '$mls_id'";
mysql_query($query) or die(mysql_error());
echo "DataBase deleted<br /><br />";
}else{
echo "There is no deleted properties from xml file.";
}
}
//show updated records
echo "<br /><br />";
printf ("Records updated: %d\n", mysql_affected_rows());
//close connection
mysql_close($con2);
?>
thanks for you, now i have the solution for my problem.
<?php
ini_set('display_errors','On');
//connecting to database.
echo "connected to DB<br /><br />";
$url = "test.xml";
$xmlfgc = file_get_contents($url);
$xmlitem = new SimpleXMLElement($xmlfgc);
echo "xml loaded<br /><br />";
$xmldata = array();
foreach ($xmlitem->property as $xml) {
$mls_id = mysql_real_escape_string($xml->ref);
// $xmldata[] = "$mls_id";
$xmldata[] = '("' . $mls_id. '")';
}
//Get all reference numbers from database in array
$result = mysql_query("SELECT mls_id FROM gitfd_ezrealty");
$storeArray = Array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$storeArray[] = $row['mls_id'];
}
//get difference between two arrays.
$result1 = array_diff($xmldata, $storeArray);
foreach ($result1 as $value) {
//echo "<strong>$value</strong> <br />";
//$value1 = '("' . $value. '")';
//$query = 'INSERT INTO gitfd_ezrealty (mls_id)
//VALUES' . implode(',', $value1);
//mysql_query($query) or die(mysql_error());
//echo "inserted into mysql<br /><br />";
}
echo "These properties are founded in target site and Not founded in our site.<br />";
echo "________________________________________________________________"."<br />";
print_r($result1);
echo "<br />"."_____________________________________________________________________"."<br />";
$result2 = array_diff($storeArray, $xmldata);
foreach ($result2 as $value) {
//echo "<strong>$value</strong> <br />";
$query = "DELETE FROM ezrealty WHERE mls_id = '$value'";
mysql_query($query) or die(mysql_error());
echo "DataBase deleted<br /><br />";
}
echo "_________________________________________________________________"."<br />";
print_r($result2);
echo "<br />"."______________________________________________________________________"."<br />";
echo "These properties were deleted .<br />";
//show updated records
echo "<br /><br />";
printf ("Records updated: %d\n", mysql_affected_rows());
//close connection
mysql_close($con2);
?>
I have a code which fetches data from a mysql table and converts it into pdf document, the code is working fine except it is skipping row 1.
Here is the code from which i have removed the pdf generation process since the problem is in the loop which is fetching data.
Please help.
<?php
session_start();
if(isset($_SESSION['user']))
{
$cr = $_POST['cour'];
$s = $_POST['sem'];
require('fpdf.php');
include('../includes/connection.php');
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(!mysql_fetch_array($rs))
{
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
}
else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
$formno[$i] = $row ['FormNo'];
$rno[$i] = $row ['rollno'];
$snm[$i] = $row ['StudentNm'];
$fnm[$i] = $row ['FathersNm'];
$mnm[$i] = $row ['MothersNm'];
$addr[$i] = $row['Address'];
$pic[$i] = $row['imagenm'];
$comm[$i] = $row['SocialCat'];
echo $formno[$i]."<br />";
echo $rno[$i]."<br />";
echo $snm[$i]."<br />";
echo $fnm[$i]."<br />";
echo $mnm[$i]."<br />";
echo $addr[$i]."<br />";
echo $pic[$i]."<br />";
echo $comm[$i]."<br />";
echo "<br />";
}
}
mysql_close($con);
}
?>
You are fetching the first row outside of your for() loop then you miss it.
After mysql_query() your should use mysql_num_rows() to check if there are any rows in your result and then fetch them in the for loop.
More info here : http://php.net/manual/fr/function.mysql-num-rows.php
Your code would look like this :
$sql = "SELECT * FROM `student` WHERE AppliedCourse ='$cr'";
$rs = mysql_query($sql) or die($sql. "<br/>".mysql_error());
if(0 == mysql_num_rows($rs)) {
$_SESSION['db_error'] = "<h2><font color = 'RED'>No such course found! Pease select again.</font></h2>";
header('Location: prinrepo.php');
} else {
for($i = 0;$i <= $row = mysql_fetch_array($rs);$i++)
{
// Your code
}
}
I keep getting
Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given in xx on line 6
error what is the problem with this code ? how can I fix it ?
$read = mysql_query("select * from detail");
while($wr = mysql_fetch_array($read)) {
echo $wr['Who'];
echo "<br />";
echo $wr['Time'];
echo "<br />";
echo $wr['What'];
}
edit; I made it like this still giving an error.
$db = new mysqli('localhost', 'root', '', 'panel');
$sql = "select * from detail";
$read = $db->query($sql);
while($wr = mysql_fetch_array($read)) {
echo $wr['Who'];
echo "<br />";
echo $wr['Time'];
echo "<br />";
echo $wr['What'];
}
You are mixing the MySQL and MySQLi extensions!
Use the appropriate functions of either, not both.
You probably having problems with querying database since your query fails:
$read = mysql_query("select * from detail"); // $read is false
// You can try to discover the error.
$read = mysql_query("select * from detail") || die(mysql_error());
Use this code:
$read = mysql_query("select * from detail");
while($wr = mysql_fetch_array($read, MYSQL_ASSOC)) {
echo $wr['Who'];
echo "<br />";
echo $wr['Time'];
echo "<br />";
echo $wr['What'];
}
See for more information: mysql-fetch-array