my question is about connect with mysql database in other server by php ??
Server A is rec.misuratau.edu.ly, Server B is alrand.ly , I need to code pls.
I am not sure if I understand well. You are asking how to make a connection to mysql database on two different server?
You can do it like this:
First create databases on server, create user define password and grant access to the user.
Create two separate file: First is Config.php
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'http://rec.misuratau.edu.ly'); // replace with servername
define('DB_NAME', 'db');
define('USER_CREATED_SUCCESSFULLY', 0);
define('USER_CREATE_FAILED', 1);
define('USER_ALREADY_EXISTED', 2);
Second is DbConnect.php
class DbConnect {
private $conn;
function __construct() {
}
/**
* Establishing database connection
* #return database connection handler
*/
function connect() {
include_once dirname(__FILE__) . '/Config.php';
// Connecting to mysql database
$this->conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
// Check for database connection error
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($this->conn,"utf8");
// returing connection resource
return $this->conn;
}
}
LAter you just call the method before sql statements.
Please check the link below as well:
https://www.w3schools.com/php/php_mysql_connect.asp
Related
I've got an php script that creates a Connection to my MYSQL Database.
<?php
$connection = new mysqli('127.0.0.1', 'root', 'password', 'table');
if($connection){
echo "Verbindung Erfolgreich";
}
?>
And than I've got other PHP Scripts for my Application. My Question is now, does it everytime opens a new DB Connection for each time i call require_once("dbconnect.php") ? Or does someone know how I can let the Connection open until i close it?
If you use require_once("dbconnect.php") every time the script is executed a connection will open. If you do not want this you can wrap the connection in a function and use that function if you need a database connection.
dbconnect.php:
function db_connect() {
$connection = new mysqli('127.0.0.1', 'root', 'password', 'table');
if ($connection->connect_error) {
die('Connect Error ('.$connection->connect_errno.')'.$connection->connect_error);
}
return $connection;
}
someotherfile.php:
require_once("dbconnect.php")
$dbconnection = db_connect();
... do something with the connection...
$dbconnection->close();; // close connection
I have installed WAMP server on my local machine and I have written php script to insert record into the database, but I'm getting the following error-
Notice: Use of undefined constant DB_SERVER - assumed 'DB_SERVER' in C:\wamp\www\test_services\db_connect.php on line 32
following is my db_config code
<?php
define('DB_USER', "root"); // db user
define('DB_PASSWORD', ""); // db password
define('DB_DATABASE', "test_db"); // database name
define('DB_SERVER', "localhost"); // db server
?>
Here is my **db_connect.php code**
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT
{
// constructor
function __construct()
{
// connecting to database
$this->connect();
}
// destructor
function __destruct()
{
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect()
{
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close()
{
// closing db connection
mysql_close();
}
}
?>
Please help.. Thank you..!
I don't think you are showing the correct file. The problem is in db_connect.php. It is very likely that that file does not include db_config.php, i.e:
require_once 'path/to/db_config.php';
Edit: Does db_config.php actually live in the same directory as db_connect.php? Your code suggests that it does, which may be incorrect?
Edit: What happens if you put $var = 'hello' in db_config.php, and then print $var in db_connect.php after you include it, do you see 'hello'? Just to clarify you are connecting with the file..
I'm using this simple function to connect to a named_database. But the code and its associated project are used on more than one site. I'm tired of having to constantly edit the DB_PASS settings etc.
function select_named_db()
{
$link = mysqli_connect( DB_HOST, DB_USER, DB_PASS);
$result=mysqli_select_db( $link, DB_NAME ) ;
return $link;
}
How would I alter my function so that it could try 2 or 3 different database settings so the script can figure out which host, user and password set to use?
Would be it something simple like this?
if (!mysqli_connect( DB_HOST, DB_USER, DB_PASS))
{
$link = mysqli_connect( DB_HOST, DB_USER, DB_PASS);
} elseif (!mysqli_connect( DB_HOST2, DB_USER2, DB_PASS2))
{
$link=mysqli_connect( DB_HOST2, DB_USER2, DB_PASS2);
}
Or is there a better procedural method?
settings.php:
if (ENV == 'dev') {
define('DB_HOST', 'localhost');
// and so on
} elseif(ENV == 'production') {
define('DB_HOST', '127.0.0.1');
// and so on
}
then define ENV somewhere in bootstrap file or webserver config
isn't it better to pass a parameter to select_named_db()?
You could have an array of DB connections:
$DB_HOST[0]=...
$DB_USER[0]=...
and depending the page where you call from, use a DB or another. This parameter could be defined in the function or be passed in session, as you wish...
I just created an android app in which app is connected with MySql through PHP files.. I did according to this tutorial this
But I am able to connect when igave localhost address when i gave my server address it is not working .. The following is db_connect .. i didnt make any change
<?php
/**
* A class file to connect to database
*/
class DB_CONNECT {
// constructor
function __construct() {
// connecting to database
$this->connect();
}
// destructor
function __destruct() {
// closing db connection
$this->close();
}
/**
* Function to connect with database
*/
function connect() {
// import database connection variables
require_once __DIR__ . '/db_config.php';
// Connecting to mysql database
$con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());
// Selecing database
$db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
// returing connection cursor
return $con;
}
/**
* Function to close db connection
*/
function close() {
// closing db connection
mysql_close();
}
}
?>
db_config.php
<?php
/*
* All database connection variables
*/
define('DB_USER', "root");
// db user
define('DB_PASSWORD', "");
// db password (mention your db password here)
define('DB_DATABASE', "androidhive");
// database name
define('DB_SERVER', "localhost"); // db server
?>
this is the link i uploaded my file...
Warning: mysql_connect() [function.mysql-connect]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in /home/content/96/11645896/html/app/db_connect.php on line 28
Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
Warning: mysql_close(): no MySQL-Link resource supplied in /home/content/96/11645896/html/app/db_connect.php on line 42
but message came like this... what I have to do...
You need to give a server address of your own.. Instead of androidhive's and give the username and password of that particular server.. and edit the db_config.php file according to that.. then try to connect..
Check your db credentials and make sure mysqld is running. Try this /etc/init.d/mysql status or http://www.cyberciti.biz/faq/how-to-find-out-if-mysql-is-running-on-linux/ to know if MySQL is up.
How can I make a mysql connection inside a function if my mysql connect is in another include file?
Here is the mysql connect include file.
DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'adfg');
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() );
}
File: DB.php
DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'adfg');
function getConnection() {
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
trigger_error ('Could not connect to MySQL: ' . mysqli_connect_error() );
} else {
return $dbc;
}
}
Other file:
require_once '/path/to/DB.php';
$connection = getConnection();
var_dump($connection->host_info);
you could make $dbc global inside the function.... as long as both files are included it should work fine
#powtac:
I agree on a per case basis, however if you plan on making a more robust database solution for a large site, having one class object (well named is important) can be a powerful tool. That's not to say you couldn't do the same thing and make the object a static variable inside of a function to prevent a careless developer from overwriting it:)
Since you might be calling getConnection a lot, it makes sense to make it static so that it does not get created more than once. Building on powtac's version:
DEFINE ('DB_USER', 'username');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'adfg');
function getConnection() {
static $dbc;
if (!$dbc) {
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (!$dbc) {
trigger_error ('Could not connect to MySQL.', E_USER_ERROR);
}
}
return $dbc;
}
You would then include this file and call getConnection wherever it's convenient.
Apart from making $dbc static so that it's only created once, I also removed the MySql error message when a connection cannot be established (it's generally a good idea to not give out such information if your users might see it; errors on connect at development time are relatively rare and can be debugged easily on the spot). Finally, I left the trigger_error and did not replace it with the more usually seen die because it might be part of your error handling framework. However, I did raise the error severity to signify that an error when attempting to connect should be an immediate show-stopper.