I'm building a script using PHP and MySQL to compare a certain live page against older versions of it - i'm doing it by md5 hashing it and comparing it to the latest version.
Now i'm trying to pull the latest known hash of a certain page using the following:
SELECT latest_hash FROM tracked_sites WHERE domain = 'domain.com
Which shows me the actual contant of latest_hash for a certain "domain.com"
Now i'm trying to put it in a valid variable so i can compare it using the following:
$latestmd5_sql=(mysqli_query($con,"SELECT latest_hash FROM tracked_sites WHERE domain = 'domain.com'"));
Now, thinking i have the actual content of the database i'm trying to compare it with
if ((md5(file_get_contents("https://domain.com/page.html")))==$latestmd5_sql)
BUT, for some reason i get False as answer.
I've tried to print out the $latestmd5_sql var using echo or print_r but it seems to be empty or empty array, i'm a bit puzzled as to what i'm doing wrong and would love to get ideas.
In your provided code, $latestmd5_sql will be a resource, not the value in the database.
You'll need to "fetch" the data from the resource in order to compare values.
Here's an example to illustrate the workflow from sql code to php variable:
// your sql
$sql="SELECT latest_hash FROM tracked_sites WHERE domain = 'domain.com'";
// the query (returns a resource)
$query = mysqli_query($con,$sql);
// fetch the resulting data (this is the part you're missing)
$result=mysqli_fetch_assoc($query);
// take a look at the data (for debugging purposes)
echo"DATA:<pre>".print_r($result,true)."</pre>";
// compare
if ((md5(file_get_contents("https://domain.com/page.html")))==$result['latest_hash']) {
echo"<p>Match</p>";
} else {
echo"<p>No Match</p>";
}
Related
I'm using my client's mysql database. My client has form search fields: voltage, power, size, material and length. They want the visitor to enter any combination of these and get some results. I've tried using AND and we get no results, I tried using OR and got all the records no matter what. What am I missing? The form data is turned into $POST_variables like $pvoltage. Any ideas on how to do this?
WHERE
(voltage = '$pvoltage' AND power_kw = '$ppower' AND size LIKE '$psize%' AND
material = '$pmat' AND immers_length LIKE '$plength%')
Also tried it with OR - gave me everything instead of just the results where there was data. This gives me nothing.
This finally worked for me in the WHERE statement:
IF('$pvoltage' ='',voltage LIKE '%',voltage LIKE '$pvoltage')
I'm needing to create more than 1 filter to run a query to cut down the number of results found.
I am trying to do these 3 queries at once using RethinkDB for php found here
The code I am running is:
$query = \r\table('payments')->filter(
\r\row('forwarded')->eq('1'),
\r\row('bad_callbacks_sent')->lt("1"),
\r\row('confirmations')->le('7')
)->run($this->conn);
I've just also tried doing the following and it doesn't work (it's just showing all where the first argument - where forwarded = 1. It's only doing the first filter:
$query = \r\table('payments')
->filter(\r\row('forwarded')->eq('1'))
->filter(\r\row('bad_callbacks_sent')->lt("6"))
->filter(\r\row('confirmations')->le("7"))
->run($this->conn);
But it doesn't seem to be doing what I ask.
I need to make it get:
Where forwarded == 1
Where bad_callbacks_sent < 1
Where confirmations <= 7
I found this the following code from here, which shows that you can chain them in js but I'm wondering about PHP:
r.db('items').table('tokens')
.filter(r.row('valid_to').gt(r.now()))
.filter(r.row["processed"] == False)
Any ideas?
Okay so it turns out, the issue was not with my new code:
$query = \r\table('payments')
->filter(\r\row('forwarded')->eq('1'))
->filter(\r\row('bad_callbacks_sent')->lt(6))
->filter(\r\row('confirmations')->le(7))
->run($this->conn);
But with the variable type.
In my database, it was stored as an int but I was trying to read it via (le("7")) a string.
The above code works fine :)
It allows you to filter more than 1 query in retihnkdb for php.
Make sure you check your variable types in your database!
RethinkDB is very sensitive to what types they're.
Hope I save you some time.
I am trying to extract from my php laravel some data like this:
$x = Carbon::now()->timestamp;
$data=Notifications->where('happened_at','>',$x)
->where('id_user',Auth::user()->getAuthIdentifier());
If I enter this sql statement in my oracle database directly it works perfectly:
select * from notifications where happened_at > '10-06-2017 00:11:12,000000000';
^ this returns correct, however in my php laravel it doesn't return the good rows(returns all the rows from the database)
Later Edit: The where is the problem, I just want to compare the timestamp location in my DB('happened_at') with the current time...I don't know how though
Let's break down your model call code.
// first, you retrieve all notifications and assign them to $data
$data = Notifications::all()
// then, you try to start a new query
->where('happened_at','>',$x)
// then, you continue the new query
->where('id_user',Auth::user()->getAuthIdentifier())
// and finally, you finish the call
;
So basically, you already get all rows at the beginning, and then you try to start building a query that you never execute. (But you can't, since all() returns a collection.)
Remove the all() and finish up with ->get() at the end, and it should work. (Although I don't know anything about Oracle timestamps.)
I have an external database that I am trying to access from within a Drupal page, I have successfully queried the database and output data to the page using fetchAssoc(), however this only returns the first row in the database. I would like to return all rows into an array for processing, so I'm attempting to use fetchAllAssoc(), this however results in an exception. The database has the following SQL fields:
id, model, manufacturer, url, date_modified
My test code is as follows:
<?php
db_set_active('product_db');
$query = db_select('product', 'p')->fields('p');
$sqlresults = $query->execute()->fetchAllAssoc('id');
foreach($sqlresults as $sqlresult)
{
printf($sqlresult);
}
db_set_active();
?>
I'm thinking that it is the key field 'id' that I am specifying with fetchAllAssoc() that is the problem, as fetchAssoc() prints values correctly. All documentation I have found seems to say that you pass a database field as the key but I have also passed a numeric value with no success.
Many thanks in advance for any advice, I'm sure I'm just missing something stupid.
I think it should work in this way, but within the foreach you want to print the $sqlresult variable as a string, but it is an object (it causes the error).
printf function needs a string as the first parameter, see:
http://php.net/manual/en/function.printf.php
Use for instance var_dump instead:
var_dump($sqlresult);
Trying to make a little Search feature for a user, so he can type in a date on a webpage made with HTML/PHP, and see which people in the db have registered as member on or after (a date). My user inputs the date in format 2015-10-01. This gets sent to a PHP page with a jqxGrid on it, populated with member details of members conforming to my query on the MySQL database (using PDO).
The query uses the operator >= on a string passed as (for example) "2015-10-01" in the WHERE clause, so I am using STR_TO_DATE to make the comparison work:
WHERE `lastUpdated` >= STR_TO_DATE( ? , '%Y-%m-%d');
With PDO, the ? later gets bound to the date (which was passed in as a string).
The db column for registration date is in DATETIME format, and in the db values look like: "2015-10-12 17:12:52".
My query returns an empty array every time, - and this after many hours of trying every conceivable permutation of date format, both in the MySQL statement and on the page that prepares the data for populating the grid.
Can someone show me what's wrong here?
Thanks!!
SP
Make it
WHERE `lastUpdated` > ?
and check your data and stuff.
Basically, you should never touch PDO until you get raw SQL to work.
okay, so here is the PDO version that works - passing in ? instead of the date:
function getJSONAllMembersByDate($PDOdbObject, $regDate)
{
try
{
$membersByDateSQL = "SELECT `id`, `name_first`, `name_last`, `organization`,`email`, `phone`,`source`,`comments`,`language_id`, `lastUpdated` FROM `member` WHERE lastUpdated>=?";//'$regDate'
$get=$PDOdbObject->prepare($membersByDateSQL);
$get->execute(array($regDate));
$rows = $get->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($rows);
return $json;
}
The fact that it works proves there were other errors in the file containing the jqxwidget (the version before I posted here). I certainly tried about a million different things to get this working.
I don't know if this counts as an answer, but at least it WORKS! There are so many variables in this problem - json, jqxgrid, pdo ... not forgetting that there are several ways to use PDO. I probably had several errors in different places.
(#apokryfos, the STR_TO_DATE was indeed unnecessary.)
In the end, this is what works:
In the PHP page containing the jqxGrid, the url sent to the server is:
url: 'my-json-responses.php?fct=getJSONAllMembersByDate®Date=<?php echo $fromDate ?>'
This $fromDate comes from the $_POST when the user typed in a date (in the format 2015-10-01) on the input page. When the PHP page containing the jqxGrid loads, it does
$fromDate = $_POST['regDate'];
The url "transits" through the file my-json-reponses.php, which contains many functions. It finds the right one:
if ($_GET['fct'] == 'getJSONAllMembersByDate')
{
$result = getJSONAllMembersByDate($connectionObject, $_GET['regDate']);
echo $result;
}
The $result is called on the file that contains all my PDO database requests, including:
function getJSONAllMembersByDate($PDOdbObject, $regDate) { try
{
$membersByDateSQL = "SELECT `id`, `name_first`, `name_last`, `organization`,`email`, `phone`,`source`,`comments`,`language_id`, `lastUpdated` FROM `member` WHERE lastUpdated>='$regDate'";
$get=$PDOdbObject->query($membersByDateSQL);
$rows = $get->fetchAll(PDO::FETCH_ASSOC);
$json=json_encode($rows);
return $json;
}
catch (PDOException $e)
{
echo "There was a problem getting all members with this search query.";
echo $e->getMessage();
}}
Note that I couldn't make the version using "?" in the query work at all, hence passing in the variable $regDate directly, with single quotes around the variable just to make life interesting.
This returns a nice list of all my users as of 2015-10-01 - but is presumably still open to MySQL injection attacks ...
But after this marathon of debugging I am happy enough for now. (All improvements welcomed, naturally!)
SP