php redis hscan return empty results - php

I have problem with hscan command in php.
When i run command in redis-cli everything works well.
hscan some:key 0 match *word*
But in php i've got empty result.
<?php
$it = null;
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$results = $redis->hscan('products:search2', $it, '*word*');
var_dump($results);
I use phpredis php extension.
Why is that?

Ok, i've got this. I just forget to select database to 1,
because my data is on database 1.
Redis as a default look in database 0.
The answer is:
<?php
$redis->select(1);

Related

Select data from table using REDISQL in Redis

I have install Redis in Ubuntu 16.04.6 successfully.
Here is the demo code for set and get value in redis.
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
//set the data in redis string
$redis->set("tutorial-name", "Redis tutorial");
// Get the stored data and print it
echo "Stored string in redis:: " .$redis->get("tutorial-name");
?>
And I have got correct output as below.
# php redis.php
Stored string in redis:: Redis tutorial
Now I want to store some value in redis with SQL so I configure Redis with RedisSQL and it configures correctly and working fine with a command line.
I have create one DB and insert one record for testing.
Here is the output of the SQL select query in Redis.
# redis-cli -p 6379
127.0.0.1:6379> REDISQL.EXEC DB "SELECT * FROM foo;"
1) 1) (integer) 3
2) "bar"
Now I want to try to get the same query result in PHP script so I search for this and find like using rawcommand to get command output in redis so I tried using the same command as below.
<?php
//Connecting to Redis server on localhost
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->rawCommand('REDISQL.EXEC', 'foo', 'SELECT * FROM foo;');
echo "Foo table Data:";
print_r($result);
?>
But get $result value blank.
# php redis.php
Foo table Data:
#
So can anyone guide me to get the correct result for SQL query, with rawCommand or any other command?
Thanks in advance.
Try to give the select statement in double quotes
$result = $redis->rawCommand("REDISQL.EXEC", "foo", "SELECT * FROM foo");

Call to a member function fetch_assoc() on a non-object on linux server but not on windows

I am getting the above error when I run this page on a linux server, but it doesn't give me any errors when running it on a windows machine.
The following is my script:
include_once '/../src/init.php';
$userid = $user_data['userid'];
$date = ('Y-m-d'); //get todays date from the server
$db = new mysqli('localhost', 'root', '', 'logfile');
if ($db->connect_error) {
$error = $db->connect_error;
} else {
$sql = "SELECT a.kiloLogid,a.travelDate,a.openning, a.closing, a.clientsName,a.timeIn,a.timeOut, a.destination,a.diff
FROM viewDailyDiff a
WHERE a.`userid` = $userid AND a.`travelDate` = '".$date."'";
$result = $db->query($sql);
if ($db->error) {
$error = $db->error;
}
}
function getRow($result)
{
return $result->fetch_assoc(); //the error is on this line
}
Please assist
I thing the included file init.php
should have the call for the getRow function.
and when there is a problem with Linux to Windows change there are most chances to find solution with .htaccess. Try not to download the .htaccess
All the best
Usually when this happens to me, it's because there is no data returned. If the "work/fail" change is moving to a new server, often this is because the new server has no permission to get the data. So no results are returned. Even thought the original server with the same select statement had records returned, avoiding the error.
If no results are returned, calling fetch_assoc on an empty data set will toss an error in more recent php. So the version of php from one server to the next can also be a factor.
Put in code to only call fetch_assoc if there are records returned (never a bad idea) and/or try to run the command manually (command line?) using mysql client from the command prompt to verify permissions and data. Often the ClI will be more verbose with the error results.

How to get the version of mongodb from php

I need a way to display a version of Mongo database in the php website.
For example, I want to show something like "powered by MongoDB version 2.2.6"
I cannot find a way to find out the version of database that php mongoClient is connected to.
I'm sure that is a way to do this. Does anyone know how?
$mongo = new \Mongo();
$admin = $mongo->admin;
$infos= $admin->command(array('buildinfo'=>true));
$version = $infos['version'];
die($version);
(You need to be admin to show it)
UPDATE : Without authentication, you can check it by using a MongoDB instance.
$c = new \MongoClient();
$db = 'yourdbname';
$mongo = new \MongoDB($c, $db);
$mongodb_info = $mongo->command(array('serverStatus'=>true));
$mongodb_version = $mongodb_info['version'];
die($mongodb_version);
Check out the serverStatus command:
http://docs.mongodb.org/manual/reference/command/serverStatus/
If you're using the PHP driver, check out:
http://php.net/manual/en/mongodb.command.php
to see how you can run that command.
I am not sure, maybe this:
<?php
$v = `mongo --version`;
print_r($v);
?>
$server_status = db->command(array('serverStatus' => TRUE));
will give you lots of information including version
(you need to be admin to run it)

connecting to mongo replica set from EC2

I am trying to connect to a mongo replica set using PHP,
my code looks like this:
$options = array(
'replicaSet' => 'Repset',
'readPreference' => 'primaryPreferred',
);
$connection = new MongoClient("mongodb://ip-10-1-2-3.ec2.internal:27017,ip-10-1-2-4.ec2.internal:27017/", $options);
When I do this I get an error back:
No candidate servers found
if I simplify the connect string to :
$connection = new MongoClient("mongodb://ip-10-1-2-4.ec2.internal:27017/");
Then it connects to the server, but this is not the right way, given I am using replica Sets
What am I doing wrong, the connect script seems right but obviously isn't.
Would appreciate any help.
Have you tried to delete space before $options?

How to connect to remote mongodb with php?

Here is the php code that I'm working with in my local machine:
$m = new Mongo();
$db=$m->selectDB("def");
//then all in my code i use $db to select insert ... (as defined in php doc)
Now I want to connect my application to a remote server (hosted by mongood.com)
How can I do this?
You can use mongoOd without the REST API
But remember, it's a replica Set cluster so You need to configure your PHP for a ReplicaSet configuration...
I use mongoOd within ruby & mongoid (not the REST API)
Here a php example
<?php
// connecting to mongood.com cluster
$m = new Mongo("mongodb://94.23.54.103:27017,188.165.219.99:27017,94.23.220.151:27017", array("replicaSet" => "cluster"));
var_dump($m);
$db = $m->selectDB('my_database');
$db->authenticate("my_login", "my_password");
$collection = new MongoCollection($db, 'my_collection');
$cursor = $collection->find();
foreach ($cursor as $doc) { var_dump($doc); }
?>
Enjoy :)
A mongoOd Team member
The constructor for the mongo object takes as its arguments connection parameters.
http://www.php.net/manual/en/mongo.construct.php
$m = new Mongo('mongodb://[username:password]#host:port')
You'll have to ask them what the connection URI is, and then use:
$m = new Mongo("mongodb://username:password#hostname");
However, I am not sure if that option is available to you. Their website says you can access data via a REST API.
At any rate, you should ask them for help. There's a button on the left that reads "aide," if you click on it you'll get a form where you can fill in your email and your question.
Reference: Mongo - Connecting

Categories