MySql connection not closing in php on window server - php

Our server is facing "Internal Server Error 500" problem and we think it is related to the mysql connection not closing. Mysql connection not closing despite connection close code written in each page.
Below is code the for connection opening & close:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'akaj#32';
$dbname = 'mydb_new';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if($conn) { mysql_close($conn);}
Can any one help?

Related

Cannot change php from localhost details to my servers to connect sql database

I am new to php/sql and i have a login system which runs on my local host using xammp and it all works fine. I now want to upload it to my website but the code no longer works... I have created a sql db on my hosting service and tried to change the code.
the code that is used on the local host is
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "loginsystem";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
and this is the code that i have got from my hosting.
<?php
$host_name = 'db682827654.db.1and1.com';
$database = 'db682827654';
$user_name = 'dbo682827654';
$password = '<Enter your password here.>';
$conn = mysql_connect($host_name, $user_name, $password, $database);
if (mysql_errno()) {
die('<p>Failed to connect to MySQL: '.mysql_error().'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p >';
}
?>
however this brings up an errror message. I have changed the password to the password for my database but its still not connecting.
This is the error message.
Failed to connect to MySQL: Access denied for user 'dbo706265806'#'217.160.62.78' (using password: YES)
Any help would be greatly appreciated
use mysqli_connect (not mysql_connect) like localhost:
<?php
$dbServername = "db682827654.db.1and1.com";
$dbUsername = "dbo682827654";
$dbPassword = "<Enter your password here.=)>";
$dbName = "db682827654";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>
On your computer you are using mysqli_connect, but on the server you are trying to use mysql_connect. Just use the same file from your computer and simply change $dbServername, $dbUsername, $dbPassword and $dbName to match those that your hosting provided.

Hardcoded user/pass work for MySQL connection, but strings from file do not

First off, I'm trying to make sure that I'm not showing my MySQL password in my index page's source code. I've determined that making a "mysql.conf" file with the information I need will be sufficient.
Here is the section of code, pre-conf file. This worked without any problems:
$dbhost = "mysql.host.com";
$dbuser = "username";
$dbpass = "password";
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
Now, here is the configuration file's contents (mysql.conf):
mysql.host.com
username
password
And the corresponding changes to the code...
$dbConfig = file("./config/mysql.conf");
$dbhost = $dbConfig[0];
$dbuser = $dbConfig[1];
$dbpass = $dbConfig[2];
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
However, with the changes to use the configuration file, the MySQL connection now fails, giving me this error response:
"Could not connect: Access denied for user 'username'#'chain-lightning.dreamhost.com' (using password: YES)"
What am I missing? I've triple-checked that the text in the configuration file is the same as when I used static strings. Thanks!
The error you are getting mostly because of the data getting from file is not as you think. all your value will be added with extra newline value.
from http://php.net/manual/en/function.file.php
Returns the file in an array. Each element of the array corresponds to
a line in the file, with the newline still attached.
use trim function with your variable it will work fine.
$dbConfig = file("./config/mysql.conf");
$dbhost = trim($dbConfig[0]);
$dbuser = trim($dbConfig[1]);
$dbpass = trim($dbConfig[2]);
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
or you can use FILE_IGNORE_NEW_LINES flag in file function
$dbConfig = file("./config/mysql.conf", FILE_IGNORE_NEW_LINES);
$dbhost = $dbConfig[0];
$dbuser = $dbConfig[1];
$dbpass = $dbConfig[2];
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);

I can't connect to phpmyadmin using php

I'm obviously new to PHP, and I've been asked by a friend to create a dynamic blog that could use continuously. I wanted it to connect to phpMyAdmin so it would be easier, but when I try to connect with it to my web page, it always fails and my set error appears. I don't know what to look up to help me. I'm sorry if I'm not clear. Post if you want me to go more into detail.
Here's my code:
<?php
$dbhost = 'localhost';
$dbuser = ' root';
$dbpass = '';
$dbname = 'blog1';
$connect = mysqli_connect($dbname, $dbuser, $dbhost, $dbpass) or die ('Cannot connect to database.');
echo "Succesfully connected to database!";
?>
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root123';
$dbname = 'mydb';
$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die ('Cannot connect to database.');
If you run PHPMyAdmin on localhost uncomment in file:
sudo gedit /etc/php5/apache2/php.ini
this line: mysqli.allow_local_infile = On
Restart Apache:
sudo /etc/init.d/apache2 restart or sudo service apache2 restart
Test This
<?php
$dbhost = 'localhost';
$dbuser = ' root';
$dbpass = '';
$dbname = 'blog1';
$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die ('Cannot connect to database.');
echo "Succesfully connected to database!";
The mysqli_connect() was called in wrong way.

Mysql database connection to PHP

I am trying to connect mysql database to a php program. I used following program to establish connection. But it is not giving any error even I make any mistake with the code.
<?php
// creating database connection
$dbhost ="localhost";
$dbuser = "root";
$dbpass = "1234";
$dbname = "my_new";
//connection
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
//testing connection
if(mysqli_connect_errno()){
die ("database connection failed :".
mysqli_connect_error() .
"(".mysqli_connect_errno().")"
);
}
?>
In my computer, your code is work well. And I find that mysql_connect will not throw exception.

Storage of database credentials in php.ini or a php file (ex. database_connection.php)?

Is one preferred over another?
And what would be php.ini equivalent syntax to connect to a database the same way this php script does?
database_connection.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
?>
The equivalent syntax would be:
$conn = mysql_connect() or die ('Error connecting to mysql');
There are no required parameters. If you do not provide any, the defaults from php.ini are used.

Categories