How to install locally an extension in SQLite on a Mac? - php

I've no real knowledge about installing an extension. I'm stuck because I cannot use SQRT in SQLite.
I'm using Laravel on a local machine and I'm working on my PHPunit Tests. In one Test I'm using a query with some heavy math functions.
I get the error:
Caused by
PDOException: SQLSTATE[HY000]: General error: 1 no such function: SQRT
I found out that SQLite has an extension called: extension-functions.c
I downloaded it, but I'm not sure if I did everything correctly (obviously not because it does not work):
I downloaded it and used this in my terminal
gcc -fno-common -dynamiclib extension-functions.c -o libsqlitefunctions.dylib
It compiled this data: libsqlitefunctions.dylib
With php --ini if found that my php.ini is located in /usr/local/etc/php/7.1/php.ini
Now I looked up for sqlite3 and found this commented ;sqlite3.extension_dir =
I changed it to
sqlite3.extension_dir =/usr/local/etc/php/7.1/extension/libsqlitefunctions.dylib
and created the extension folder and put the dylib file inside
I restarted my apache with sudo apachectl restart
And tried my Query and still, it fails. What did I do wrong?
For future readers
To use it in Laravel you call it like this:
$pdo = DB::connection()->getPdo();
$pdo->sqliteCreateFunction('SQRT', 'sqrt', 1);
$pdo->sqliteCreateFunction('ASIN', 'asin', 1);
$pdo->sqliteCreateFunction('SIN', 'sin', 1);
$pdo->sqliteCreateFunction('COS', 'cos', 1);
$pdo->sqliteCreateFunction('POWER', 'pow', 2);
$pdo->sqliteCreateFunction('pi', 'pi', 0);

It seems that you want to use this with PDO. The problem is that loading extensions only works with the SQLite3 api directly, using the SQLite3::loadExtension method.
The PDO driver does not allow the loading of extensions (see this wiki entry regarding LoadableExtensions under "Security Considerations").
So you could either swith to the SQLite3 api directly or you could use "user defined functions" with PDO. For example if you need the SQRT function:
$db = new PDO('sqlite:testdb.sqlite', null, null, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);
$db->sqliteCreateFunction('SQRT', 'sqrt', 1);
$stmt = $db->query("SELECT sqrt(25)");
foreach ($stmt as $row) {
echo $row[0], PHP_EOL; // output: 5.0
}
Most of the math-functions your extension provides map 1:1 to the PHP function names (like sqrt), so it should be easy to create the functions you need.

It seems you are pointing the sqlite3.extension_dir property at a file instead of a directory. According to the manual for sqlite3, the extension_dir property is:
[The] Path to the directory where the loadable extensions for SQLite reside.
I think it may work if you change
sqlite3.extension_dir = /usr/local/etc/php/7.1/extension/libsqlitefunctions.dylib
to
sqlite3.extension_dir = /usr/local/etc/php/7.1/extension
However, I would create a separate directory for the SQLite3 extensions so they are not mixed with the PHP extensions; I say this since it seems like you are currently using the PHP extension directory for the sqlite3.extension_dir property. (If you aren't, then please disregard this note.) I assume that SQLite3 just scans the configured directory for loadable modules and loads them using something like dlopen. This might conflict if it tries to load a PHP extension. (Who knows, it may gracefully handle this.)

Related

Use odbc eloquent outside laravel PHP7

I am using eloquent outside laravel. I have my own PHP application.
This is what i am using https://github.com/illuminate/database
My config is as following
$settings3 = array(
'driver' => 'odbc',
'dsn' => "Driver={SQL Server};Server={serverName};Trusted_Connection=true;Database=telesur_mis;",
'username' => 'user',
'password' => 'user',);
$capsule->addConnection($settings3,'teleappframework');
After executing this code I am getting the following error
Fatal error: Uncaught InvalidArgumentException: Unsupported driver [odbc]
I have PDO ODBC installed, i have also tested pdo odbc connection outside of eloquent.
The reason for using ODBC is, because i am using PHP7 and currently there is no PDO extension for SQL Server.
Can anyone help me on this?
i would download FreeTDS and just use the regular sqlsrvr driver it works like a charm (if you are on a unix env) there is a lot of posts on how to configure it etc just look it up.
PHP7 has a few modules disabled by default that were previously enabled in PHP5.
Most likely running php -m does not show the modules you need.
It's an easy fix though since the extension should already exist in the \ext\ folder that came with PHP7. You just need to modify your php.ini file to include the line:
extension=php_pdo_odbc.dll
extension=php_odbc.dll
Then from a command prompt or terminal test again using php -m to see that the module is now listed.

PECL_HTTP not recognised php ubuntu

I am trying to use the HttpRequest class which should be in the PECL_HTTP extension of php.
I have php5 and used the following to install pecl_http.
sudo pecl install pecl_http
pecl/pecl_http is already installed and is the same as the released version 2.0.1
install failed
After that i enter into my php.ini:
[PHP]
extension=http.so
[...]
Then i restart my apache2 server, and try using the HttpRequest class, but it gives me the following error:
PHP Fatal error: Class 'HttpRequest' not found
What are possible error's i might have missed?
UPDATE: (Changed the title)
The Extension is not shown in phpinfo(), i set
extension=http.so
And checked if the http.so file is in my extension_dir. I really don't know how to make php recognise the extension.
UPDATE 2:
I managed to install extension, but the class still does not exist.
For others, i had to reference the other extensions pecl_http needs. (For me: propro.so, raphfr.so)
UPDATE 3:
I did not manage to make the Class visible, the answers below show some approaches with other classes.
I solved this issue by using CURL.
You have installed the new version of the extension which uses a completely different API. I still don't know how it works, but I will update my answer one I know. The Documentation of the new version is to be found at http://devel-m6w6.rhcloud.com/mdref/http.
To Install the old version, first uninstall the new verion and then execute
pecl install http://pecl.php.net/get/pecl_http-1.7.6.tgz
UPDATE: here are two examples from the documentation, both should work well:
one request (can be found at http://devel-m6w6.rhcloud.com/mdref/http/Client/enqueue):
<?php
(new http\Client)->enqueue(new http\Client\Request("GET", "http://php.net"),
function(http\Client\Response $res) {
printf("%s returned %d\n", $res->getTransferInfo("effective_url"), $res->getResponseCode());
return true; // dequeue
})->send();
multiple requests (can be found at http://devel-m6w6.rhcloud.com/mdref/http/Client/once):
<?php
$client = new http\Client;
$client->enqueue(new http\Client\Request("HEAD", "http://php.net"));
$client->enqueue(new http\Client\Request("HEAD", "http://pecl.php.net"));
$client->enqueue(new http\Client\Request("HEAD", "http://pear.php.net"));
printf("Transfers ongoing");
while ($client->once()) {
// do something else here while the network transfers are busy
printf(".");
// and then call http\Client::wait() to wait for new input
$client->wait();
}
printf("\n");
while ($res = $client->getResponse()) {
printf("%s returned %d\n", $res->getTransferInfo("effective_url"),
$res->getResponseCode());
}
In both examples you can use $res->getBody() to return the body of the response.
I couldn't test these examples but I have heard of other people that they work.
I had the same problem and solved it by arrange the order in wich extensions are loaded in php.ini
All my other extensions was loaded using their own .ini files in /etc/php5/mods_available.
In my apache2 error.log I noticed that json_decode was needed by http.so to load.
I created a file for http (http.ini) and a symlink in /etc/php5/apache/conf.d with a higher prefix than json.
my http.ini
; configuration for php http module
; priority=50
extension=raphf.so
extension=propro.so
extension=http.so
use the new pecl_http.2.0.6 like this
new http\Message();
or extend in your own class as extends http\Message

Doctrine Cli on Windows

I am having some trouble configuring doctrine orm on windows 8, php 5.4. I have installed Doctrine using Composer.
I have followed the docs to the letter but when I run any commands, php vendor/bin/doctrine orm:schema-tool:create for example, my command line just outputs
SRC_DIR="`pwd`"
cd "`dirname "$0"`"
cd "../doctrine/orm/bin"
BIN_TARGET="`pwd`/doctrine"
cd "$SRC_DIR"
"$BIN_TARGET" "$#"
I have also tried php vendor/bin/doctrine.php .... but it just prints out the above.
I have followed Doctrine's guide to the letter. Has anyone seen this before and if so, can you suggest anything?
i found a solution
there are also a bin folder in vendor/doctrine/orm/bin/ you can use this one like this
php vendor/doctrine/orm/bin/doctrine orm:schema-tool:create
make sure you have root folder and a cli-config.php file is present in root folder.
below is location where i found a solution
https://groups.google.com/forum/#!msg/doctrine-user/_ph183Kh-5o/_P_coljB-dcJ
this is working fine for me .
I had the same problem. The following solution worked for me:
"vendor/bin/doctrine.bat" orm:schema-tool:create
So, basically you:
use the ".bat" file provided by Doctrine and
enclose the call to that ".bat" file in quotes.
My Environment
Windows 7 Professional (x64)
PHP 5.5.12
Doctrine ORM 2.4.4
Do not write "php..." (it will write the file content)
Just "vendor\bin\doctrine orm:schema-tool:create" do the job (from the project root eg. c:\php\theProject).
Next, you will need a "cli-config.php" in the project root...
For Window version use backward slash "\"
vendor\bin\doctrine orm:schema-tool:create
not forward slash "/"
vendor/bin/doctrine orm:schema-tool:create
You can either install something like git bash or simply use the PHP version of the script:
php vendor\bin\doctrine.php orm:info
Obviously, the php binary directory should be in your PATH environment variable, otherwise, it's something like:
C:\path\to\php.exe vendor\bin\doctrine.php orm:info
Copy doctrine.bat (located at vendor/bin/doctrine.bat) to your project root directory
Create a bootstrap.php file in any path inside your project root directory with the following content:
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("../model");
$isDevMode = false;
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'user' => 'root',
'password' => '',
'dbname' => 'angular_php',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);
Create a cli-config.php file in your project root directory with the following content:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
// replace with file to your own project bootstrap
require_once 'path/to/file/bootstrap.php';
return ConsoleRunner::createHelperSet($entityManager);
Execute from a command line window (CMD):
c:\path\to\project\root\directory>doctrine --help
It's done!
I found it wasnt returning anything from the doctrine.php.bat. Turns out it was a PHP error in my cli-config.php file
Using Cygwin on Windows with doctrine installed via composer, was having the same problem
resolved by:
vendor/bin/doctrine.bat orm:convert-mapping
if you are still having issues you can run the cli script using php to get the console tools running:
for example
php cli-config.php orm:schema-tool:create
All the answers on this question are either outdated or outright incorrect. In my case, I observed, after installing the the library, the "\vendor\doctrine\orm\" folder was completely empty. The docs ask you to run vendor/bin/doctrine, which in turn attempts to call "vendor\doctrine\orm\bin\doctrine[.php]" (The php file extension is optional). After discovering this, I downloaded the library from the git repository and replaced the composer-installed version.
php vendor/doctrine/orm/bin/doctrine
then works fine.
Also, beware of the common misconception that the cli-config.php file must exist in the root of your project. It's fine to leave it in your config folder

Class 'PharData' not found

I get this error on my production server (CentOS 5.4 and php 5.3.5) :
Warning: include_once(PharData.php): failed to open stream: No such
file or directory in /var/www/ZendFramework/library/Zend/Loader.php on
line 146
Warning: include_once(): Failed opening 'PharData.php' for inclusion
(include_path='/var/www/fw:/var/www/vmms:/var/www/ZendFw/library:.:/usr/share/pear:/usr/share/php')
in /var/www/ZendFw/library/Zend/Loader.php on line 146
Fatal error: Class 'PharData' not found in
/var/www/vm/app/Backup.php on line 40
And this is the code which fail :
$phar = new PharData($imageBackupFile);
$phar->buildFromDirectory($imageDir);
Logger::info("Image directory backed up to: $imageBackupFile");
This code is working fine on my own computer.
PharData should be included by default in php 5.3+ ...
Thanks for your help!
UPDATE :
I am using the Zend Auto loader feature to load the good php files using this code :
require_once("Zend/Loader/Autoloader.php");
$autoloader = Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);
Zend autoloader is doing the include_once(PharData.php).
Just because Phar is bundled by default in PHP 5.3 doesn't mean that it's necessarily included in your install. When you build PHP with ./configure, you can pass the --disable-phar to disable the Phar extension.
To confirm this, run the following script:
<?php
phpinfo();
?>
One of the first sections to appear will be the Configure Command section. Review this section to see if the --disable-phar switch is present, and if there is a Phar section to the page in general.
If it's not present, you'll need to contact your host to have it enabled. There's a decent chance, however, that they won't do it for you since it could impact other users depending on how their servers are set up. If this is on your own machine, you'll need to either rebuild PHP without that switch, or install Phar manually from PECL (no idea if this would still work in 5.3, but I don't see why it wouldn't).

PHP Extension (memcache|memcached) not showing in phpinfo() but showing in php -m and php -i?

I am getting both modules listed as installed / configured when I use:
php -m
or if I use:
php -i
but when I use:
$m = new Memcache;
// or
$m = new Memcache();
// or
$m = new Memcached();
//or
$m = new Memcached;
I get the following error:
Fatal error: Class 'Memcached' not found
I am running on a Mac - OS X (10.5.7) with default install of apache & php. Additionally, I have memcached running as a daemon on 127.0.0.1:11211 and libmemcache as required by the php-memcached library. I have restarted apache tons of times and even done a machine restart.
Does anyone know why the modules/extensions show up in the command line but not in my phpinfo()? I am literally stumped, after 3 hours of googling, I am just about ready to give up.
Also, please note, my phpinfo() outputs my ini files as follows AND they are both the exact same file:
Configuration File (php.ini) Path: /etc
Loaded Configuration File: /private/etc/php.ini
UPDATE:
Apache is failing to load the extension.
[Fri May 14 04:22:26 2010] [warn]
Init: Session Cache is not configured
[hint: SSLSessionCache] PHP Warning:
PHP Startup: Unable to load dynamic
library
'/usr/lib/php/extensions/no-debug-non-zts-20060613/memcached.so'
- (null) in Unknown on line 0 PHP Warning: PHP Startup: Unable to load
dynamic library
'/usr/lib/php/extensions/no-debug-non-zts-20060613/memcache.so'
- (null) in Unknown on line 0
Does anyone know why or how this would happen? Both of the files referenced above DEFINITELY ARE there. Should I move this question to server fault?
Your webserver is probably using mod_php, which is a seperate bit of code from the standalone (CLI) interpreter. If they are both using the same ini file, and the memcache extension is configured in the ini file, then it sounds like for some reason, the mod_php is failing to load the extension - check your webserver error_log for startup errors.
It may be that the mod_php was compiled without memcache support (most extensions need to have a stub file linked into the php code, even though the bulk of the code is not linked until run time). Or it may be a permissions problem on the shared object file. Or your webserver may be running chroot, and unable to find the extension (which would also mean that although the ini files appear to have the same path, this is relative to different roots).
HTH
C.
because both versions use different php.ini
place your php.ini into location noted in the phpinfo() outout
I would suspect that the issue revolves around permissions. When you run php from comand line, it runs as the user invoking it. When run as an apache module, it runs as "nobody".
I would assume that the memcached.so file, or the directory it's in does not have proper permissions.
I stumpled upon this post and was having the exact same problem with an extension in my php -i but not in phpinfo(). Mine was a permissions problem because of selinux on a CentOS machine. I had to change ownership and permissions and now it is working as expected.
Set php path in environment variables as given below.
Right-click on "My Computer"
Properties
Advanced Tab > Environment Variables
Under System variables, scroll down to find "Path", select and click on Edit.
Add path to your php install on the end (make sure to precede with semi-colon ";"). Example: ";C:\php7"
Click Ok.

Categories