How to connect to local MySql database using PHP. [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
we are trying to connect to our local mysql database using PHP. We left out the password on purpose but is it correct. However, this is not working, we keep getting following error message: Warning: mysqli_connect() expects parameter 5 to be long, string given in.
It is referring to line 7 which would be $conn = mysqli_connect($servername, $port, $uname, $pword, $dbname);
<?php
$servername = "Mysql#127.0.0.1";
$port = "3306";
$uname = "root#localhost";
$pword = "1234";
$dbname = "database";
$conn = mysqli_connect($servername, $port, $uname, $pword, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>
Our confiq.php file
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'database');
/** MySQL database username */
define('DB_USER', 'root#localhost');
/** MySQL database password */
define('DB_PASSWORD', '');
/** MySQL hostname */
define('DB_HOST', 'Mysql#127.0.0.1');
define('port', '3306');
/** Database Charset to use in creating database tables. */
define('DB_CHARSET', 'utf8');
/** The Database Collate type. Don't change this if in doubt. */
define('DB_COLLATE', '');

You have the args in the wrong order in the constructor
$conn = mysqli_connect($servername, $port, $uname, $pword, $dbname);
should be
$conn = mysqli_connect($servername, $uname, $pword, $dbname, $port );
It looks however like the info in the config file is incorrect -
define('DB_HOST', 'Mysql#127.0.0.1');
would, I suspect, actually be
define('DB_HOST', '127.0.0.1');
though perhaps not!
so then, using the info from your config file, the connection would be:
$conn = mysqli_connect( DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, port );

Related

PHP connecting to database fails without error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to connect to databse on my server by using this code:
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
But code after $conn = new mysqli($servername, $username, $password); is not executing. When i try to echo anything after it I do not get output. Code before that line works as expected. I do not get any errors from php.
I am not sure what is problem. Could it be something server related? I did try to add:
ini_set('display_errors',1);
error_reporting(E_ALL);
but it didn't help, no errors have been displayed.
I have been trying many things from stackoverflow (and other places) but they didnt help, some examples:
Connecting to a mysql database from php, error but no error shown?
Connection of MySQL with PHP not working
There are multiple ways to handle errors
Simplest of all, use try catch while connecting
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
try {
$conn = new mysqli($servername, $username, $password); } catch(\Exception $e) { var_dump ('oopss... this is the error', $e)}
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected

Why isn't my PHP able to connect to MySQL?

I am trying to connect to my MySQL database using the following PHP code
<?php
require "credentials.php";
$conn = mysqli_connect($hostname, $username, $password, $databaseName);
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
And I am getting the following output:
Failed to connect to MySQL: Connection refused
What could be the reasons that this is happening, and how can I fix it?
Thanks in advance.
from your code it seems that you are using undefined variables to connect to database. if you load them from external file, than use something like this in that file:
define("DBPASSWORD", "database_pass");
define("DBUSERNAME", "databaseusername");
define("DBNAME", "database");
define("DBHOST", "database_host");
and than use in file you shown us:
$conn = mysqli_connect(DBHOST, DBUSERNAME, DBPASSWORD, DBNAME);
OR another way, the way I can't recomend is using globals.
WARNING: I discourage using globals, when not needed.
Your credentials.php should look like that:
global $databaseName;
global $password;
global $username;
global $username;
$databaseName= "db_name";
$password= "db_password";
$username= "db_username";
$hostname= "db_host";
and than in other places, first you need to call the globals before usage.
require "credentials.php";
global $databaseName;
global $password;
global $username;
global $username;
$conn = mysqli_connect($hostname, $username, $password, $databaseName);

mysqli_select_db() ERROR, Database couln't connect [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I'm new to PHP and i coded a php page and created a database. I tried to connect db but it's not connecting.
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
mysql_close($mysqlconn);
?>
Hi Try to debug and echo bugs in connection with mysqli_error() function.
$uname = "root";
$pwd = "";
$hostn = "localhost";
//connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd) or die(mysqli_connect_error());
or
$mysqlconn = mysqli_connect($hostn, $uname, $pwd , $database) or die(mysqli_connect_error());
You are mixing mysql and mysqli
You have to replace
/connection to the database
$mysqlconn = mysqli_connect($hostn, $uname, $pwd)
or die("Unable to connect to MySQL");
//select a database to work with
$dbselect = mysqli_select_db("dataforuse",$mysqlconn)
or die("Could not select dataforuse");
With this code
$mysqlconn = mysqli_connect($hostn, $uname, $pwd,"dataforuse")
or die("Unable to connect to MySQL");
connection variable then database name ..try this
$dbselect = mysqli_select_db($mysqlconn,"dataforuse");
and mysql close should be like this
mysqli_close($mysqlconn);
dont mix mysql with mysqli
You should write
<?php
$uname = "root";
$pwd = "";
$hostn = "localhost";
$dbname = "dataforuse";
$conn = new mysqli($hostn, $uname, $pwd, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
This will solve Your Problem

Connection Issue - Unable to connect to database server?

I am getting this message in my site all of a sudden without making any changes in the config file. I will post my config code to see if there are any issues with it.
define('DB_SERVER', 'www.victorexoticagoa.com');
define('DB_SERVER_USERNAME', '******');
define('DB_SERVER_PASSWORD', '********');
define('DB_DATABASE', 'victor');
define('USE_PCONNECT', 'false');
define('STORE_SESSIONS', 'mysql');
define('CFG_TIME_ZONE', 'Asia/Kolkata');
The above code is from the configure.php file.
The below code is the connection:
tep_db_connect() or die('Unable to connect to database server!');
And the code below is the function which does the connection:
function tep_db_connect($server = DB_SERVER, $username = DB_SERVER_USERNAME, $password = DB_SERVER_PASSWORD, $database = DB_DATABASE, $link = 'db_link') {
global $$link;
if (USE_PCONNECT == 'true') {
$server = 'p:' . $server;
}
$link = mysqli_connect($server, $username, $password, $database);
if ( !mysqli_connect_errno() ) {
mysqli_set_charset($$link, 'utf8');
}
return $$link;
}
Any help will be gladly appreciated. Thanks
Check if your database is up by using the following command in a command shell:
mysql --host=www.victorexoticagoa.com --port=yourport --user=youruser --pass=yourpass
If it cannot connect, the problem is the server, not your code.
Have you checked if that is the way you need to connect or the port to connect? I dont see the port (thats not supposed to be port 80 most times). Please check docs of the host and ask for details. It should be documented.

How to open DB connection in Openshift? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
This is my dbcon file in PHP.
Basically, I need to connect my PHP application with openshift MySQL DB. Here's what I did.
<?php
// Database Connection Setting
$dbhost = "127.0.0.1"; // Host name
$dbport = "3308"; // Host port
$dbusername = "user"; // Mysql username
$dbpassword = "pass"; // Mysql password
$db_name = "mf"; // Database name
$mysqlCon = mysqli_connect($dbhost, $dbusername, $dbpassword, "", $dbport) or die("Error: " . mysqli_error($mysqlCon));
mysqli_select_db($mysqlCon, $db_name) or die("Error: " . mysqli_error($mysqlCon));
?>
This gives me an error on openshift but works for other PHP apps. I get nothing on error explanation only as Error: { ...blank space... }.
I've made it to work by doing this.
Global Use
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT', getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER', getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS', getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME', getenv('OPENSHIFT_GEAR_NAME'));
$dbhost = constant("DB_HOST"); // Host name
$dbport = constant("DB_PORT"); // Host port
$dbusername = constant("DB_USER"); // MySQL username
$dbpassword = constant("DB_PASS"); // MySQL password
$db_name = constant("DB_NAME"); // Database name
Alternatively
$dbhost = getenv('OPENSHIFT_MYSQL_DB_HOST'); // Host name
$dbport = getenv('OPENSHIFT_MYSQL_DB_PORT'); // Host port
$dbusername = getenv('OPENSHIFT_MYSQL_DB_USERNAME'); // MySQL username
$dbpassword = getenv('OPENSHIFT_MYSQL_DB_PASSWORD'); // MySQL password
$db_name = getenv('OPENSHIFT_GEAR_NAME'); // Database name
define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT', getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER', getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS', getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME', getenv('OPENSHIFT_APP_NAME'));
$mysqlCon = mysqli_connect(DB_HOST, DB_USER, DB_PASS, "", DB_PORT) or die("Error: " . mysqli_error($mysqlCon));
mysqli_select_db($mysqlCon, DB_NAME) or die("Error: " . mysqli_error($mysqlCon));
Or better still, just use the environment variables in your connection string:
$mysqlCon = mysqli_connect(getenv('OPENSHIFT_MYSQL_DB_HOST'), getenv('OPENSHIFT_MYSQL_DB_USERNAME'), getenv('OPENSHIFT_MYSQL_DB_PASSWORD'), "", getenv('OPENSHIFT_MYSQL_DB_PORT')) or die("Error: " . mysqli_error($mysqlCon));
mysqli_select_db($mysqlCon, getenv('OPENSHIFT_APP_NAME')) or die("Error: " . mysqli_error($mysqlCon));

Categories