Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to connect to databse on my server by using this code:
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
But code after $conn = new mysqli($servername, $username, $password); is not executing. When i try to echo anything after it I do not get output. Code before that line works as expected. I do not get any errors from php.
I am not sure what is problem. Could it be something server related? I did try to add:
ini_set('display_errors',1);
error_reporting(E_ALL);
but it didn't help, no errors have been displayed.
I have been trying many things from stackoverflow (and other places) but they didnt help, some examples:
Connecting to a mysql database from php, error but no error shown?
Connection of MySQL with PHP not working
There are multiple ways to handle errors
Simplest of all, use try catch while connecting
<?php
$username = "username";
$servername = "localhost";
$password = "password";
echo "Before connection";
// Create connection
try {
$conn = new mysqli($servername, $username, $password); } catch(\Exception $e) { var_dump ('oopss... this is the error', $e)}
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected
Related
I've been trying to upload my PHP MySQL(in Dreamweaver) project to a free web-hosting site.
When I logged in, there is an error that appear in dbconn.php file.
The error is shown below:
and here's the code in my dbconn.php file:
<?php
/* php& mysqldb connection file */
$user = 1350048; //mysqlusername to db
$pass = "password"; //mysqlpassword to db
$host = "eskl.freeoda.com"; //server name or ipaddress
$dbname= 1350048; // db name in server freeoda
$dbconn= mysql_connect($host, $user, $pass);
if(isset($dbconn)){
mysql_select_db($dbname, $dbconn) or die("<center>Error: " . mysql_error() . "</center>");
}
else{
echo "<center>Error: Could not connect to the database.</center>";
}
?>
I would really appreciate if anyone can teach me how to solve this.. thanks in advance!
As Kerbholz already stated, don't use mysql_* functions, they are really outdated.
Instead use mysqli:
$servername = "eskl.freeoda.com";
$username = "1350048";
$password = "password";
$database = "1350048";
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
For your error it got mostly something to do your host doesn't allow remote connections. Try to change the serverhost to localhost or 127.0.0.1
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 6 years ago.
I've installed xampp and it seems to be up and running, I have tested php by executing the phpinfo() function and it works. I can create databases and manipulate them in phpmyadmin, and the localhost server works too
How ever when I attempt to actually connect through php....
<?php
$conn = mysqli_connect('localhost', 'root', '');
mysql_select_db("testskills");
if(!$conn) {
die("Connection Failed: ". mysqli_connect_error());
}
..... I don't get any errors but the code breaks and the browser just shows me the actual code from the file the form action called
I'm stumped
Lori
Try this
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
<?php
$host_name = "localhost";
$u_name = "root";
$u_pass = "";
try {
$dbh = new PDO("mysql:host=$host_name;dbname=personal_db", $u_name, $u_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $ex) {
echo "Connection failed: " . $ex->getMessage();
}
?>
I would suggest that you start learning to use PDO man.
I am building a website for a friend and he would like some statistics on the website. The statistics are on a dedicated MySQL server, and the website is on another. I have been trying to keep linking the database to the website but it keeps failing. I am using PHP to attempt this.
The code I have been trying to use is here:
<?php
$servername = "ns303998.ip-x-x-x.eu";
$username = "x";
$password = "x";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
The database name is the issue. Any ideas about this?
mysqli_connect takes 4 arguments, not 3 as you are trying. The fourth one is database name. Documentation is here.
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
Edit: The argument is optional, but if it's is a shared hosting, your credentials are probably working for the one specific database, not the whole server. So specifying the database could help you with this issue.
mysqli_connect needs database name to execute
<?php
$servername = "ns303998.ip-x-x-x.eu";
$username = "x";
$password = "x";
$dbname ="x";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
use
$dbname='yourdbname';
mysqli_select_db($conn,$dbname);
to select the database
I have a small page that I'm trying to building a journal with using PHP and My_SQL for both practice and recreation. I have a solid understanding of PHP but less so on My_SQL. Based on what I viewed it seemed that I can implement it almost like looking up a function and applying data to the corresponding areas. I tried doing the most basic thing and connect to my localhost server, but I cannot figure out why I am getting a 500 error. What is wrong with my server???
$servername = "http://127.0.0.1/";
$username = "root";
$password = "***";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else echo "Connected successfully";
`
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How do I connect to a MySQL database in PHP?
Database name: NewDB1
Username : Mark
Password : secret
I have googled a few solutions, but none of them seem to work.
<?php
$servername = "localhost";
$username = "Mark";
$password = "secret";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
With PDO -
<?php
// turn on error reporting and display
error_reporting(E_ALL);
ini_set('display_errors', 1);
// define the user
define('USER', 'Mark');
define('PASS', 'secret');
// establish database connection
try {
$dbh = new PDO('mysql:host=localhost;dbname=NewDB1', USER, PASS);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // make sure to turn on error reporting
}
catch(PDOException $e) {
echo $e->getMessage(); // show any error messages.
$errorCode = $e->getCode();
}
?>
This comes from a more detailed tutorial on how to understand and simplify PDO.