How to save data to a database from file_get_html($url)? - php

I can't save data to database from file_get_html($url) function. My script is showing data nicely by scraping from a url. But I can't save the showed data to a database. It shows error between object and array. I can't even show data by array index. Such as $value[0]. Here is my code sample:
$con=mysqli_connect("localhost","root","","crawler");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
require 'simple_html_dom.php';
$url = "http://www.realestate.com.au/sold/in-perth/list-1";
//Address Collection
$html = file_get_html($url);
foreach ($html->find("h2") as $key => $value){
echo $value."<br>";
$result = mysqli_query($con,"INSERT INTO data (info) VALUES ('$value')");
if (!$result){
echo "Error!<br>";
}
}
mysqli_close($con);
?>

Try this and let me know if it works:
$con=mysqli_connect("localhost","root","","crawler");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
require 'simple_html_dom.php';
$url = "http://www.realestate.com.au/sold/in-perth/list-1";
//Address Collection
$html = file_get_html($url);
foreach ($html->find("h2") as $key => $value){
echo $value."<br>";
}
$value = base64_encode($value);
$result = mysqli_query($con,"INSERT INTO data (info) VALUES ('$value')");
if (!$result){
echo "Error!<br>";
}
mysqli_close($con);
?>

$value need convert to a string before write to DB. You can show array or object use
echo '<pre>';
print_r($value);
echo '</pre>';
or
var_dump($value)

Related

Parsing json array And Merging Objects via php

I Have a json array with id type answer I'm merging id, type and answer together and put them in the id2 column, so why can't I get $answers value in output?
[{"id":"38","answer":[{"option":"3","text":"HIGH"}],"type":"a"},
{"id":"39","answer":[{"option":"3","text":"LOW"}],"type":"b"},
{"id":"40","answer":["Hello Word"],"type":"c"}]
This is my code:
<?php
$con=mysqli_connect("localhost","root","","arrayok");
mysqli_set_charset($con,"utf8");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
while ($row = mysqli_fetch_row($result)){
$json = $row[0];
if(!is_null($json)){
$json = preg_replace("!\r?\n!", "", $json);
$jason_array = json_decode($json,true);
// id2
$id = array();
foreach ($jason_array as $data) {
if (array_key_exists('id', $data)) {
if (array_key_exists('type', $data)) {
if (array_key_exists('answer', $data)) {
foreach($data['answer'] as $ans){
$answers[] = isset($ans['text']) ? $ans['text'] : $ans;
}
$id[] = ' ID='.$data['id'].', TYPE='.$data['type'].', AWNSER='.$answers;
}
}
}
}
// lets check first your $types variable has value or not?
$ids= implode(',',$id); /// implode yes if you got values
$sql1="update user_survey_start set id2='$ids' where us_id=".$row[1];//run update sql
echo $sql1."<br>";
mysqli_query($con,$sql1);
}
}
}
mysqli_close($con);
?>
And this is my output:
update user_survey_start set id2=' ID=38, TYPE=a, AWNSER=Array,
and I got Notice: Array to string conversion in C:\wamp64\www\json\awnser.php on line 29
I want to have value of $answers
Solved By Myself
Because I Couldn't Put Awnser Directly To id[], I Taked Awnser Like This:
$id[] = ' ID='.$data['id'].', TYPE='.$data['type'];
$id[] = isset($ans['text']) ? ' AWNSER='.$ans['text'] : ' AWNSER='.$ans;
And This is My Code:
<?php
$con=mysqli_connect("localhost","root","","arrayok");
mysqli_set_charset($con,"utf8");
// Check connection
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT `survey_answers`,us_id FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
while ($row = mysqli_fetch_row($result)){
$json = $row[0];
if(!is_null($json)){
$json = preg_replace("!\r?\n!", "", $json);
$jason_array = json_decode($json,true);
// id2
$id = array();
foreach ($jason_array as $data) {
if (array_key_exists('id', $data)) {
if (array_key_exists('type', $data)) {
if (array_key_exists('answer', $data)) {
foreach($data['answer'] as $ans){
$id[] = ' ID='.$data['id'].', TYPE='.$data['type'];
$id[] = isset($ans['text']) ? ' AWNSER='.$ans['text'] : ' AWNSER='.$ans;
}
}
}
}
}
// lets check first your $types variable has value or not?
$ids= implode(',',$id); /// implode yes if you got values
$sql1="update user_survey_start set id2='$ids' where us_id=".$row[1];//run update sql
echo $sql1."<br>";
mysqli_query($con,$sql1);
}
}
}
mysqli_close($con);
?>

Split a Column of json Object to More Column via PHP

How Can I Split This json Object to 3 Part id awnser and type ?
[{"id":"26","answer":[{"option":"3","text":"HIGH"}],"type":"a"},
{"id":"30","answer":[{"option":"3","text":"LOW"}],"type":"b"},
{"id":"31","answer":[{"option":"3","text":"LOW"}],"type":"c"}]
And db Name: array Table Name: user_survey_start JSON Column Name: survey_answers, This is my code:
<?php
$con=mysqli_connect("localhost","root","","arrayy");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT `survey_answers` FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql)){
while ($row = mysqli_fetch_row($result)){
}
mysqli_close($con);
?>
Try using json_decode()
<?php
$sql="SELECT `survey_answers` FROM `user_survey_start`";
if ($result=mysqli_query($con,$sql))
{
while ($row = mysqli_fetch_row($result))
{
$json = $row[0];
$jason_array = json_decode($json,true);
foreach ($jason_array as $data){
$id[] = $data['id'];
$answer[] = $data['answer'];
$type[] = $data['type'];
// here code to insert/update values to db column
}
echo implode(',',$id);
echo implode(',',$answer);
echo implode(',',$type);
}
}

Wwriting links to database in a foreach loop fails

I am using simple html dom to write records to a database, however it doesn't seem to write the records.
The problem is with the foreach loop. It outputs all the urls and the following error:
Notice: Undefined variable: url in C:\xampp\htdocs\meh\crawler.php on line 28
<?php
// Create connection
$con=mysqli_connect("localhost","root","spidermankillssuperman","expatriates");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
<?php
include_once('../simple_html_dom.php');
$links = array (
'http://www.expatriates.com/classifieds/bhr/hs/index100.html'
);
foreach ($links as $link) {
$html = file_get_html($link);
foreach($html->find('a') as $element) {
if(strpos($element->href, "cls"))
$url = "http://expatriates.com".$element->href . '<br>';
echo $url;
}
$sql="INSERT INTO urlstocrawl (url)
VALUES ('$url')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo '<br>'.'<p>'."1 record added";
}
mysqli_close($con);
?>
you want your mysql query to insert foreach link in you html file, not run once after the loop. move the code inside the loop:
foreach ( $html->find('a') as $element ) {
if ( strpos($element->href, "cls") !== false ) {
$url = "http://expatriates.com" . $element->href . '<br>';
$sql = "INSERT INTO urlstocrawl (url) VALUES ('$url')";
if ( !mysqli_query($con,$sql) ) {
die('Error: ' . mysqli_error($con));
}
echo '<br>'.'<p>'."1 record added" . $url;
}
}
Note the modification to the if condition. strpos() returns either false when the string is absent or an integer indicating the position, starting at 0. This means the condition could fail if cls is at the start of the string. With this strict type checking you can be sure of the desired behavior.

MYSQL foreach Row Echo Data

How do I echo out every column's data of a row from MYSQL results?
I do not know what the rows are as the query is dynamically created.
Here's what I have:
$query = $_POST['query'];
// Create connection
$con=mysqli_connect($db_host, $db_user, $db_pass, $db_name);
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($results)) {
}
$row is just an array. Like any other array you can do fun things like iterate over it:
while ($row = mysqli_fetch_array($results)) {
foreach ($row as $key => $value) {
echo 'Key: ' . $key . ', Value: ' . $value;
}
echo "<br><br>\n"
}

Grabbing data from steamapi (json) and importing it to mysql

I can't quite figure out how to import data from remote json (steamapi) into MySql.
<?php
$con = mysql_connect("localhost","XXXXXXXXXXXXXXXXXXXX","XXXXXXXXXXXXXXXXXXXXXXXXX");
mysql_select_db('XXXXXXXXXXXXXXXXX',$con);
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXX&steamids=76561198033811393";
$json = file_get_contents($url);
$result = json_decode($json);
foreach($result as $key => $value)
{
if($value)
{
mysql_query("INSERT INTO steam (steamid,
communityvisibilitystate,
profilestate)
VALUES ($value->steamid,
$value->communityvisibilitystate,
$value->profilestate)");
}
mysql_close($con);
}
?>
Steam JSON Image(click for full size):
You can read each player using $data->response->players, it will be assigned to $player where then, you can directly access their object like $player->steamid. See below example:
$data = json_decode($json);
foreach($data->response->players as $player)
{
echo $player->steamid, "\n\n";
}
I also suggest you change your query like this:
$query = sprintf("INSERT INTO steam (steamid,
communityvisibilitystate,
profilestate) VALUES ('%s','%s','%s')",
mysql_real_escape_string($player->steamid),
mysql_real_escape_string($player->communityvisibilitystate),
mysql_real_escape_string($player->profilestate));
To prevent any further issues like broken strings or injections.
Resulting code for the SQL part would be:
// Build the query
$query = sprintf("INSERT INTO steam (steamid,
communityvisibilitystate,
profilestate) VALUES ('%s','%s','%s')",
mysql_real_escape_string($player->steamid),
mysql_real_escape_string($player->communityvisibilitystate),
mysql_real_escape_string($player->profilestate));
// Perform Query
$insert = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$insert)
{
echo 'Invalid query: ' . mysql_error() . "\n";
echo 'Whole query: ' . $query . "\n";
echo "------------------\n";
}
Given your actual code it would look like this:
<?php
$con = mysql_connect("localhost","XXXXXXXXXXXXXXXXXXXX","XXXXXXXXXXXXXXXXXXXXXXXXX");
mysql_select_db('XXXXXXXXXXXXXXXXX',$con);
$url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=XXXXXXXXXXXXXXXXXXXXXXXX&steamids=76561198033811393";
$json = file_get_contents($url);
$data = json_decode($json);
foreach($data->response->players as $player)
{
// Build the query
$query = sprintf("INSERT INTO steam (steamid,
communityvisibilitystate,
profilestate) VALUES ('%s','%s','%s')",
mysql_real_escape_string($player->steamid),
mysql_real_escape_string($player->communityvisibilitystate),
mysql_real_escape_string($player->profilestate));
// Perform Query
$insert = mysql_query($query);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$insert)
{
echo 'Invalid query: ' . mysql_error() . "\n";
echo 'Whole query: ' . $query . "\n";
echo "------------------\n";
}
}
mysql_close($con);
Use this
<?php
$id = $_GET['id'];
$key = 'xxx';
$link = file_get_contents('http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=' . $key . '&steamids=76561198033811393&format=json');
$myarray = json_decode($link, true);
$result = $myarray['response']['players'][0];
echo $result->steamid;
?>
if you get more then one result use foreach format given below
$result = $myarray['response']['players'];
foreach($result as $key => $value)
{
echo $value->steamid."<br>";
}

Categories