gridfs mongodb and php script - php

I have a php script who inserts documents in gridfs.
in this DB i have 1346214 document.
when i run this script for adding 10500 document i have this error
PHP Fatal error: Uncaught exception 'MongoConnectionException' with message 'Failed to connect to: 127.0.0.1:27017: Cannot assign requested address' in /root/upload.php:5
Stack trace:
#0 /root/upload.php(5): Mongo->__construct('127.0.0.1:27017')
#1 {main}
thrown in /root/upload.php on line 5
in mongod.log i have this message
2014-05-08T12:24:13.537+0200 [initandlisten] connection accepted from 127.0.0.1:57221 #93806 (63 connections now open)
2014-05-08T12:24:13.538+0200 [conn93806] authenticate db: uniform_server { authenticate: 1, user: "user", nonce: "xxx", key: "xxx" }
2014-05-08T12:24:13.538+0200 [conn93806] Failed to authenticate user#uniform_server with mechanism MONGODB-CR: AuthenticationFailed UserNotFound Could not find user user#uniform_server
2014-05-08T12:24:13.538+0200 [conn93806] end connection 127.0.0.1:57221 (62 connections now open)
have you any idea about this problem?
Thanks

This is the content of the php script and i run it 10500 via this command for i in {1..10500..1}; do php upload.php; done
<?php
$conn = new Mongo("127.0.0.1:27017"); // Connect
$db = $conn->uniform_server; // Select DB
$db->authenticate("user","passwd"); // Authenticate to MongoDB
$grid = $db->getGridFS(); // Initialize GridFS
$tab_pj = array("document0.pdf",
"document1.tif",
"document2.doc",
"document3.png",
"document4.JPG",
"document5.xls",
"document6.rar",
"document7.ppt",
"document8.rtf"
);
$pj_id = rand(0,8);
$name = $tab_pj[0]; // Get Uploaded file name
$id = $grid->storeFile($name); // Store uploaded file to GridFS
$conn->close(); // Close connection
exit(0);
?>

Related

Print receipe with php

This is my scenario:
I have a web server running php
I have a client with a receipt printer
I have a HTML form that retrieves various data from the user and stores them in a database. Then I can access it as I want.
What I want to do is that when I click on a print button, the server sends a command to the printer to print the ticket.
I'm trying to do this with Mike32's escpos-php library but I can't do it.
Here is the php file I made
<?php
require 'vendor/autoload.php';
use Mike42\Escpos\Printer;
use Mike42\Escpos\PrintConnectors\WindowsPrintConnector;
$data = "Hello world !";
$printerName = 'Custom';
$clientName = 'LAPTOP-J3MTKIN3';
$connector = new WindowsPrintConnector("\\\\$clientName\\$printerName");
try {
$printer = new Printer($connector);
$printer->text($data);
$printer->cut();
$printer->close();
} catch (Exception $e) {
echo "Erreur : " . $e->getMessage() . "\n";
}
?>
Here are the 2 errors I get:
Warning: preg_match(): Compilation failed: invalid range in character
class at offset 29 in
/var/www/html/vendor/mike42/escpos-php/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php
on line 126
and
Fatal error: Uncaught BadMethodCallException: Printer
'\LAPTOP-J3MTKIN3\Custom' is not a valid printer name. Use local port
(LPT1, COM1, etc) or smb://computer/printer notation. in
/var/www/html/vendor/mike42/escpos-php/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php:155
Stack trace: #0 /var/www/html/test_impression.php(22):
Mike42\Escpos\PrintConnectors\WindowsPrintConnector->__construct() #1
{main} thrown in
/var/www/html/vendor/mike42/escpos-php/src/Mike42/Escpos/PrintConnectors/WindowsPrintConnector.php
on line 155

PHP JasperReports Server API Error

I am trying to ultimately run and display reports from a remote Jasper Server in a PHP application. What I am trying to do this with is the jrs-rest-php-client project on github.
The error:
Fatal error: Uncaught exception 'Jaspersoft\Exception\RESTRequestException' with message 'An unexpected HTTP status code was returned by the server' in C:\xampp\htdocs\jrs\vendor\src\Jaspersoft\Tool\RESTRequest.php:409
Stack trace:
#0 C:\xampp\htdocs\jrs\vendor\src\Jaspersoft\Tool\RESTRequest.php(479): Jaspersoft\Tool\RESTRequest->handleError(0, Array, false)
#1 C:\xampp\htdocs\jrs\vendor\src\Jaspersoft\Service\ReportService.php(40): Jaspersoft\Tool\RESTRequest->prepAndSend('https://jasper....', Array, 'GET', NULL, true)
#2 C:\xampp\htdocs\jrs\report.php(30): Jaspersoft\Service\ReportService->runReport('/Reports/Distri...', 'html')
#3 {main} thrown in C:\xampp\htdocs\jrs\vendor\src\Jaspersoft\Tool\RESTRequest.php on line 409
My PHP:
require_once __DIR__ . "/vendor/autoload.php";
use Jaspersoft\Client\Client;
$d = new Client(
"http://jasper.server.com/jasperserver-pro",
"username",
"password",
"organization"
);
$info = $d->serverInfo();
Any ideas?
Looking into the code of RESTRequest.php there are two cases in which this exception is thrown:
A JSON result set with an unknown error code is returned (unknown meaning different from 200 OK)
No JSON result set is returned at all
So I suspect the connection isn't working properly. To find out more, you should catch the exception in your code and evaluate it:
It could look something like this (I'm more familiar with Java):
try {
$d = new Client(
"http://jasper.server.com/jasperserver-pro",
"username",
"password",
"organization"
);
$info = $d->serverInfo();
} catch (RESTRequestException $e) {
echo 'RESTRequestException:';
echo 'Exception message: ', $e->getMessage(), "\n";
echo 'Set parameters: ', $e->parameters, "\n";
echo 'Expected status code:', $e->expectedStatusCodes, "\n";
echo 'Error code: ', $e->errorCode, "\n";
}
If there is still an error, you can check the following:
Can you reach the jasper server from the server where this code is deployed? Sometimes e.g. firewall settings can interfere.
Is the organization called exactly like defined in the properties of the organization in the server? (Open repository / right click on organization / properties / resource-id)

Error trying to create SQLite database with PHP on Windows

I'm following the SQLite tutorial on this page http://www.tutorialspoint.com/sqlite/sqlite_php.htm
But I'm getting an error when I run this code
<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open('test.db');
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo "Opened database successfully\n";
}
?>
I get this error
Fatal error: Uncaught exception 'Exception' with message 'Unable to open
database: unable to open database file' in C:\inetpub\wwwroot\cdb.php:6
Stack trace:
#0 C:\inetpub\wwwroot\cdb.php(6): SQLite3->open('test.db')
#1 C:\inetpub\wwwroot\cdb.php(9): MyDB->__construct()
#2 {main} thrown in C:\inetpub\wwwroot\cdb.php on line 6
So the error is that the program is not able to create the database but I'm on the inetpub/wwwroot folder and IIS has permissions over it and the turorial doesn't mention anything about permission problems. Any idea why it happens?

When I type in connect() to my database, my localhost server comes up blank

Is the connect() function depecrated or something? When I try to connect to a mysql database using that function, my page stops working?
i'm doing jeffrey ways 30daysjquery course lesson 25
here's the code in my index.php file
<?php
require 'functions.php'
if ( isset($_POST['q'])) {
connect();
}
include 'views/index.tmpl.php'
?>
This is my code in my functions.php file
<?php
function connect(){
global $pdo; // set it as global so other functions can access it
$pdo = new PDO("mysql:host=localhost;dbname=sakila", "root", "root");
}
function get_actors (){
}
?>
Might it be that I do not have access to the content that's in functions.php? if it is how do I create access?
Your function should maybe return a connection:
function connect(){
$pdo = new PDO("mysql:host=localhost;dbname=sakila", "root", "root");
return $pdo;
}
Then, your function get_actors() could be
$db_connection = connect();
get_actors( $db_connection );
function get_actors( $pdo ) {
//
// You use the database connection $pdo to get actors
//
}
You have to add a parameter to your function get_actors. No need to modify the code into the function.
When I checked MAMP this is the information I have if I want to connect to a MySQL server using a script.
Host localhost
Port 8889
User root
Password root
Socket /Applications/MAMP/tmp/mysql/mysql.sock
thrown in /Applications/MAMP/htdocs/lesson-25/functions.php on line 5
[09-Nov-2014 23:01:47 Europe/Berlin] PHP Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in /Applications/MAMP/htdocs/lesson-25/functions.php:5
Stack trace:
#0 /Applications/MAMP/htdocs/lesson-25/functions.php(5): PDO->__construct('mysql:host=loca...', 'root', 'root')
#1 /Applications/MAMP/htdocs/lesson-25/functions.php(9): connect()
#2 /Applications/MAMP/htdocs/lesson-25/index.php(3): require('/Applications/M...')
#3 {main}

PHP Fatal error: Call to undefined method MongoDB::find() when try to use collection

I have followed structure of mongoDB:
DB name: bios
user: "fess"
password: "boo"
IP: 1.2.3.4
collection name: bios2
From MongoVUE:
This is my snippets of PHP code:
$db = new Mongo('mongodb://fess:boo#1.2.3.4/bios');
// if i remove "bios" it asks default db name
$collection_bios2 = $db->bios2;
$cursor_bios2 = $collection_bios2->find(); // <- here I get error
// PHP Fatal error: Call to undefined method MongoDB::find()
...
$db->close();
Why I get this error?
I saw other examples and seems that $collection_bios2 should be collection.
Thanks
Do you select db? such as
$mongo = new Mongo();
// the following two lines are equivalent
$db = $mongo->selectDB("foo");
$db = $mongo->foo;
Found the problem. In order to create connection only, 1st off I need create user/pswd for admin:
Through mongoDB CLI:
> use admin
> use bios
> db.createUser("fess", "boo");
Actually I had user only for bios DB
Otherwise it will drop me error:
Error connecting to MongoDB server Failed to connect to: 1.2.3.4:27017: Authentication failed on database 'admin' with username 'fess': auth fails
PHP
// here we create just client (for admin aka root)
$m = new MongoClient('mongodb://fess:boo#1.2.3.4');
// and switch to "bios"
$db = $m->selectDB("bios");
$list = $db->listCollections();
foreach ($list as $collection) {
Log::Debug(get_class() . " -> testMongoDB", "feederliteRC/U", array("Output" => "collection: $collection"));
}
//$db = new Mongo('mongodb://max:bagabu#172.20.0.23/bios');
//$collection_bios = $db->bios;
$collection_bios2 = $db->bios2;
Output
"collection: bios.bios2"
For me, it is fine as below. I used "mongodb/mongodb" with composer from packagist. "composer require mongodb/mongodb".
require(__DIR__."/vendor/autoload.php");
use MongoDB\Client as MGDClient;
$collection = (new MGDClient(username:password#127.0.0.1:27017/db_name))->db_name->collection_name;
$cursor = $collection->find([],['limit'=>5]);
var_dump($cursor);

Categories