php change database with sub domain - php

I searched in google, stackoverflow but all topics related php framework not core php, I want to know what is the best solution to change database depend on sub domain. i have a multi language website, i want to use single source code but with different sub domain, just change db according to sub domain name.
Here is my code:
$domain = $_SERVER['HTTP_HOST'];
$sub = array_shift((explode('.', $domain)));
if ($sub == 'tr') {
$servername = "localhost";
$username = "db_tr";
$password = "xxxxx";
$dbname = "xxxx";
} else if ($sub == 'gb') {
$servername = "localhost";
$username = "db_gb";
$password = "xxxxx";
$dbname = "xxxx";
} else if ($sub == 'us') {
$servername = "localhost";
$username = "db_us";
$password = "xxxxx";
$dbname = "xxxx";
} else if ($sub == 'ca') {
$servername = "localhost";
$username = "db_ca";
$password = "xxxxx";
$dbname = "xxxx";
} else {
$servername = "localhost";
$username = "db_all";
$password = "xxxxx";
$dbname = "xxxx";
}
So i have two question:
Is this logic good in programming? if not, What is the best solution to do this?
Is it harmful? for example for sql injection, hack or other security issue.

Related

Mongodb select collection is not working in php

I am trying to get the data from the mongo db table (location) for this i am
trying to use selectCollection() function of mongo db but it is giving following error i am new on mongo db that why i am not getting why this error
occured.
object(MongoCollection)#3 (2) { ["w"]=> int(1) ["wtimeout"]=> int(10000) }
Below is my php code
<?php
$host = "xxxx";
$user = "xxxx";
$pass = "xxx";
$db = "xxxx";
$port = "xxxx";
$con = new MongoClient("mongodb://{$host}:{$port}");
$data = $con->selectDB($db);
if ($user != '' && $pass != '')
$record=$data->authenticate($user, $pass);
$location = $data->selectCollection('location');
var_dump($location);die;
?>
Please help me thanks in Advance.

mysqli_select_db() returns true, but I get "No database selected"

as you can see my code down below, the method I used to detect does mysql_select_db() return true or false. It does return true but I still ge the "No database selected" error.
$host = "localhost";
$sql_username = "root";
$sql_password = "password";
$sql_db = "tryckstore";
$con = mysqli_connect($host, $sql_username, $sql_password) or die("Error");
if (!mysqli_select_db($con, "tryckstore")) {
die("Error selecting databse.");
} else {
echo "ok";
}
It used to work actually, then I suddenly get this error. Thanks in advance.
Use this one:
$host = "localhost";
$sql_username = "root";
$sql_password = "password";
$sql_db = "tryckstore";
$con = mysqli_connect($host, $sql_username, $sql_password,$sql_db) or die("Error");
if (!mysqli_select_db($con, "tryckstore")) {
die("Error selecting databse.");
} else {
echo "ok";
}
//pass the $sql_db as your 4th argument in the mysqli_connect also
for simplier approach:
$host = "localhost";
$sql_username = "root";
$sql_password = "password";
$sql_db = "tryckstore";
$con = mysqli_connect($host, $sql_username, $sql_password,$sql_db) or die("Error");

Method name must be a string fatal error class

How can I fix this code? When I run check.php I get this error:
Erorr : Fatal error: Method name must be a string in C:\AppServ\www\Weboo\cms\check.php on line 46
<?php
ob_start();
session_start();
class mysql {
private $localhost = "localhost";
private $db_user = "root";
private $db_pass = "root";
private $db_name ="webocms";
function __construct(){
mysql_connect($this-> localhost,$this->db_user,$this->db_pass);
mysql_select_db($this->db_name);
}
}
function sql(){
$username = $_POST ['username'];
$password = $_POST['password'];
$sql = "SELECT * from users WHERE username='$username'
AND password='$password'";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
if ($num > 1){
$_SESSION['username'] = "username";
$_SESSION['password'] = "password";
header("Location: admin/admin.php");
}else {
echo "<h2><b> No Users </h2></b>";
}
}
$use=new mysql;
$use->$sql();
ob_end_flush();
?>
As Mark said in the comments, I think it should be more like this:
<?php
ob_start();
session_start();
class mysql {
private $localhost = "localhost";
private $db_user = "root";
private $db_pass = "root";
private $db_name ="webocms";
function __construct(){
mysql_connect($this-> localhost,$this->db_user,$this->db_pass);
mysql_select_db($this->db_name);
}
function sql(){
$username = $_POST ['username'];
$password = $_POST['password'];
$sql = "SELECT * from users WHERE username='$username'
AND password='$password'";
$query = mysql_query($sql);
$num = mysql_num_rows($query);
if ($num > 1){
$_SESSION['username'] = "username";
$_SESSION['password'] = "password";
header("Location: admin/admin.php");
} else {
echo "<h2><b> No Users </h2></b>";
}
}
}
$use=new mysql;
$use->sql();
ob_end_flush();
?>
Notice, the sql() function is now inside your class and the 2nd to last line is $use->sql() not $use->$sql().
I think you've made a mistake with the closing brace of your mysql class. sql() is a function and not a method of the mysql class. You're also using a variable which in my opinion appears unintentional.
Instead it should be $use->sql(); but again that won't work until you put sql() inside the class.

php/mysql connection on different servers

I have a local and a remote server on which I am developing a project
//local
$servername = "localhost";
$username = "bob";
$password = "fred";
$dbname = "jane";
//remote
$servername = "arthur";
$username = "Milly";
$password = "Horace";
$dbname = "Erastus";
At the moment I am commenting out the //local or //remote bit depending which way I am connecting. Of course, I regularly overwrite the connection file by mistake which is a pain.
Is there some sort of way I can detect which server I am on, and then connect automatically. I have tried googling but couldn't turn anything up maybe because i couldn't think of the right search terms.
Thanks
You can try this code:-
<?php
if ($_SERVER['SERVER_NAME'] == "localhost") {
$servername = "localhost";
$username = "bob";
$password = "fred";
$dbname = "jane";
}
elseif ($_SERVER['SERVER_NAME'] == "google.com") {
$servername = "arthur";
$username = "Milly";
$password = "Horace";
$dbname = "Erastus";
}
else {
echo "No configuration found!";
exit;
}
There are more than a few ways of doing this but one you could try is to detect which server you are on from the IP Address
if ($_SERVER['SERVER_ADDR'] === '127.0.0.1') {
$servername = "localhost";
$username = "bob";
$password = "fred";
$dbname = "jane";
} else {
//remote
$servername = "arthur";
$username = "Milly";
$password = "Horace";
$dbname = "Erastus";
}
try this code :-
$conn = new mysqli($servername1, $username1, $password1);
if (! $conn->connect_error) {
$conn = new mysqli($servername2, $username2, $password2);
}
Or check :-
$_SERVER['HTTP_HOST']=="localhost"

PHP Insert Query Using Prepare Statement

I have created an insert form. I'm doing an insertion operation into MySQL using prepare statement but it's not working. I don't understand what's wrong. Please help me to solve this issue. Is this what I did correct?
insert.php
<?php
include('dbconn.php');
session_start();
$_SESSION['example']='Session Created';
$srn = $_POST['srn'];
$client = $_POST['client']; // required
$category = $_POST['category'];
$sd = $_POST['sd']; // required
$fd = $_POST['fd'];
$host = "localhost";
$user = "root";
$pwd = "root";
$db = "eservice";
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
$sql = "Insert into main(client,category,sd,fd) values(:client,:category,:sd,:fd)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':client',$_POST['client'],PDO::PARAM_STR);
$stmt->bindParam(':category',$_POST['category'],PDO::PARAM_STR);
$stmt->bindParam(':sd',$_POST['sd'],PDO::PARAM_STR);
$stmt->bindParam(':fd',$_POST['fd'],PDO::PARAM_STR);
$stmt->execute();
?>
dbconn.php
<?php
$host = "localhost";
$user = "root";
$pwd = "root";
$db = "eservice";
$mysqli = new mysqli($host,$user,$pwd,$db);
/* ESTABLISH CONNECTION */
if (mysqli_connect_errno()) {
echo "Failed to connect to mysql : " . mysqli_connect_error();
exit();
}
?>
Its always good to put up the errors you are having.
You are using two different database connection types pdo, and mysqli. You only need one.
I stripped your code down to the minimum.
<?php
$host = "localhost";
$user = "root";
$pwd = "root";
$db = "eservice";
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pwd);
//$srn = $_POST['srn'];
$client = $_POST['client']; // required
$category = $_POST['category'];
$sd = $_POST['sd']; // required
$fd = $_POST['fd'];
// make sure client and sd are set here
$stmt = $pdo->prepare("
INSERT INTO
main
(client,category,sd,fd)
VALUES
(:client,:category,:sd,:fd)
");
$success = $stmt->execute([
'client' => $client,
'category' => $category,
'sd' => $sd,
'fd' => $fd
]);

Categories