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"
Related
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.
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");
I try to upload file to a database by using php.
Here is my code:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db ='testdb';
// Create connection
mysql_connect($servername, $username, $password);
mysql_select_db("testdb");
if(isset($_POST['submit']))
{
$UploadName = $_FILES['UploadFileField']['name'];
$UploadTmp = $_FILES['UploadFileField']['tmp_name'];
$UploadName = preg_replace("#[^a-z0-9.]#i","",$UploadName);
if (!$UploadTmp)
{
die ("No File Selected, Please Upload Again");
} else
{
move_uploaded_file($UploadTmp,"uploaded/$UploadName");
$url = "http://localhost/uploadandview/uploaded/$UploadName";
mysql_query("INSERT INTO `videos` VALUE('1','$UploadName','$url')");
}}?>
But i have a proplem, I can't upload any file mp3/mp4, just only upload file text, such as .doc,pdf, css, etc...
Please help me!
Try this code
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db ='testdb';
// Create connection
mysql_connect($servername, $username, $password);
mysql_select_db("testdb");
if(isset($_POST['submit']))
{
$UploadName = $_FILES['UploadFileField']['name'];
$UploadTmp = $_FILES['UploadFileField']['tmp_name'];
$UploadName = preg_replace("#[^a-z0-9.]#i","",$UploadName);
if (!$UploadTmp)
{
die ("No File Selected, Please Upload Again");
} else
{
move_uploaded_file($UploadTmp,"uploaded/".$UploadName);
$url = "http://localhost/uploadandview/uploaded/".$UploadName;
mysql_query("INSERT INTO `videos` VALUE('1','$UploadName','$url')");
}} ?>
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
]);
<?php
$host = "localhost";
$username = "root";
$password = "";
$db = "mineforums";
$connect = mysqli_connect($host, $username, $password, $db) or die(mysqli_error($connect));
?>
That is my php code to connect to my database. Whenever I try to just connect though it doesnt do anything. The way I have my login-form setup is the action will be to this code and then doesnt do anything and when I return to my index it says database not selected because if you're logged in it says 'Welcome, {username}'
you could split the process
<?PHP
$host = "localhost";
$username = "root";
$password = "";
$db = "mineforums";
$connect = mysql_connect($host, $username, $password)OR DIE("Could Not Connect To Server". MySQL_Error());
if($connect) {
mysql_select_db($db)OR DIE("Could Not Select Databse". MySQL_Error();
}
?>