DOMXPath query error , not working in web server - php

i am using the following code to grab some data from a site and store them in a database .
<?php
error_reporting(0);
include("database.php");
$dom = new DOMDocument();
$url = "http://www.kitco.com";
$html = file_get_contents($url);
$dom->loadHTML($html);
$xp = new DOMXPath($dom);
get_live_spot_gold();
get_silver_and_pgm();
function get_live_spot_gold()
{
global $xp;
$live_spot_gold = array();
$bid='000';
$change='000';
$qs = $xp->query('//div[#class="item_border"]/table/tr[#class="alternating"][1]/td[2]');
foreach($qs as $q)
{
$bid = $q->textContent;
break;
}
$qs = $xp->query('//div[#class="item_border"]/table/tr[#class="alternating"][2]/td[2]');
foreach($qs as $q)
{
$change = $q->textContent;
break;
}
//insert into db
echo $bid."<br>";
echo $change."<br>";
$query = "UPDATE live_spot_gold SET _bid = '$bid' , _change = '$change'";
echo $query;
$result = mysql_query($query);
if(!$result)echo "problem in live spot gold"."<br>";
}
function get_silver_and_pgm()
{
global $xp;
$silver_and_pgm = array();
$cnt=0;
$qs = $xp->query('//td[#id="right_column"]/div[#class="item_container"][2]/div[#class="item_border"]/table/tr');
foreach ($qs as $q)
{
$line = $q->nodeValue;
$demo="";
for($i=0;$i<strlen($line);$i++)
{
if($line[$i]==' ')
{
$demo.=' ';
for( ;$line[$i]==' ';$i++);
}
$demo.=$line[$i];
}
$words = explode(" ",$demo);
$silver_and_pgm[$cnt][0]=$words[0]; //metal name
$silver_and_pgm[$cnt][1]=$words[1]; //bid
$silver_and_pgm[$cnt][2]=$words[2]; //change
$cnt++;
}
for($i=0;$i<$cnt;$i++)
{
$metal_name = $silver_and_pgm[$i][0];
$bid = $silver_and_pgm[$i][1];
$change = $silver_and_pgm[$i][2];
//echo "here";
echo $metal_name."<br>";
echo $bid."<br>";
echo $change."<br>";
//$query = "insert into 'silver_and_pgm' values('$metal_name','$bid','$change')";
//$query = "UPDATE silver_and_pgm set _bid='$bid' WHERE _metal_name='$metal_name'";
//$query = "UPDATE silver_and_pgm set _bid = '$bid' WHERE _metal_name = '$metal_name'";
$query = "UPDATE silver_and_pgm set _bid='$bid',_change = '$change' WHERE _metal_name
like'$metal_name%'";
echo $query."<br>";
$result = mysql_query($query);
if(!$result)
echo "problem in silver_and_pgm"."<br>";
}
}
?>
its just working fine in my localhost . but not in my web server . Its grabbing data and storing them in localhost ... no query , no echo statement is working in web server.
would anybody please suggest a way to handle this problem ??

DOM requires PHP 5 (and libxml to be installed). Make sure your server meets the requirements by running php_info(). Also, turn back on your error reporting and see what errors you get.

Related

Issue in php script for android app

I have a showProduct.php file from where i want to call a function showProduct() in another file. In showProduct() i want to extract all rows from database and to showProduct.php file. the issue is that when i return the array only last row is showing. I want to show all the rows.
The showProduct.php is:
<?php
require_once '../includes/DbOperations.php';
$response = array();
$result = array();
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$db = new DbOperations();
$result = $db->showProduct();
if(!empty($result))
{
$response["prod_name"] = $result["prod_name"];
$response["prod_desc"] = $result["prod_desc"];
$response["prod_image"] = $result["prod_image"];
}
else
{
$response["error"] = true;
$response["message"] = "products are not shown";
}
}
echo json_encode($response);
?>
and showProduct() function is:
public function showProduct(){
$menu = array();
$query = mysqli_query($this->con,"SELECT * FROM `products` WHERE 1");
while ($row = mysqli_fetch_array($query)) {
$menu['prod_name'] = $row['prod_name'] ;
$menu['prod_desc'] = $row['prod_desc'] ;
$menu['prod_image'] = $row['prod_image'];
}
return $menu;
}
In your function, you are just overwriting the last data each time, you need to build this data up. Create an array with the new data and use $menu[] to add this new data to the list of menus...
public function showProduct(){
$menu = array();
$query = mysqli_query($this->con,"SELECT * FROM `products` WHERE 1");
while ($row = mysqli_fetch_array($query)) {
$newMenu = []; // Clear array to ensure no details left over
$newMenu['prod_name'] = $row['prod_name'] ;
$newMenu['prod_desc'] = $row['prod_desc'] ;
$newMenu['prod_image'] = $row['prod_image'];
$menu[] = $newMenu;
}
return $menu;
}

How do i verify query from mysql database before outputing record

In my code am trying to verify if query is true before outputing result i have tried:
require("init.php");
if(empty($_GET["book"]) && empty($_GET["url"])) {
$_SESSION["msg"] = 'Request not valid';
header("location:obinnaa.php");
}
if(isset($_GET["book"]) && isset($_GET["url"])) {
$book = $_GET['book'];
$url = $_GET['url'];
$drs = urldecode("$url");
$txt = encrypt_decrypt('decrypt', $book);
if(!preg_match('/(proc)/i', $url)) {
$_SESSION["msg"] = 'ticket printer has faild';
header("location:obinnaa.php");
exit();
} else {
$ql = mysqli_query($conn, "select * from books where book='$txt' AND used='loading'");
$count = mysqli_num_rows($sql);
if($count < 1) {
$_SESSION["msg"] = 'Transation has oready been made by a customer please check and try again';
header("location:obinnaa.php");
exit();
}
while($riow = mysqli_fetch_assoc($ql)) {
$id = $riow["id"];
$tqty = $riow["quantity"];
for($b = 0; $b < $tqty; $b++) {
$run = rand_string(5);
$dua .= $run;
}
}
$sql = mysqli_query($conn, "select * from books where book='$txt' AND used='loading'");
$split = $dua;
$show_plit = str_split($split, 5);
$b = 0;
while($row = mysqli_fetch_assoc($sql)) {
$id = $row["id"];
$qty = $row["quantity"];
$oldB = $b;
$am = " ";
for(; $b < $oldB + $qty; $b++) {
$am .= "$show_plit[$b]";
$lek = mysqli_query($conn, "UPDATE books SET ticket='$am' WHERE id=$id");
}
if($lek) {
$adr = urlencode($adr = "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
$ty = encrypt_decrypt("encrypt", $txt);
$vars = array(
"book" => $ty,
"url" => $adr
);
$querystring = http_build_query($vars);
$adr = "viewbuy.php?" . $querystring;
header("location: $adr");
} else {
$_SESSION["msg"] = 'Transation failed unknow error';
header("location:obinnaa.php");
}
}
}
}
but i get to
$_SESSION["msg"]='Transation has oready been made by a customer please check and try again
even when the query is right what are mine doing wrong.
Check your return variable name from the query. You have $ql when it should be $sql.
$sql = mysqli_query($conn, "select * from books where book='$txt' AND used='loading'");
$count = mysqli_num_rows($sql);
A good IDE would flag this. NetBeans is a good free one.
Public Service Announcement:
NEVER build SQL queries straight from a URL parameter. Always sanitize your inputs and (better yet) use parameterized queries for your SQL calls. You can Google these topics for more info.

Fetching single data returns error

I'm trying to fetch couple of single data in my server database but this is throwing some errors. The incoming data is correct. The search function just don't get completed.
Here's the code:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
define('HOST','xxxxxxxxxxx');
define('USER','xxxxxxxxxxxx');
define('PASS','xxxxxxxxx');
define('DB','xxxxxxxxxx');
$con = mysqli_connect(HOST,USER,PASS,DB);
$post_id = $_POST['id'];
$buyer_mobile = $_POST['mobile'];
$buyer_name = $_POST['name'];
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysqli_query($con,$sql);
$owner_mobile = $row['mobile'];
$sql = "select name from user where mobile='$owner_mobile'";
$r = mysqli_query($con,$sql);
$owner_name = $row['name'];
$sql = "INSERT INTO flat_booking (post_id,owner_mobile,owner_name,buyer_mobile,buyer_name) VALUES ('$post_id','$owner_mobile','$owner_name','$buyer_mobile','$buyer_name')";
if(mysqli_query($con,$sql)){
echo "Success";
}
else{
echo "error";
}
mysqli_close($con);
}else{
echo 'error1';
}
What am I doing wrong here? Maybe this:
$owner_mobile = $row['mobile'];
Thanks in advance!
create table flatower and add mobile column
$post_id = 1;
$sql = "select mobile from flatowner where id='$post_id'";
$res = mysql_query($con,$sql);
$row = mysql_fetch_array($res);
$owner_mobile = $row[0]['mobile'];
Your problem is this line:
$owner_mobile = $row['mobile'];
You have not created the $row variable. For this you would need to do something such as:
Do this first:
<?php
$row = array();
while ($result = mysqli_fetch_assoc($res))
{
$row[] = $result;
}
?>
This allows you to do this:
<?php
foreach ($row as $r)
{
var_dump($r); print "<br />"; // One row from the DB per var dump
}
?>

SQL statement in while loop

For some reason my SQL UPDATE statement is not working.
$con=mysqli_connect("localhost","admin","password","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$items = array();
$desc = array();
$price = array();
$quantity = array();
$p=0;
while($inventorynumber>$p)
{
$invid[$p] = $_POST['invid'.$p.''];
$items[$p] = $_POST['item'.$p.''];
$desc[$p] = $_POST['desc'.$p.''];
$price[$p] = $_POST['price'.$p.''];
$quantity[$p] = $_POST['quantity'.$p.''];
$sql = "UPDATE `inventory_db` SET `item`='$items[$p]',`description`='$desc[$p]',`quantity`='$quantity[$p]',`price`='$price[$p]' WHERE `invid` = '$invid[$p]'";
$p++;
}
Is there a more efficient way of doing this? Or do I need to add in a mysqli_close($con); after?
I would run the sql query in the loop itself. Unless you're using the array somewhere later, you can simplify this. It's not clear where $inventorynumber is being set. Also, remember to set autocommit.
mysqli_autocommit($con, TRUE);
$p=0;
while($inventorynumber > $p) {
$invid = $_POST['invid'.$p.''];
$item = $_POST['item'.$p.''];
$desc = $_POST['desc'.$p.''];
$price = $_POST['price'.$p.''];
$quantity = $_POST['quantity'.$p.''];
$sql = "UPDATE `inventory_db` SET `item`='$items[$p]',`description`='$desc[$p]',`quantity`='$quantity[$p]',`price`='$price[$p]' WHERE `invid` = '$invid[$p]'";
mysqli_query($conn, $sql);
$p++;
}
mysqli_close($conn);

SQL won't work? It doesn't come up with errors either

I have PHP function which checks to see if variables are set and then adds them onto my SQL query. However I am don't seem to be getting any results back?
$where_array = array();
if (array_key_exists("location", $_GET)) {
$location = addslashes($_GET['location']);
$where_array[] = "`mainID` = '".$location."'";
}
if (array_key_exists("gender", $_GET)) {
$gender = addslashes($_GET["gender"]);
$where_array[] = "`gender` = '".$gender."'";
}
if (array_key_exists("hair", $_GET)) {
$hair = addslashes($_GET["hair"]);
$where_array[] = "`hair` = '".$hair."'";
}
if (array_key_exists("area", $_GET)) {
$area = addslashes($_GET["area"]);
$where_array[] = "`locationID` = '".$area."'";
}
$where_expr = '';
if ($where_array) {
$where_expr = "WHERE " . implode(" AND ", $where_array);
}
$sql = "SELECT `postID` FROM `posts` ". $where_expr;
$dbi = new db();
$result = $dbi->query($sql);
$r = mysql_fetch_row($result);
I'm trying to call the data after in a list like so:
$dbi = new db();
$offset = ($currentpage - 1) * $rowsperpage;
// get the info from the db
$sql .= " ORDER BY `time` DESC LIMIT $offset, $rowsperpage";
$result = $dbi->query($sql);
// while there are rows to be fetched...
while ($row = mysql_fetch_object($result)){
// echo data
echo $row['text'];
} // end while
Anyone got any ideas why I am not retrieving any data?
while ($row = mysql_fetch_object($result)){
// echo data
echo $row->text;
} // end while
I forgot it wasn't coming from an array!

Categories