Reputation: 261
I have a question: how do you I extract a value from database using pdo? Current code:
$item = $_GET['item'];
$item = str_replace("\"", "", $item);
$item = str_replace("\'", "", $item);
$item = str_replace(" ", "%20", $item);
$item = str_replace("\\", "", $item);
@include_once ("pdocon.php");
$stmt = $dbh->prepare("SELECT * FROM items WHERE name=?");
$stmt->execute(array($item));
$rs = $stmt->fetch(PDO::FETCH_ASSOC);
$stmtt = $dbh->prepare("SELECT auth FROM auth where id=1");
$code = $stmtt->fetch(PDO::FETCH_ASSOC);
I am trying to get $code as a result of the mysql query "SELECT auth FROM auth where id=1" from the database. I don't know why but it's not working.
UPDATE: Ok so this is the whole code of the php file that extracts $code and puts it in a URL link which is an API call. After running this php, I keep getting 0 values.
<?php
$item = $_GET['item'];
$item = str_replace("\"", "", $item);
$item = str_replace("\'", "", $item);
$item = str_replace(" ", "%20", $item);
$item = str_replace("\\", "", $item);
@include_once ("pdocon.php");
$stmt = $dbh->prepare("SELECT * FROM items WHERE name=?");
$stmt->execute(array($item));
$rs = $stmt->fetch(PDO::FETCH_ASSOC);
$stmtt = $dbh->prepare("SELECT auth FROM auth where id=1");
$stmtt->execute();
$code = $stmtt->fetch(PDO::FETCH_ASSOC);
if(!empty($rs)) {
if(time()-$rs["lastupdate"] < 604800) die($rs["cost"]);
}
$link = "https://bitskins.com/api/v1/get_item_price/?api_key=(MYKEYHERE)&code=".$code."&names=".$item."&delimiter=!END!";
$string = file_get_contents($link);
$json = $string;
$obj = json_decode($json);
if($obj->{'status'} == "fail") die("notfound");
$lowest_price = $obj->data->prices[0]->{'price'};
$lowest_price = (float)($lowest_price);
$stmt = $dbh->prepare("UPDATE items SET `cost` = ?,`lastupdate` = ? WHERE `name` = ?");
$stmt->execute(array($lowest_price, time(), $item));
$stmt = $dbh->prepare("INSERT INTO items (`name`,`cost`,`lastupdate`) VALUES (?, ?, ?)");
$stmt->execute(array($item, $lowest_price, time()));
echo $lowest_price;
print_r($lowest_price);
?>
Upvotes: 1
Views: 1598
Reputation: 5256
You haven't executed $stmtt, so there's no result set to fetch.
$stmtt = $dbh->prepare("SELECT auth FROM auth where id=1");
$stmtt->execute();
$code = $stmtt->fetch(PDO::FETCH_ASSOC);
Upvotes: 6