How do I configure MySQLi to recognize mysqli commands in PHP? - php

Currently, I'm trying to test connection to a database from one machine to another. One machine has MySQL installed on it and has a database set up and running, and I want to have another machine connect to it using mysqli in PHP. Both machines are Ubuntu, and I'm using PHP 5.6. I installed the mysql client on the machine I want to use to connect to the database, and when I try and connect to the database in php using the following code:
<?php
$mysqli = new mysqli('hostname', 'username', 'password', 'dbname');
if($mysqli->connect_errno) {
echo "Failed to connect to MySQL database.";
exit();
}
?>
it errors out, and shows a "This page isn't working" error page with a HTTP Error 500. I've tried the following:
sudo apt-get install php-mysqli (Got the message that it was installed and at standard version, I assume this is because I installed the mysql client)
phpenmod mysqli (This didn't change anything)
I'm not sure what else I should try. I believe I have PHP 5.6 and PHP 7.1 both installed, will this cause an issue? What steps can you recommend to troubleshoot and fix the issue of mysqli not working?

First, I would take a look at the error log on the machine you are trying to connect from. There should be some explanation for the 500 error.
Second, I believe you need to install the mysqlnd PHP extension on the machine you are connection from. The machine that is hosting the database is just doing that. So, you don't need to worry about having PHP install there.

Related

Installing on XAMPP PHP7.4.11 SQLSRV drivers [duplicate]

I trying to connect to an SQL Server in PHP. With XAMPP on my local machine, everything works well. But now I going to bring my application on the production server.
On this server there is installed the Microsoft IIS 6.1 and running the PHP version 7.0.7. I also installed the ODBC Driver from here. Next I decomment the following line in my php.ini file:
extension=php_sqlsrv_7_nts.dll
extension=php_pdo_sqlsrv_7_nts.dll
I got the files from the official microsoft site.
What's my problem?
Unfortunately, after I restarted the IIS. The PDO function throws the PDOException error with the following message:
could not find driver
For the connection I am using the following function which works pretty well on my local machine:
try {
$con = new PDO("sqlsrv:Server=" . SERVER . ";Database=" . DATABASE, USERNAME, PASSWORD);
// set the PDO error mode to exception
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "No connection: " . $e->getMessage();
exit;
}
What can I else do?
Here is detailed process if it's helpful for someone. PHP Version - 7.4
Download and extract the .dll files from this link - https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15
Paste the files in C:\xampp\php\ext, your path could be different.
in php.ini add those two lines at bottom or in extension section.
extension=php_sqlsrv_74_ts_x64.dll
extension=php_pdo_sqlsrv_74_ts_x64.dll
Restart your Xampp server, I'll suggest restart your computer and everything will work without an issue then.
Check if SqlSRV enabled
Check using phpinfo() or http://localhost/dashboard/phpinfo.php at like this -
Hope, it will help someone.
After I found the error log on the Windows Server, I solved the error by myself.
I got this error in my log:
[21-Apr-2017 07:12:14 UTC] PHP Warning: PHP Startup: Unable to load dynamic library '...\ext\php_pdo_sqlsrv_7_nts.dll' - %1 is not a valid Win32 application. in Unknown on line 0
Then I downloaded again the driver and installed the x64-Driver. Finally It works without any problems.
please notice you must use the correct version of php_sqlsrv_xx_xts_xxx.dll and php_pdo_sqlsrv_xx_xts_xx.dll files.
for exmple if you use php version 7.4 and a 64 bit system and wamp you must download and use these files:
php_sqlsrv_74_ts_x64.dll
php_pdo_sqlsrv_74_ts_x64.dll
for download you can use this site:
https://go.microsoft.com/fwlink/?linkid=2152937
https://learn.microsoft.com/en-us/sql/connect/php/download-drivers-php-sql-server?view=sql-server-ver15
It took some time for me to solve the 'No driver'-error. I went through some steps as mentioned here and found some other ones that helped me after new errors. For future references:
Download the latest drivers from Microsoft (as said by Julian Schmuckli).
Check if your XAMPP is 64 bits(!) with Phpinfo(). If you've got 32-bit, you need different drivers.
Add the drivers to your Php.ini file and save the dll's in your php/ext-folder (question of saber tabatabaee yazdi).
For the connection, use this code:
$dbh = new PDO ("sqlsrv:Server=$server;Database=$dbname",$username,$pw);
If you add a port, use:
$server = "192.168.1.15, 51022";
Where the IP (can be hostname to) is your server and 51022 your port.

Dreamweaver CC can't connect to my local XAMPP testing server via PHP

I'm a newbie at PHP, and coding in general, however I'm attempting to set up a simple user login/registration page using a MySQL database via phpmyadmin. After hours of trawling through forums etc, I could only find outdated information with no relevance to my specific issue.
My set up is as follows:
XAMPP v3.2.2, phpMyAdmin (Apache/2.4.18 (Win32) Open SSL/1.0.2e, PHP/7.0.3 with mysqli PHP extension/ Maria DB Server Type) & Dreamweaver CC.
I have set up a new site & a new server (following various online tutorials that show the same method) but when I attempt to define a connection via recordset, it displays the error message:
"Your PHP server doesn't have the MySQL module loaded or you can't use the mysql_(p)connect functions."
I have the MySQL module loaded and it's working fine as I am able to connect to phpmyadmin. Apache module is loaded and it's working fine as I am able to connect to my localhost in the web browser. Details for testing server have been set up in C:\xampp\htdocs\mysite and changed to PHP/MySQL server model.
The database and tables have been created in phpmyadmin and ready to go, however I just can't connect to them through Dreamweaver CC.
I've tried changing information in config.inc.php, tried changing extension_dir path to mysqli.dll in php.ini (which is in C:\xampp\php\ext) and also uninstalled and reinstalled XAMPP twice.
I believe that my phpmyadmin and XAMPP server are working fine, however I just can't seem to have any success linking it to my php pages in Dreamweaver CC.
Am I missing anything obvious?
Any help/clarity would be greatly appreciated as I'm currently losing all hope! Thank you!
i know this is a little old but i was able to fix the problem.
xampp by default only has MySQLi dll activated. go to the php ini file and uncomment the MySQL dll and restart the Apache server.
this worked for me. also cs6 need the database extension installed.
The error says "Your PHP server doesn't have the MySQL module loaded or you can't use the mysql_(p)connect functions."
In php7 mysql_* has been deleted you have to use mysqli or PDO instead.
You can read more about hereenter link description here
The error shows that you are using mysql_* extension. Your code must look like these: mysql_connect("localhost", "user", "password");. You have to change this to $conn = new mysqli("localhost", "user", "password", "database");. Check this link to review how to change your

PHP file can't access MySQL database after changing server

So I have a PHP application running well on my local server, powered by Zend Server. After I uploaded it to the ubuntu server, it stops working, except for two php file, the index.php and register.php, which handles the user registration part. When I say working, it means I can view the page as in my local server but the internal process is not functioning. For example, when I try to register a user, it should tell me whether it is successful or not. Instead, it is a just a blank page.
I rule out the directory problem. All the path are relative and they are working fine. I also rule out the MySQL database problem as I can access the database and do everything with it. So I wrote the following script to test where php can access mySQL (the MySQL database is set up in the same host). So I wrote the following script:
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
require_once("config/connect_database_viewer.php");
echo $db_hostname;
echo "<br>";
echo "1";
echo $db_username;
$sql = "CREATE TABLE writer (UPC VARCHAR(15)) ENGINE MyISAM";
if ($db_server->query($sql)) {
echo "the database works";
} else {
echo "so it didn't even reach the server";
}
Fatal error: Fatal error: Class 'mysqli' not found in /var/www/viewer/angelo/config/connect_database_viewer.php on line 3.
But it doesn't make sense to me as I tried to install php5-mysql by this command:
sudo apt-get install php5-mysql
it comes back to me saying php5-mysql is already the newest version. And I checked the ubuntu page for the version, the php5-mysql module should include a mysqli extension. The php module is enabled too! Here is my connect_database_viewer.php file:
<?php
$db_hostname = '127.0.0.1';
$db_database = 'viewer';
$db_username = 'juvo1';
$db_password = 'juvo1';
$db_server = new mysqli($db_hostname, $db_username, $db_password, $db_database);
if ($db_server->connect_error) die($db_server->connect_error)
?>
You need to check the error log. It'll tell you where the error happened in your PHP script. Try looking in
/var/log/httpd/error.log
and see what it says.
If only the first line of your test script is printed, your include script creates an error. The reason can be that your include fails (maybe due to rights problem) or something inside the script. I would include the
ini_set("display_errors", 1);
error_reporting(E_ALL);
to get the errors in the browser if your console (= error log?) does not mention anything else.
It appears that there are two problem in the process. The first one is that there are no mysqli module built-in for my version of php. People can install the php mysqli module with this command:
sudo apt-get install php5-mysqlnd
This stops the white page from showing up. But there is still warning about trying to connect via tcp. So I change the database conf php file from 127.0.0.1 to localhost. For more information, please refer to this link: mysqli::mysqli(): (HY000/2002): Can't connect to local MySQL server through socket 'MySQL' (2).

Configuring PHP 5.3 to work with MS SQL Server

I've been tasked with taking all of our MySQL databases and migrating them to SQL Server. In attempting to get PHP 5.3.24 to work with MSSQL, I've done the following:
Copied the non thread-safe drivers to /php/ext.
Updated my php.ini file to include the following two lines:
extension=php_pdo_sqlsrv_53_nts_vc9.dll;
extension=php_sqlsrv_53_nts_vc9.dll
Restarted IIS 7 running on Windows Server 2008 R2.
phpinfo shows sqlsrv under Registered PHP Streams, and sqlsrv also appears under PDO drivers and pdo_sqlsrv support shows as "enabled." Yet, when I try to connect via my application, I get the following:
Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\inetpub\wwwroot\mystuff\myphpconnectfile.php
My connect string looks like this:
$con = new PDO("mssql:host=xxx.xxx.xxx.xxx,1433;dbname=mydb", "user", "pwd");
$con -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return($con);
Looks like it's finding the server okay, but not the PHP/PDO mssql drivers. What have I missed?
EDIT: Configure Command under phpinfo shows the lines "--without-mssql" "--without-pdo-mssql". I don't know where to go to change this, or if it's necessary.
You're using an invalid DSN connection string for MSSQL. You need to use the format found here.
new PDO('sqlsrv:Server=xxx.xxx.xxx.xxx,1433;Database=mydb', 'user', 'pwd');

How to connect to Sybase via PHP

I'm trying to connect to a Sybase database with PHP5. I believe I've successfully compiled PHP with PDO_DBLIB, as phpinfo() lists dblib under PDO drivers, and freetds as the pdo_dblib flavour.
However, when I try to test a connection, I get an error reading:
'PDOException' with message 'could not find driver'
I'm trying to connect to a server on my LAN with this code:
$dbh = new PDO("sybase:host=192.168.1.xxx;dbname=[database-name]", '[user]', '[pass]');
Any suggestions would be greatly appreciated!
You should use dblib instead of sybase, like this:
$dbh = new PDO("dblib:host=192.168.1.xxx;dbname=[database-name]", '[user]', '[pass]');
PDO wouldn't work, or at least there is no PDO Sybase support for php. On Windows, you can use ODBC, and PDO_SQLSRV or PDO_ODBC, it might sound weird, but it should work.
Second option and I would recommend it, is to connect directly to Sybase (SqlAnywhere), but you need to install SQL Anywhere PHP Module
If you are using Ubuntu you can put the LD_LIBRARY_PATH inside envvars and it seems to read... still trying to find a way to get it to stick on RHEL based systems... Windows I am not too sure about I would hope you could set a system wide variable under
my computer -> properties -> advanced options
If are using RHEL based systems it might be better to include the:
export LD_LIBRARY_PATH=/path/to/library/ in the httpd restart script (check to see if it loads /etc/sysconfig/httpd and if so add the line in there - now restart apache and you should see some activity.

Categories