Why isn't my php connecting to my server? - php

I'm trying to take and get data from a database to a website using php. When I'm trying to connect I'm using this code:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_errno) echo "failed to connect to database";
?>
but upon trying to open the php file I recieve this on firefox:
connect_errno) echo "failed to connect to database"; ?> (on firefox)
and this on opera/chrome:
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
$db_host = 'localhost';
$db_user = 'root';
$db_pass = '';
$db_name = 'test';
$conn = new mysqli($db_host, $db_user, $db_pass, $db_name);
if ($conn->connect_errno) echo "failed to connect to database";
?>
I have also tried loading it with internet explorer but it simply tries to save it again. I'm not understanding what I am doing wrong.
For extra detail I am trying to do this through xampp using phpmyadmin.
Okay, I fixed it just incase anyone looks back at this essentially the issue was my misunderstanding of the instruction 'open the php'. I was opening it directly to the browser, I needed to open it via localhost.
so localhost/connect.php rather than just opening connect.php to the browser.

1) Make sure the extension of file is ".php"
2) Make sure that you are running page from "http://localhost//" or your local host ip.
You can test your apache/xampp by visiting "localhost" or "localhost:8080" if it is running on 8080 port.

Related

Mysqli error connecting database using mac preinstalled php not with xampp

Though every credentials is correct it shows error connecting database, using macOS i am not using xampp , using pre installed php and only downloaded mysql if i fetch data from database by typing it manually it shows my data to my site but i cannot figure it out why is this happening , here is the code for database., cannot login to system because of this error.
<?php
$db_host = "localhost";
$db_user = "root";
$db_password = "";
$db_name = "dbname";
$conn = new mysqli($db_host, $db_user, $db_password, $db_name);
if($conn->connect_error){
die("connection failed ". mysqli_connect_error());
}
if(mysqli_query($conn, $db_name)){
echo "Database created";
}else{
echo "error creating database";
}
mysqli_close($conn);
?>

Unable to run php mysqli script from ubuntuserver

I got hosted ubuntu server where I got apache2 and phpmyadmin. I wrote small script to connect to my mysql database. I placed that script in /var/www/html/ . When I enter to 193.219.91.103:5858/init.php I just see blank page. I use php 7. I found that I could check if mysqli enabled with php -m | grep mysql it did showed me mysqli.
<?php
$db_name = "AndroidDatabase";
$mysql_user = "dbreader";
$mysql_pass = "dbreader";
$server_name = "localhost";
$port = 3306
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name,$port);
if(!$con)
{
echo "Connection error".mysqli_connect_error();
}
else
{
echo "<h3>Database connection success</h3>";
}
?>
EDIT:
Fixed all i missed was extra argument $port

How to connect MYSQL with PHP

I am stuck after writing all the code, the Website just doesn't connect with mysql at all!
I think this code will help
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
?>
write this code in html file and save it as .php
I hope it works
Create a database in phpmyadmin and then write down below code in a file and save it with .php extension
<?php
$db_username = "db_username"; // Your database login username
$db_password = "db_password"; // Your database login password
$db_name = "db_name"; // The name of the database you wish to use
$db_host = "db_host"; // The address of the database. Often this is localhost, but may be for example db.yoursite.com
$conn = new mysqli($db_host, $db_username, $db_password, $db_name);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
set those 4 variable as per your db details. mysqli_connect_error() will give you error if the connection will not establish

Mysql database connection to PHP

I am trying to connect mysql database to a php program. I used following program to establish connection. But it is not giving any error even I make any mistake with the code.
<?php
// creating database connection
$dbhost ="localhost";
$dbuser = "root";
$dbpass = "1234";
$dbname = "my_new";
//connection
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbname);
//testing connection
if(mysqli_connect_errno()){
die ("database connection failed :".
mysqli_connect_error() .
"(".mysqli_connect_errno().")"
);
}
?>
In my computer, your code is work well. And I find that mysql_connect will not throw exception.

Not able to connect to mysql php/xampp

my I am trying to make login registration page in php.
apache and mysql is up and running.
I am executing the following code
<?php
$db_host = "localhost:777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysql_select_db("$db_name") or die ("no database");
?>
I am using localhost:777 because port 80 is being used by skype.
phpMyAdmin is up and running.
The output i am getting is nothing but the above code only.
can anyone help me on this issue?
Use MySQLi or PDO not MySQL.
You're passing the variables as a string using " " in the mysql_connect function
The syntax is as follows: mysqli_connect(host,username,password,dbname,port,socket);
<?php
$db_host = "localhost";
$db_port = "777";
$db_username = "root";
$db_pass = "";
$db_name = "login";
$conn = mysqli_connect($db_host,$db_username,$db_pass,$db_name,$db_port);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
Try that and it should work. Please make sure you understand the code above and the error you made. Do not simply copy and paste please you will not learn from it.

Categories