Store htmldom results in mysql database - php

I try to store scraped data into my database. just to echo the result works perfectly, so the scraping is OK. But there must be an error in defining a variable and passing it to the mysql-insert. I get the message "New record created successfully". The result for the variable is empty and the date is there.
<?php
$html = file_get_contents('https://www.marketwatch.com/market-data/us?mod=market-data-center');
$scriptDocument = new DOMDocument();
libxml_use_internal_errors(TRUE);
if(!empty($html)){
$scriptDocument->loadHTML($html);
libxml_clear_errors();
$scriptDOMXPath = new DOMXPath($scriptDocument);
$scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]//following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){echo $row->nodeValue;}} // echo result works
$scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]//following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){$row->nodeValue = $nasdaq_dec;}} // defining variable does not work
};
$host_name = '';
$database = '';
$user_name = '';
$password = '';
try {
$conn = new PDO("mysql:host=$host_name; dbname=$database;", $user_name, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO `morgenroutine` (date,nasdaq_dec)
VALUES (NOW(), '$nasdaq_dec')";
$conn->exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

Now I got it! I changed the format in mysql to "text" and the line to:
$scriptRow = $scriptDOMXPath->query('//th[starts-with(text(), "ISSUES:")]//following::td[6]');if($scriptRow->length > 0){foreach($scriptRow as $row){$nasdaq_dec = $row->nodeValue;}}

Related

Have problem with write the code to check database and insert into different table

I wrote some code to check database table name is hms_history if type = commission then will add another row of data into hms_history but change amount_balance to -amount_balance then will add another data to table hms_deposit. I try to write the code but when I check it show only connect successful, but nothing changes in the database at all.
This is my code
<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";
function setChecked($conn,$params) {
$s = $conn->prepare("UPDATE `hm2_history`
SET ref_id=-1
WHERE id=:id
");
$s->execute($params);
}
try {
$conn = new PDO("mysql:host=$servername;dbname=xxxx", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$stmt = $conn->prepare("SELECT * FROM hm2_history
WHERE id NOT IN(SELECT ref_id FROM hm2_history WHERE ref_id > 0) AND
ref_id=0 AND commission=0");
//deposits
$s = $conn->prepare("INSERT INTO `hm2_deposits`
SET `user_id`=:user_id,
`type_id`=:type_id,
`deposit_date`=:deposit_date,
`last_pay_date`=:last_pay_date,
`status`=:status,
`q_pays`=:q_pays,
`amount`=:amount,
`actual_amount`=:actual_amount,
`ec`=:ec,
`compound`=:compound,
`dde`=:dde,
`unit_amount`=:unit_amount,
`bonus_flag`=:bonus_flag,
`init_amount`=:init_amount,
`ref_id`=:ref_id
");
$v['type_id']= 9;
unset($v['id']);
$lastDepositId = $conn->lastInsertId();
$date = date('Y-m-d H:i:s');
$s->execute($v);
//history1
$s = $conn->prepare("INSERT INTO `hm2_history`
SET `user_id`=:user_id,
`amount`=:amount,
`type`=:type,
`description`=:description,
`actual_amount`=:actual_amount,
`date`=:date,
`ec`=:ec,
`deposit_id`=:deposit_id,
`rate`=:rate
");
$x = array(
'user_id'=>$v['user_id'],
'type'=>'bonus',
'amount'=>$v['amount'],
'description'=>'Bonus note',
'actual_amount'=>$v['actual_amount'],
'date'=>$date,
'ec'=>68,
'deposit_id'=>0,
'rate'=>1.0,
);
$s->execute($x);
//history2
$s = $conn->prepare("INSERT INTO `hm2_history`
SET `user_id`=:user_id,
`amount`=:amount,
`type`=:type,
`description`=:description,
`actual_amount`=:actual_amount,
`date`=:date,
`ec`=:ec,
`deposit_id`=:deposit_id,
`rate`=:rate
");
$x = array(
'user_id'=>$v['user_id'],
'type'=>'depoit',
'amount'=> -$v['amount'],
'description'=>'Deposit from plan',
'actual_amount'=> -$v['actual_amount'],
'date'=>$date,
'ec'=>999,
'deposit_id'=>$lastDepositId,
'rate'=>1.0,
);
$s->execute($x);
?>

UPDATE mysql table from Oracle query

So I've seen posts of updating mysql table from mysql queries, but I can't seem to find the right answer for updating from an Oracle dbase query. I've tried so many different ways of formatting this that now I'm just at a loss of things to try. I'm not sure whether I need to be echoing the results of the oracle query or not. Any help would be greatly appreciated and I'll be happy to try and explain anything that's not clear. I'm not a programmer by any means, just basically teaching myself.
<?php
error_reporting(E_ALL ^ E_NOTICE);
$SD = $_POST[SD];
$ED = $_POST[ED];
$db = '(DESCRIPTION=
(ADDRESS=
(PROTOCOL=TCP)
(HOST=host)
(PORT=port)
)
(CONNECT_DATA=
(SERVICE_NAME=name)
)
)
';
$c1 = oci_connect("user", "pass", $db);
$sql = oci_parse($c1, "ALTER SESSION SET NLS_DATE_FORMAT = 'mm/dd/yyyy'");
oci_execute($sql);
$sql = oci_parse($c1, "SELECT ulid, itmcod, locatn, casqty, ctlgrp, expdat, subsit, qastat from invent_view");
oci_execute($sql);
$results = array();
while ($result = oci_fetch_array($sql)){
$results[] = $result;
foreach ($results as $result){
$ULID = ($result['ULID']);
$ITMCOD = ($result['ITMCOD']);
$LOCATN = ($result['LOCATN']);
$CASQTY = ($result['CASQTY']);
$CTLGRP = ($result['CTLGRP']);
$EXPDAT = ($result['EXPDAT']);
$SUBSIT = ($result['SUBSIT']);
$QASTAT = ($result['QASTAT']);
}
echo $ULID . '<br/>';
echo $ITMCOD . '<br/>';
echo $LOCATN . '<br/>';
echo $CASQTY . '<br/>';
echo $CTLGRP . '<br/>';
echo $EXPDAT . '<br/>';
echo $SUBSIT . '<br/>';
echo $QASTAT . '<br/>';
}
?>
<?php
$servername = "server";
$username = "user";
$password = "pass";
try {
$conn = new PDO("mysql:host=$servername;dbname=dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE `dbname`.`invent_view` SET `ULID` = '$ULID', `ITMCOD` = '$ITMCOD', `LOCATN` = '$LOCATN', `CASQTY` = '$CASQTY', `CTLGRP` = '$CTLGRP', `EXPDAT` = '$EXPDAT', `SUBSIT` = '$SUBSIT', `QASTAT` = '$QASTAT'";
$stmt = $conn->prepare($sql);
$stmt->execute();
echo $stmt->rowCount() . "records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>

Warning: Illegal string offset - Syntax [duplicate]

I'm using the following code to make a connection to the database, fetch the Data_length index column, and calculate the database size based on the data.
For some reason PDO will always return "0", which is the value for the Data_length index in the first row. Whatever I do, I only get the first rows index.
The database is MySQL, the engine MyISAM.
PHP Version: 5.5.38
MySQL Version: 5.5.50
Here is the source code.
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<?php
try {
error_reporting(-1);
$host_name = "my_host";
$database = "my_db";
$user_name = "my_user";
$password = "my_pwd";
$conn = new PDO("mysql:host=$host_name;dbname=$database", $user_name, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $conn->query('SHOW TABLE STATUS');
$dbSize = 0;
$row = $sth->fetch(PDO::FETCH_ASSOC);
$dbSize = $row["Data_length"];
$decimals = 2;
$mbytes = round($dbSize/(1024*1024),$decimals);
echo $dbSize . "\n" . $row["Data_length"];
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
</body>
</html>
Add a while loop,
while($row= $sth->fetch( PDO::FETCH_ASSOC )){
echo $row['your_field_name'];
}
Or you can use fetchAll
$rows = $sth->fetchAll();
print_r($rows);

PDO fetch returns only first row

I'm using the following code to make a connection to the database, fetch the Data_length index column, and calculate the database size based on the data.
For some reason PDO will always return "0", which is the value for the Data_length index in the first row. Whatever I do, I only get the first rows index.
The database is MySQL, the engine MyISAM.
PHP Version: 5.5.38
MySQL Version: 5.5.50
Here is the source code.
<!DOCTYPE html>
<head>
<title></title>
</head>
<body>
<?php
try {
error_reporting(-1);
$host_name = "my_host";
$database = "my_db";
$user_name = "my_user";
$password = "my_pwd";
$conn = new PDO("mysql:host=$host_name;dbname=$database", $user_name, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $conn->query('SHOW TABLE STATUS');
$dbSize = 0;
$row = $sth->fetch(PDO::FETCH_ASSOC);
$dbSize = $row["Data_length"];
$decimals = 2;
$mbytes = round($dbSize/(1024*1024),$decimals);
echo $dbSize . "\n" . $row["Data_length"];
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
</body>
</html>
Add a while loop,
while($row= $sth->fetch( PDO::FETCH_ASSOC )){
echo $row['your_field_name'];
}
Or you can use fetchAll
$rows = $sth->fetchAll();
print_r($rows);

PDO/SQL insert not working

got the following code. All values are gotten through javascript and then sent through ajax. The var_dump($array) at the end works and display all the correct values. So they are all passed through correctly. The catch error for the try method also never pops up. The values are not being inserted into the sql table. Whats wrong?
Thanks in advance.
$name = $_GET['name'];
$category = $_GET['category'];
$subCategory = $_GET['subCategory'];
$date = $_GET['date'];
$address = $_GET['address'];
$city = $_GET['city'];
$state = $_GET['state'];
$host = $_GET['host'];
$imagePath = $_GET['imagePath'];
$info = $_GET['info'];
//turn into array
$array = array();
$array[0]=$name;
$array[1]=$category;
$array[2]=$subCategory;
$array[3]=$date;
$array[4]=$address;
$array[5]=$city;
$array[6]=$state;
$array[7]=$host;
$array[8]='j';//$imagePath;
$array[9]=$info;
try {
$con = new PDO('mysql:host=localhost;dbname=test');
$insert = $con->prepare(" INSERT INTO create
(name,category,subCategory,date,address,city,state,host,imagePath,info)
VALUES (?,?,?,?,?,?,?,?,?,?) ");
$insert->execute($array);
}
catch(PDOException $e) { //try
echo 'error';
//echo 'ERROR: ' . $e->getMessage();
}
var_dump($array);
create is a reserved word in mysql so you need to quote it in backticks:
INSERT INTO `create` ...
To have PDO throw exceptions, you need to add that after you open your connection:
$con = new PDO('mysql:host=localhost;dbname=test');
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
By the way, I assume that you are logging into your database with a username and a password as well (the second and third parameter of the PDO constructor)...

Categories