i just want to connect mysql database from other server - php

example : mydatabase is on google.com/phpmyadmin
and i want to insert data from yahoo.com/insertdata.php
both domains are not on same server both domains are on different servers
so how to connect database
<?php
$server = "38.89.136.77";
$database = "dbname";
$username = "dbusers";
$password = "password";
$mysqlConnection = mysqli_connect($server, $username, $password);
if (!$mysqlConnection)
{
echo "Please try later.";
}
else
{
mysqli_select_db($database, $mysqlConnection);
echo " database is connected";
}
?>
this code is not working how can i do it get error Please try later.

Do it like this. Then you get all errors displayed.
<?php
$mysqli = new mysqli("localhost", "user", "password", "database");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>
Then post your error here in the comments, so I can help you.

Related

How to connect html to Azure database?

I'm trying to connect my Azure Sql storeage and html to show everything i have. but i'm having some trouble. i researched w3school and other resources but i still don't know what is wrong?
so i use Notepad++ and save it as html and use php to establish the connection
here is the code in my notepad+++ so far:
<?php
$servername = "servername*****.mysql.database.azure.com";
$username = "loginfor****n#mysql***";
$password = "*****";
$db = "db";
// Create connection
$conn = new mysqli($servername, $username, $password. $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
and this is what i got
connect_error) { die("Connection failed: " . $conn->connect_error);
i have no idea where did i went wrong. please help me if you can, thanks
I'm not 100% sure as I'm not a PHP dev, but Microsoft have the following on their Azure documentation (here):
<?php
$serverName = "your_server.database.windows.net"; // update me
$connectionOptions = array(
"Database" => "your_database", // update me
"Uid" => "your_username", // update me
"PWD" => "your_password" // update me
);
//Establishes the connection
$conn = sqlsrv_connect($serverName, $connectionOptions);
$tsql= "SELECT TOP 20 pc.Name as CategoryName, p.name as ProductName
FROM [SalesLT].[ProductCategory] pc
JOIN [SalesLT].[Product] p
ON pc.productcategoryid = p.productcategoryid";
$getResults= sqlsrv_query($conn, $tsql);
echo ("Reading data from table" . PHP_EOL);
if ($getResults == FALSE)
echo (sqlsrv_errors());
while ($row = sqlsrv_fetch_array($getResults, SQLSRV_FETCH_ASSOC)) {
echo ($row['CategoryName'] . " " . $row['ProductName'] . PHP_EOL);
}
sqlsrv_free_stmt($getResults);
?>
My guess is that your PHP is malformed and you're getting a render of the code rather than it actually properly executing - be sure to read the documentation.
i've also tried this code too
<?php
$host = '****.mysql.database.azure.com';
$username = '****#mysql****';
$password = '****';
$db_name = '*****';
//Establishes the connection
$conn = mysqli_init();
printf("hello")
mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306);
if (mysqli_connect_errno($conn)) {
printf("sory");
die('Failed to connect to MySQL: '.mysqli_connect_error());
}
// Run the create table query
if (mysqli_query($conn, '
select * from table1;
')) {
printf("Table created\n");
}
//Close the connection
mysqli_close($conn);
?>
it returns a blank page, i feel that some how it just skip all of my php code

PHP & MYSQL no database selected

i get an "no database selected" error and i can't figure out why. Would be nice if someone could help me out. Code below. There are no typos in there and im using XAMPP/Apache as server so localhost should be right i guess?
<!--Insert in database-->
<?php
$servername = "localhost";
$dbname = "databank";
$conn = mysqli_connect($servername, $dbname);
if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$Kundennummer = $_POST["id"];
$Vorname = $_POST["vorname"];
$Nachname = $_POST["nachname"];
$plz = $_POST["plz"];
$strasse = $_POST["strasse"];
$hausnummer = $_POST["hausnummer"];
$sql = "INSERT INTO kundendaten (Kundennummer, ProduktID, Vorname,Nachname, Hausnummer, Strasse, PLZ)
Values ('$Kundennummer', '0', '$Vorname', '$Nachname', '$hausnummer', '$strasse', '$plz')";
if(mysqli_query($conn, $sql))
{
echo "DONE";
}
else
{
echo "ERROR: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Learn mysqli_connect() in php
The valid syntex is
mysqli_connect(host,username,password,dbname,port,socket);
You have forgot to add username and password into the mysqli_connect.
Please check below sample.
<?php
$con = mysqli_connect("localhost","mysqli-user","mysqli-password","databank");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
I hope this might be helpful for you to resolve your issue.

PHP MySQL function dies

Ive written my function just to check if the database connection is working.
And it seems like he cant connect to my database, thats no problem, but he dies at the point where i run the function.
function testconnection() {
global $dbhost, $dbuser, $dbpassword, $dbname;
error_reporting(E_ERROR);
$conn = mysql_connect($dbhost, $dbuser, $dbpassword);
$dbconn = mysql_select_db($dbname);
if ( !$conn ) {
return "connfailed";
}
if ( !$dbcon ) {
return "dbconnfailed";
}
}
It stops any further building of the website.
All variables are defined. This function is just used to display an error message if it returns "dbconnfailed".
but even with echo testconnection(); it displays nothing.
can be seen here
but I host this at a big company and on localhost via xampp
it isnt working on one.com but it is working on xampp
You did not add your connection to $dbconn
$dbconn = mysql_select_db($dbname,$dbconn);
Pls Don't use the deprecated and insecure mysql_*-functions. They have been deprecated since PHP 5.5 (in 2013) and were completely removed in PHP 7. Use MySQLi or PDO instead
If you are using mysqli: Try this one..
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, firstname, lastname FROM usertable";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
use mysqli library
function testconnection() {
global $dbhost, $dbuser, $dbpassword, $dbname;
error_reporting(E_ERROR);
$conn = mysqli_connect($dbhost, $dbuser, $dbpassword);
/* check connection */
if (mysqli_connect_errno()) {
return "connfailed " . mysqli_connect_error();
}
$dbconn = mysql_select_db($dbname);
if(!$dbcon) {
return "dbconnfailed " . mysqli_error($dbconn);
}
}

I have sort of php code, in this i want to send connection variable ($conn) into another page so that i can create table for database dynamically

<?php
$a = $_GET['host'];
$b = $_GET['username'];
$c = $_GET['password'];
$d = $_GET['db_name'];
define("localhost",$a);
define("username",$b);
define("password",$c);
define("db",$d);
$conn = mysqli_connect(localhost,username,password);
if($conn === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Attempt create database query execution
$sql = "CREATE DATABASE $d ";
if(mysqli_query($conn, $sql)){
echo "Database demo created successfully";
header("Location:create_table.php");
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
// Close connection
mysqli_close($conn);
?>
In this i need to send $conn with header to send it to create_table.php page.
because i am getting details of connection from user. so i cannot include this file into create_table.php . please help to find out how can i send connection variable into another file.
<?php
$server= "localhost";
$user= "root";
$pass= "";
$conn= mysqli_connection($server, $user, $pass);
if(!$conn){
echo "Database Connection Install Failed ! ".mysqli_errno() ;
}
else {
echo "Database Connection Establiseh Successfully! ";
}
?>

Php mysql create database if not exists

I want to create a database. Why is not the db created with this code?
$dbname = 'regulations_db';
$con = mysql_connect("localhost","root","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_num_rows(mysql_query("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '". $dbname ."'"))) {
echo "Database $dbname already exists.";
}
else {
mysql_query("CREATE DATABASE '". $dbname ."'",$con);
echo "Database $dbname created.";
}
This is working, but I think the first one is the best practice:
if (mysql_query("CREATE DATABASE IF NOT EXISTS regulations_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
Just do a simple mysql_select_db() and if the result is false then proceed with the creation.
As an example, check out the first answer here by another very smart StackOverflower.
<?php
// Connect to MySQL
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Make my_db the current database
$db_selected = mysql_select_db('my_db', $link);
if (!$db_selected) {
// If we couldn't, then it either doesn't exist, or we can't see it.
$sql = 'CREATE DATABASE my_db';
if (mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: ' . mysql_error() . "\n";
}
}
mysql_close($link);
?>
Three steps to fix this:
Don’t specify the database name when connecting.
Your SQL statement should be CREATE DATABASE IF NOT EXISTS php1.
Call mysqli_select_db($link, 'php1') to make that the default database for your connection.
If you're using MySQLi Object-oriented method, you can use following code, this code is similar to previous answer and only the method is different, I just put this because if anyone using MySQLi Object-oriented method, you can use this code directly.
$servername = "localhost";
$username = "mysql_user";
$password = "user_password";
$dbName = "databaseName";
// Connect to MySQL
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// If database is not exist create one
if (!mysqli_select_db($conn,$dbName)){
$sql = "CREATE DATABASE ".$dbName;
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
}else {
echo "Error creating database: " . $conn->error;
}
}
Furthermore you can refer W3school site here.
Good Luck! :D

Categories