PDO Cannot connect to MySQL DB - php

I am using Yii as my PHP framework. I'm trying to use Gii to generate a model, the problem is I can't event get to the page to generate models because of this error:
PDO::__construct() [<a href='pdo.--construct'>pdo.--construct</a>]: [2002] No such file or directory (trying to connect via unix:///tmp/mysqld.sock)
My php.ini looks like:
upload_max_filesize = 12M
post_max_size = 12M
mysql:host=localhost;port=3306
mysql:unix_socket=/tmp/mysqld.sock
pdo_mysql.default_socket=/tmp/mysqld.sock
mysql.sock does exist in /tmp/ (installed from MySQL package via MySQL.com). MySQL is running, I'm on Mac OS X 10.6. The root password is not set.
Any ideas?

Related

My computer has two php folders so I either can't connect to SQL Server or install Laravel

I spent two days setting up my PHP connection to SQL Server.
I downloaded the drivers, added them to the C:\Program Files\php\ext folder (as indicated by phpinfo()). Then I modified the php.ini file to add those extensions. Then I went to services and restarted "SQL Server EXPRESS". Nothing worked until I found that PHP is also installed in another folder on my PC: C:\Program Files\iis express\PHP\v8.0. So I added the drivers there too and changed the php.ini file and restarted the SQL Server EXPRESS service (is that what they mean with restarting your web server?). The phpinfo() page said:
Configuration File (php.ini) Path: no value
Loaded Configuration File: C:\Program Files\iis express\PHP\v8.0\php.ini
and there was no section "sqlsserver". I gave up and the next day it magically worked. The phpinfo had a "sqlserver" section and the connection to my database no longer said "Call to undefined function sqlsrv_connect()".
Now, I had to download Laravel for my course, and install Composer. When doing so, it was unable to install when I had C:\Program Files\iis express\PHP\ as my PATH and it asked me if I wanted to make C:\Program Files\php\ my PATH instead. So I clicked yes, and the installation was successful.
Laravel works, however now my database connections with SQL Server don't work anymore! I get the same old "Call to undefined function sqlsrv_connect()" error, and my phpinfo() now says
Configuration File (php.ini) Path:
Loaded Configuration File: C:\Program Files\php\php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed: (none)
and there is no longer a "sqlsserver" section on the page.
I have tried everything in StackOverflow articles, such as removing the semicolon in the php.ini extension, including the full address in the extensions in the php.ini file, including the extensions of the other php folder into this php.ini file, restarting SQL Server service, etc. But nothing works. I also don't know how to reset the old PHP folder back into the PATH variable.
My connection code is:
$con = sqlsrv_connect( 'localhost' ,
[ 'Database' => 'sample_db' ,
'UID' => 'sample_user' ,
'PWD' => 'sample_password' ]
);
//Print error message if the database doesn't connect
if ($con === false ){
echo 'Failed to connect to db: ' . sqlsrv_errors()[ 0 ][ 'message' ];
exit ();
}
How can I have these two php folder on my computer both work, the one for my PHP SQL Server database connection, and the other for my Laravel programme to run? Has anyone else had this issue and I so, what worked for you?
I managed to resolve this by changing the PATH back to the php folder instead of the iis/php folder. I was worried that would break my Laravel now, but it didn't.

vtiger Call to a member function Execute() on null in C:\xampp\htdocs\vtigercrm\include\database\PearDatabase

I am installing vtiger,
when i open the index page,
i got this error:
Fatal error: Call to a member function Execute() on null in C:\xampp\htdocs\vtigercrm\include\database\PearDatabase.php on line 357
i opened the PearDatabase.php file, and I found this:
if($this->avoidPreparedSql || empty($params)) {
$sql = $this->convert2Sql($sql, $params);
$result = $this->database->Execute($sql);
} else {
$result = $this->database->Execute($sql, $params);
}
the line 357 is:
$result = $this->database->Execute($sql);
If you have installed vtiger once locally, you have to clean cookies in your brower of your local vtiger domain.
That should fix your problem, what a amazing bug it is!
Make sure you have installed all the pre-requisites:
Pre-requisites from here:
Apache 2.1+
MySQL 5.1+ (default storage engine = InnoDB)
PHP 5.2+, 5.3
php-imap
php-curl
php-xml
max_memory (min. 256MB)
max_execution_time (min. 60 seconds)
error_reporting (E_ALL & ~E_NOTICE & ~E_DEPRECATED)
Hardware: 4 GB RAM, 250 GB Disk (for file attachments)
The error is suggesting that it can't work out what to do with the back end database.
You will receive this error when installing vTiger if your session_save_path() is not writable by your web server user.
In my case, my 'session_save_path' was /var/lib/php/7.1/session and was owned by root. I am using Nginx so I executed the following command to resolve my issue:
sudo chown -R nginx:nginx /var/lib/php/7.1/session
If you are using Apache, you would execute the following command to resolve your issue:
sudo chown -R www-data:www-data /var/lib/php/7.1/session
The information entered into the vTiger wizard is saved to $_SESSION as you navigate through the installation steps.
When the 'session_save_path' is owned by root rather than the web server user, the session data is not saved between function Step5() and function Step6() in modules/Install/views/Index.php. So when the config.inc.php file is created by the wizard, all the configuration data you entered into the form is not written to config.inc.php since your data was not saved in $_SESSION between the requests. This can be fixed by changing the permissions on your 'session_save_path' to be writable by the web server user.
You can find your session.save_path in your php.ini file or your www.conf file if you are using php-fpm:
/etc/php-fpm-7.1.d/www.conf:php_value[session.save_path] = /var/lib/php/7.1/session
This unhelpful error is actually because your database connection is unsuccessful in file include/database/PearDatabase.php in function connect() since all the database variables are empty.
Hope this helps.

mysqli_connect() error with PHP's built-in web server

I compiled PHP with mysqlnd using the prescriptions from Mysqlnd installation. PHP works fine. php -m | grep mysql shows that neccessary MySQL modules are actually installed. Then I run php -S localhost:8088 -t <dir>. In PHP code function_exists('mysqli_connect') does return true. But when I run mysqli_connect() warning mysqli_connect(): (HY000/2002): No such file or directory arises. In /etc/php.ini I set default socket to /tmp/mysql.sock then to /var/lib/mysql/mysql.sock. Both variants didn't work. How can I enable mysql support in PHP-built-in server?
Have you tried those solutions ?
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///tmp/mysql.sock) in
First try to use 127.0.0.1 instead of "localhost" in your mysqli_connect call.
Then make sure there is sockets where it should : make a symbolic link from /tmp/mysql.sock to /var/mysql/mysql.sock (or other way depending what exists on your computer)
Finally try to read all answers listed in the link above, maybe one of them will hel you

CakePHP 2.4.7 / AclExtras / aco_sync no database connection

When I execute this command in my app path "./Console/cake acl_extras.acl_extras aco_sync" and I get the error, that this output:
Welcome to CakePHP v2.4.7 Console
---------------------------------------------------------------
App : app
Path: l:\server\htdocs\test\app\
---------------------------------------------------------------
Error: Database connection "Mysql" is missing, or could not be created.
#0 L:\server\htdocs\test\lib\Cake\Model\Datasource\DboSource.php(260): Mysql
->connect()
but the database connection is working
A bit late, but Cake's console was using a different php.ini. In that php.ini, the extension php_pdo_mysql.dll was commented out.
Once the extension was no longer commented out, the error disappeared.
Update
This was based on a WAMP installation. If you run a PHP script with only phpinfo(), you should find the following row:
Loaded Configuration File
In my case, it displays: C:\wamp\bin\apache\apache2.4.9\bin\php.ini. This was the ini file to modify.

Will I need to recompile/install php to add the mssql extension?

I have a system that is running php, and I recently needed to add connectivity to an MSSQL database. I have FreeTDS and UnixODBC installed/configured correctly, and I can make successful queries in python, and via utilities like tsql and isql. After looking at phpinfo() I've discovered I don't have a 'sqlsrv' section, and there is no mssql.so file in my php extensions directory.
I want to add this to my system without having to recompile/install php. Would I be able to find and download the mssql.so file, put it into my extensions directory, add extension=/path/to/mssql.so to my php.ini file, and reload apache to get this working? Or is there more steps I would need to take?
EDIT:
The system is running SLES11 with PHP 5.2
EDIT 2:
I've managed to get the php5-mssql extension installed. I grabbed the source, extracted it, and copies these files:
ext/mssql/config.m4
ext/mssql/php_mssql.c
ext/mssql/php_mssql.h
Then, in the directory where I copied the files to, I ran phpize (you will need to install php5-devel to get this tool), and compiled the extension like so:
./configure --with-mssql=/usr/local/freetds
make
I also had to add a line and comment out a line in php_mssql.c before it could actually compile correctly (not everyone will need to do this):
{NULL,NULL,NULL}
/*PHP_FE_END*/
This created the mssql.so file in /php_mssql/modules/ (relative to where I compiled the code), which I was able to move to my extensions directory (you can find this with php -i | grep extensions). I added extension=mssql.so to my php.ini file; however, there is still no 'sqlsrv section in phpinfo().
Some connection methods seem to partially work:
When running the following code from a shell, <h1>Connection Success</h1> is shown; but when opened in a browser, nothing after the mssql_connect line is shown:
<?php
//*************************************************************************
//Open Database Connection
//*************************************************************************
//phpinfo();
$dbserver="MyServer";
$dbusername="user";
$dbpassword="pass";
$defaultdb="DBName";
$cn = mssql_connect($dbserver,$dbusername,$dbpassword) or die("Connection Error");
$db = mssql_select_db($defaultdb,$cn) or die("Database Error");
echo "<h1>Connection Success</h1>";
?>
So it looks like I'm partially getting a connection that way? When I try with a PDO object, I get another error:
Code:
<?php
$con = new PDO('odbc:host=MyServer;dbname=DBName','user','pass');
?>
Error:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[IM002] SQLDriverConnect: 0 [unixODBC][Driver Manager]Data source name not found, and no default driver specified' in /path/to/php/file/test3.php:3
Stack trace:
#0 /path/to/php/file/test3.php(3): PDO->__construct('odbc:host=MySer...', 'user', 'pass')
#1 {main}
thrown in /path/to/php/file/test3.php on line 3
I've also tried the following (assuming that the PDO statement/DSN in the previous code was incorrrect):
<?php
try {
$db = new PDO("odbc:Driver=FreeTDS; Server=MyServer; Port=1433; Database=DBName; UID=user; PWD=pass;");
} catch (PDOException $exception) {
die("$exception");
}
echo "<h1>Success!</h1>";
?>
This showed <h1>Success!</h1> from the shell, but showed the following error in my web browser:
exception 'PDOException' with message 'SQLSTATE[08001] SQLDriverConnect: 0 [unixODBC][FreeTDS][SQL Server]Unable to connect to data source' in /path/to/php/file/test4.php:3 Stack trace: #0 /path/to/php/file/test4.php(3): PDO->__construct('odbc:Driver=Fre...') #1 {main}
In ODBC the error message contains elements in [] at the start of the message and the rightmost one is the part of the chain reporting the error (see ODBC Diagnostics & Error Status Codes. So, "[unixODBC][Driver Manager]Data source name not found, and no default driver specified" was reported by unixODBC. What unixODBC is saying is the string passed to the ODBC API SQLConnect or SQLDriverConnect does not identify a DSN (data source name) or an ODBC driver and there is no default DSN defined. You can find where your data sources are defined by running odbcinst -j e.g.,
$ odbcinst -j
unixODBC 2.2.14
DRIVERS............: /etc/odbcinst.ini
SYSTEM DATA SOURCES: /etc/odbc.ini
FILE DATA SOURCES..: /etc/ODBCDataSources
USER DATA SOURCES..: /home/martin/.odbc.ini
SQLULEN Size.......: 4
SQLLEN Size........: 4
SQLSETPOSIROW Size.: 2
Here, drivers are defined in /etc/odbcinst.ini, system data sources in /etc/odbc.ini and user data sources in /home/martin/.odbc.ini. As you'll probably be running PHP probably under a web server I'd stick to using the system data sources if I were you. You can list your system data sources with odbcinst -q -l -s. You find a a very good explanation of Linux/ODBC at Linux/UNIX ODBC.
Your second error "[unixODBC][FreeTDS][SQL Server]Unable to connect to data source" is reported by the SQL Server driver from FreeTDS so in this case you must have passed sufficient information to unixODBC to at least allow it to identify the driver, load it and call SQLConnect/SQLDriverConnect in it. You can see what was passed to unixODBC's SQLConnect/SQLDriverConnect by enabling tracing in unixODBC. You enable tracing of unixODBC by editing your odbcinst.ini file (locate with odbcinst -j command above) and adding the following to the top of it:
[ODBC]
Trace = yes
TraceFile = /tmp/unixodbc.log
Now, when you run your php example it will log to /tmp/unixodbc.log all ODBC API calls and the one you are looking for is SQLConnect or SQLDriverConnect. e.g., when I connect to a DSN called mydsn with a username and password of XXX and YYY I see:
[ODBC][31521][1374740062.012973][SQLDriverConnect.c][687]
Entry:
Connection = 0x9d7d430
Window Hdl = (nil)
Str In = [DSN=mydsn;UID=XXX;PWD=********][length = 29]
Str Out = 0xbfdeb83c
Str Out Max = 512
Str Out Ptr = 0xbfdeb638
Completion = 0
UNICODE Using encoding ASCII 'ISO8859-1' and UNICODE 'UCS-2LE'
DIAG [01000] [Easysoft][SQL Server Driver][SQL Server]Changed database context to 'master'.
DIAG [01000] [Easysoft][SQL Server Driver][SQL Server]Changed language setting to us_english.
Note, this connection was successful and it clearly shows part of the connection string was DSN=mydsn and mydsn exists as a DSN in my /etc/odbcinst.ini.
isql can work differently to some ODBC enabled applications as isql calls the ODBC API SQLConnect whereas most ODBC applications these days are ODBC 3 aware and use SQLDriverConnect. The main difference is SQLConnect is only given 3 arguments, a DSN name, a username and a password where SQLDriverConnect is given a single string of attribute/value pairs defining the connection. I only tell you this so you are aware how it is possible for isql to work and something else not to.
However, in your second case when you examine your trace you'll see unixODBC got something useful enough to identify the driver, load it and call freeTDS's ODBC driver and the error "Unable to connect to data source" is coming from freeTDS. So, I suggest your DSN is probably ok and your freetds.conf is incorrect in some way. As I don't use freeTDS myself I'm not sure but I've heard you can use ODBC with freeTDS without any reference to the freetds.conf file and switches based on whether you use Server or ServerName. I'm sure there are loads of examples on the freeTDS web site.
Here's how I connect to MS SQL servers from a LAMP (Ubuntu) stack:
/etc/odbc.ini
# Define a connection to a Microsoft SQL server
# The Description can be whatever we want it to be.
# The Driver value must match what we have defined in /etc/odbcinst.ini
# The Database name must be the name of the database this connection will connect to.
# The ServerName is the name we defined in /etc/freetds/freetds.conf
# The TDS_Version should match what we defined in /etc/freetds/freetds.conf
[mssql]
Description = MSSQL Server
Driver = freetds
Database = XXXXXX
ServerName = MSSQL
TDS_Version = 8.0
/etc/odbcinst.ini
# Define where to find the driver for the Free TDS connections.
[freetds]
Description = MS SQL database access with Free TDS
Driver = /usr/lib/i386-linux-gnu/odbc/libtdsodbc.so
Setup = /usr/lib/i386-linux-gnu/odbc/libtdsS.so
UsageCount = 1
/etc/freetds/freetds.conf
# The basics for defining a DSN (Data Source Name)
# [data_source_name]
# host = <hostname or IP address>
# port = <port number to connect to - probably 1433>
# tds version = <TDS version to use - probably 8.0>
# Define a connection to the Microsoft SQL Server
[mssql]
host = XXXXXX
port = 1433
tds version = 8.0
And here's the PHP code:
$con = new PDO('dblib:host=mssql;dbname=MyDB', 'domain\username', 'password');
You may need to tweak things a bit for your OS. To install the necessary software on Ubuntu I did something like this:
sudo apt-get install php5-odbc php5-sybase tdsodbc
Use PDO and instal this http://www.php.net/manual/en/ref.pdo-sqlsrv.php
I always use PDO it can easy do all the database interaction you need with different db drivers and the same php code. Except the query languages which is sometimes a little different.
For MSSQL you only need to add the drivers just paste the .dll's, add the entry to the conf.ini and restart apache.

Categories