PHP script cannot access database in sql - php

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

Related

MYSQL Database connection for php

I am using my website database with php mysql_connect but my hosting provider is saying I need to use mysqli_connect.
When I edit connection file and update change method its not working with mysqli.
Currently using this code:
MSQL Method:
$con=mysql_connect("localhost","username","password");
mysql_select_db("databasename");
and for mysqli I am using this code but fail
MYSQLI Method:
$con = mysqli_connect("localhost","my_user","my_password","my_db");
Please help. Why is mysqli not working? Is my method wrong?
Try this:
Example (MySQLi Object-Oriented)
<?php
$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);
}
echo "Connected successfully";
?>
Example (MySQLi Procedural)
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>

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.

Need help connecting PHP to MySQL database

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

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

I want to connect with my: online MySQL database, but I get this error. Can someone help me?

I want to make connection to my online database, but then I get this error:
Warning: mysqli :: mysqli (): ( HY000 / 2002 ): A connection attempt failed because the " Related party Incorrectly If the answer after a certain time , of the costs Connection failed because " the Confederate host has not responded . C: \ wamp \ www \ array Add in db \ 222.php on line 8
This is my code:
?php
$servername = "db.tapdeleest.nl"; $username = "***"; $password = "***"; $dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "nice";
}
?>
Thanks for helping!
Using new mysqli() you don't add the database name in your connection.
You use mysqli_select_db($conn, $dbname)
<?php
$servername = "db.tapdeleest.nl"; $username = "***"; $password = "***"; $dbname = "***";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
else
{
echo "nice";
mysqli_select_db($conn, $dbname);
}
?>
Just advice: Give pdo a try, I feel like pdo is easier.
http://www.w3schools.com/php/php_mysql_connect.asp

Categories