Need help connecting PHP to MySQL database - php

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

Related

How to fix error in dbconn file when upload php mysql project to free webhosting site?

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

How do I display different images using MySQL/PHP when database connected/not connected?

I'm new to PHP and MYSQL, and I have what I think is going to be a relatively easy question.
My objective is to show one image (a green flashing light) when the database is connected, and display another image (a red flashing light) when there is no database connection.
I imagine it should be a simple variation on this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
But if I attempt to add an image to where it echos "Connected successfully" I receive an error.
I'm attempting to add the image like this:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("<img src="Red_Light.gif" style="width:10px;height:10px;"> " . $conn->connect_error);
}
echo "<img src="Green_Light.gif" style="width:10px;height:10px;">";
?>
I probably have the completely wrong syntax but any help is appreciated.
Many thanks,
Leif
You can use Janno answer or you may use this (by changing " to ':-
if ($conn->connect_error) {
die("<img src='Red_Light.gif' style='width:10px;height:10px;'> " . $conn->connect_error);
}
echo "<img src='Green_Light.gif' style='width:10px;height:10px;'>";
if ($conn->connect_error) {
die("<img src=\"Red_Light.gif\" style=\"width:10px;height:10px;\"> " . $conn->connect_error);
}
echo "<img src=\"Green_Light.gif\" style=\"width:10px;height:10px;\">";
Whole problem seems to be related to not escaping the quote marks.

cant show the results of query in php from mysql

Hello I am new at php and mysql and I don't know what is wrong.
I cant show the results from query and the connection with mysql is successfully connected.
I don't use wampserver I just install php,mysql and Apache separately.
Thanks in advance.
Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql="select * from `books`;";
$result=mysqli_query($conn,$sql);
if (!$result){
echo "query cannot execute";
};
?>
its only show me "query cannot execute"
You need to pass fourth parameter database name in mysqli_connect()
It would be
$conn = mysqli_connect($servername, $username, $password,"YOUR_DATABASE");
Read http://php.net/manual/en/mysqli.error.php to check error in query.
Read http://php.net/manual/en/mysqli-result.fetch-array.php
To fetch data from query result

Getting 500 Error when trying to access localhost my_sql 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";
`

PHP script cannot access database in sql

I am trying to connect to a MySQL database through a php script. It gives me this error from mysql_error(): Access denied for user '#localhost' to database 'userinfo'
userinfo is the database.
my script is this
<?php
$servername = "localhost";
$username = "root";
$password = "'mm'";
$database = "userinfo";
$conn = mysqli_connect($servername, $username, $password);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully<br>";
mysql_select_db($database) or die(mysql_error());
echo "connected successfully to db:" . $database . "<br>";
?>
you are connecting using
mysqli_
function
and selecting data with
mysql_
avoid using both at the same time. since they're incompatible.
use the mysqli_ alternative instead
mysqli_select_db($conn, $database);
and
mysqli_error($conn)
Please keep in mind this isn't the safest way. But since you have said your learning this it is a start.
<?php
$servername = "localhost";
$username = "root";
$password = "mm";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
http://www.w3schools.com/php/php_mysql_connect.asp
To select data from the database
http://www.w3schools.com/php/php_mysql_select.asp
It appeared you where combining the old mysql in php with the new mysqli

Categories