How to connect html to Azure database? - php

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

Related

Selecting data from sql database doesn't work. how do I fix it?

I am trying to select data from a database. I do have a successful connection, but it seems like the query doesn't work even though I know for sure that the query is right. What am I doing wrong?
If I execute the code below, the result I get is: "Connected successfullyBad query". The 'Bad query' should mean that the query is wrong, but I checked it and it isn't wrong...
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>
I expect to only see "connected successfully"
You are missing your database name. You can do it two ways, or in the connect statement:
$conn = new mysqli($servername, $username, $password,$database);
Or you can do it in your select statement:
$sql ="SELECT * FROM `yourdatabase`.`producten`";
If you donĀ“t set your database your query is wrong
Your query is right just write your database name in mysqli constructor.
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
Visit: https://www.php.net/manual/en/mysqli.construct.php
Please give database name also, check below code.
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = ""; //Enter database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>

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 just want to connect mysql database from other server

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.

New mysqli () is reset to null when using a function

I am currently working with mySQL and php to create a web application.
In my main index.php file, I have
<?php
require_once('includes/config.php');
require_once('includes/functionName.php');
?>
My config.php is as below:
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "DB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected Successfully!";
?>
When I call the actual function functionName("test");
it goes into the actual functionName.php and goes to
$sql = "UPDATE Table SET id = " . $test1 . " WHERE name = \"". $name . "\"";
echo $sql;
var_dump ($conn);
This returns UPDATE Table SET id = 09 WHERE name = "test" which is correct as an SQL query.
However, my $conn returns NULL, and as a result I get FATAL ERROR query on NULL.
Please tell me why my $conn is returning NULL, and failing my query?
Thank you it was solved after adding line global $conn

php mysql connection get error message

this is my first php code , i am trying to connect to a database with user name = "root" and password = "root"
i have a connection file called dbConnection.php
as following :
<?php
echo "in connection file";
$hostname = "localhost";
$username = "root";
$password = "root";
$db = "ledDB";
echo "<br> db-connection : vars definde ";
//connection to the database
$conn = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
echo "db-connection : initilize connection ";
mysql_select_db($db);
echo "db-connection : connection done";
// Check connection
echo "Connected successfully";
?>
and i call it in a file called what.php :
<?php
echo "Hello";
include "dbConnection.php";
echo "ohhhh";
?>
this returns status code 500 which is internal server error
but i want to know what is the error to fix it how can i get the error message ?
i tried
$conn = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
but it is not returning any thing.
can any one help me please ?
if you are having password on localhost then use password otherwise leave it blank
$con = mysqli_connect("localhost","root","yourpassword","yourdb");
You mysqli_connect() method.
<?php
echo "in connection file";
$hostname = "localhost";
$username = "root";
$password = "root";
$db = "ledDB";
echo "<br> db-connection : vars definde ";
//connection to the database
$conn = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
echo "db-connection : initilize connection ";
mysqli_select_db($conn,$db);
echo "db-connection : connection done";
// Check connection
echo "Connected successfully";
?>
First Thing is to Change your connection method to mysqli or my personal favorite PDO.
PDO Example:
class db extends pdo{
//Website Variables
public $sitedb = '';
public $siteconfig;
public $sitesettings = array(
'host' => 'localhost',
'database' => 'yourdb',
'username' => 'youruser',
'password' => 'yourpass',
);
public function __construct(){
$this->sitedb = new PDO(
"mysql:host={$this->sitesettings['host']};" .
"dbname={$this->sitesettings['database']};" .
"charset=utf8",
"{$this->sitesettings['username']}",
"{$this->sitesettings['password']}"
);
$this->sitedb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
}
$db = new db();
Then you can extend your PDO class to new classes after including the db.php page.
Example Select:
class yourclass extends db {
public function SelectUsers() {
global $db;
$query = <<<SQL
SELECT email
FROM users
WHERE active = :active
SQL;
$resource = $db->sitedb->prepare( $query );
$resource->execute( array (
':active' => 1,
));
$count = $resource->rowCount();
foreach($resource as $user){
$this->email = $user['email'];
}
}
}
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "mydb";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
// make the current db
$db_selected = mysqli_select_db ( $conn , $dbname );
if (!$db_selected) {
die ('Can\'t connect to Database : ' . mysql_error());
}
?>

Categories