PHP script can't run once i add `mysql_connect()` - php

i have installed Linux Mint 18.1, then LAMP server using the command
sudo apt-get install lamp-server^ , for testing i tried to create a script connecting to MYSQL database, just like the following.
<?php
$h = "localhost";
$u = "root";
$p = "password";
$conn = mysql_connect($h,$u,$p);
echo "test";
?>
when i remove the line $conn = mysql_connect($h,$u,$p);
the script works fine, otherwise it isn't running,
php5.6-mysql is already installed , and i also tried to connect using
mysqli_connect() instead of mysql_connect()
i don't know what's wrong ?!

Paste this at the top of your file:
error_reporting(E_ALL);
ini_set('display_errors', 1);
It will help you to see the exact error.
Alternatively, you can try:
$conn = mysql_connect($h,$u,$p) or die('Connection Error');
If connection is not getting established then it will show Connection Error

Related

How to connect Docker PHP container to MySql container?

I have created a docker container with MySQL:
docker run --name mysqlfordocker -p3307:3306 -e MYSQL_ROOT_PASSWORD=password_db-d mysql
It works as expected.
It is used by MySQLWorckbech and PHP application which are not in the container with MySQL:
//CONNECTION BY PHP APPLICATION NOT IN CONTAINER
$servername = "127.0.0.1:3307";
$username_db = "username_db";
$password_db = "password_db";
$db = "db";
The Dockerfile for my PHP application is:
FROM php:7.0-apache
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq-dev \
&& docker-php-ext-install mysqli pdo_pgsql pdo_mysql
COPY / /var/www/html
EXPOSE 80
Then I created a container with my PHP application:
docker run –d –p 5000:80 myphpapplication
The PHP application runs, but it has issues with database connection to MySQL container.
I tried to configure a connection servername in a different way but without any luck:
$servername = "127.0.0.1:3307";
$servername = "locahosto:3307";
$servername = "10.0.75.1:3307"; //DockerNAT
$servername = "172.17.0.2:3307"; //Ip by docker inspect mysql
Could you please help me on this?
Ps.
There are two parts to this, one is getting the right IP address of the container. Either the actual IP address should work- 172.17.0.2, but you should also be able to use 172.17.0.1.
Also you normally put the port number as a separate parameter...
$servername = "172.17.0.1";
$username_db = "username_db";
$password_db = "password_db";
$db = "db";
$port= = 3307;
$db = mysqli_connect($servername, $username_db, $password_db, $db, $port);

Eclipse: "PHP Fatal error: Call to undefined function mysqli_connect()"

I have a php script (machine_db.php) which makes a connection to mysql database as follows:
<?php
//phpinfo();
// 1 connect to mysql
$servername = "localhost";
$username = "root";
$password = "abcd";
$conn = mysqli_connect($servername, $username, $password);
...
?>
I am using:
Server version: Apache/2.4.12 (Ubuntu)
PHP 5.6.11
mysql Ver 14.14 Distrib 5.6.28
I am using Eclipse Mars and I installed PHP packages (addin or whatever it is called) for Eclipse. Then I created a php project and added my php file to the project and did run as php cli application but I get the following error in Eclipse console,
PHP Fatal error: Call to undefined function mysqli_connect()
However I don't get any error when I run the exact same script from terminal like:
php machine_db.php
I also see that in my Eclipse project explorer there is a PHP Language Library which contains mysqli class.
I think you forget the db name:
$servername = "localhost";
$username = "root";
$password = "abcd";
$db_name = "my-db";
$conn = mysqli_connect($servername, $username, $password, $db_name);
You can also use
$conn = new mysqli($servername, $username, $password, $db_name);
Documentation: http://php.net/manual/fr/mysqli.construct.php
Also try to install php5-mysqli
sudo apt-get update
sudo apt-get install php5-mysql php5-mysqli
sudo service apache2 restart
dont forget to uncomment this line in your /etc/php5/apache2/php.ini
extension=php_mysql.so
extension=php_mysqli.so
Dont forget to restart apache server
sudo service apache2 restart
I just realized that if in eclipse i check "use system default php.ini" under window->preferences->php->phpexecutables it will magically work.
Does anyone know what is "system default php.ini"?
I do a a quick $php --ini and it returns:
Configuration File (php.ini) Path: /etc/php5/cli
Loaded Configuration File: /etc/php5/cli/php.ini
Scan for additional .ini files in: /etc/php5/cli/conf.d
Additional .ini files parsed: /etc/php5/cli/conf.d/05-opcache.ini,
/etc/php5/cli/conf.d/10-mysqlnd.ini,
/etc/php5/cli/conf.d/10-pdo.ini,
/etc/php5/cli/conf.d/20-json.ini,
/etc/php5/cli/conf.d/20-mysql.ini,
/etc/php5/cli/conf.d/20-mysqli.ini,
/etc/php5/cli/conf.d/20-pdo_mysql.ini,
/etc/php5/cli/conf.d/20-readline.ini

Cannot connect to MYSQL using PHP

I could not connect to the local host MYSQL database using my simple php code and DOES NOT display anyything. THis is applicable to all users and root.Eventhough the problem is reported many times, i could not fix it. Also it is written in one site that it may have to make changes in php file.Please give me solution for this connection error.
I use php 5.5.9-1 in Ubuntu. MYSQL version is 5.5.38.
My code is
<?php
$servername = "localhost";
$username = "root";
$password = "*****";
$conn = mysql_connect($servername, $username, $password);
if ($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Call to undefined function mysql_connect()
This error message from your comments tells me that you do not have the MySQL extension installed.
Execute this in your terminal to install: sudo apt-get install php5-mysql
Then restart Apache: sudo /etc/init.d/apache2 restart
Please be aware that the "mysql_" functions are marked as deprecated and your script might fail with a future PHP update. Please use "mysqli_" or PDO.
Mysql is deprecated. Use Mysqli.
Create a file and name it as db_connect.php and copy the below code.
<?php
$connection = mysqli_connect("localhost","username","password","database_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Open browser and type http://localhost/db_connect.php. If you see a blank page, your connection is fine. If there's a problem, it will show you the error.

configuring php with oracle usin tns on mac os x

I am new to php but I've got it running (php 5.3.3) on my mac (OS 10.6). However when I try to run this script:
<?php // Create connection to Oracle
$conn = oci_connect("user", "pass", "tnsnames.ora");
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
// Close the Oracle connection
oci_close($conn);
?>
I get the error: "Fatal error: Call to undefined function oci_connect()..."
Now, I've checked oracle's website and downloaded their instant client, but I'm stuck as to what to do next. Their instructions are:
On non-Windows platforms rebuild PHP
using the following configuration
option:
--with-oci8=shared,instantclient,/path/to/instant/client/libs
Edit your php.ini file and add:
extension = oci8.so Ensure that your
extension_dir parameter (in php.ini)
points to the location where oci8.so
was installed. Set environment
variables required by Oracle, such as
PATH (Windows) or LD_LIBRARY_PATH (on
Linux) Restart you webserver.
But could someone explain that to me in simpler language? I am really confused. I can't find an oci8.so file in the instant client folder, and I don't know where to put the extention = oci8.so in the file (all I have are php.ini-production and php.ini-development. And I don't know how "rebuild" PHP with configuration options.
Oh and I don't know how to pull in the tnsnames.org file either. Is this the right way? Since I don't even have a connection yet, I don't know if this is failing or not.
You don't want the filename "tnsnames.ora" in your oci_connect call. You want the name of a database connection alias in the tnsnames.ora file. The tnsnames.ora file contains a list of known database connection entires, identified by aliases. For a database alias named "db_alias", you would use this call:
$conn = oci_connect("user", "pass", "db_alias");
A typical tnsnames.ora entry would look something like this:
DB_NAME =
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = foo.bar.com)(PORT = 1521))
(CONNECT_DATA = (SERVICE = some_db_service_name))
)
Alternatively, you can use an Easy Connect string:
$conn = oci_connect("user", "pass", "//host:port/db_service_name");
Where host, port and db_service_name are to be replaced by values from your environment. So, for host "foo" with database "bar" on standard port 1521, your connection would look like this:
$conn = oci_connect("user", "pass", "//foo:1521/bar");
Have a look at the manual page for oci_connect for more information.
I do it with a full connect string:
$rnum=rand(0,99999999);
$connect_str = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP) (HOST = $dbserver) (PORT = $dbport) (HASH = '.$rnum.') ) (CONNECT_DATA =(SID = $dbname)) )";
$DB = oci_connect($dbuser, $dbpass, $connect_str);
Works like a charm for me.

PHP: Unknown MySQL server host with mysqli

I have the following code:
$username = 'username';
$password = 'password';
$host = 'localhost';
$db = 'dbname';
$dbh = new mysqli('p:'.$host, $username, $password, $db)
or die('no connection to server');
But I get the following error
PHP Warning: mysqli::mysqli() [<a href='function.mysqli-mysqli'>function.mysqli-mysqli</a>]: (HY000/2005): Unknown MySQL server host 'p:localhost' (1) in /var/www/vhosts/politiker.lu/httpdocs/includes/sql.php on line 8
This intrigues me because the very same code worked on my development environment. Now that I want to set it up on my production server, it does not work. Any ideas?
Note: I am not very good with servers, so excuse me if the error should be something trivial.
EDIT 1
Here are the versions:
Development PHP (5.3.3-1ubuntu9.1) + MySQL(5.1.49-1ubuntu8.1)
Production PHP (5.2.4-2ubuntu5.12) + MySQL(5.0.51a)
take out the p: like this:
$dbh = new mysqli($host, $username, $password, $db)
or die('no connection to server');
Just adding this since I didn't think it was clear, but the problem is definitely your production version of PHP. Persistent connections for the mysqli extensions weren't added until 5.3 since they caused headaches before.
Some hosts might have different versions of PHP installed... I realized today that I can make HostGator use PHP 5.3! I just had to add to my .htaccess file:
Action application/x-hg-php53 /cgi-sys/php53
AddType application/x-hg-php53 .php
i think you missed the mysqli package in this server. Try :
sudo apt-get install php5-mysqli
if apt-get says it is already installed try to do a ping to localhost, if don't resolve edit /etc/hosts and check the line where localhost points to 127.0.0.1
should be there, if not, your server its horribly configured!
do a
ifconfig
and check if you get some output like this...
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
You need to specify the actual location of the MySQL server. For development, it is localhost (most likely), just how you have it configured. But for production servers, it is going to be something else. I doubt it should be localhost.

Categories