I can't connect to phpmyadmin using php - php

I'm obviously new to PHP, and I've been asked by a friend to create a dynamic blog that could use continuously. I wanted it to connect to phpMyAdmin so it would be easier, but when I try to connect with it to my web page, it always fails and my set error appears. I don't know what to look up to help me. I'm sorry if I'm not clear. Post if you want me to go more into detail.
Here's my code:
<?php
$dbhost = 'localhost';
$dbuser = ' root';
$dbpass = '';
$dbname = 'blog1';
$connect = mysqli_connect($dbname, $dbuser, $dbhost, $dbpass) or die ('Cannot connect to database.');
echo "Succesfully connected to database!";
?>

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root123';
$dbname = 'mydb';
$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die ('Cannot connect to database.');
If you run PHPMyAdmin on localhost uncomment in file:
sudo gedit /etc/php5/apache2/php.ini
this line: mysqli.allow_local_infile = On
Restart Apache:
sudo /etc/init.d/apache2 restart or sudo service apache2 restart

Test This
<?php
$dbhost = 'localhost';
$dbuser = ' root';
$dbpass = '';
$dbname = 'blog1';
$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname) or die ('Cannot connect to database.');
echo "Succesfully connected to database!";
The mysqli_connect() was called in wrong way.

Related

i need to connect a php MyAdmin MySQL database using php on MacOSX, but it cannot connect

here is the code that I am using:
$dbhost = "localhost:8080";
$dbuser = "root";
$dbpass = "";
$dbname = "test";
$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
if(! $conn){
die('Could not connect:'. mysql_error());
}
else{
echo "Connected Successfully";
}
?>
when I use include or require_once, I never get the echo "Connected successfully" I always receive the error.
This is for a university project, and in uni the windows computers connect right away, using the same code.
since i am using MacOSX is there anything i need to do?
My server is also running on bitnami MAMP

Cannot change php from localhost details to my servers to connect sql database

I am new to php/sql and i have a login system which runs on my local host using xammp and it all works fine. I now want to upload it to my website but the code no longer works... I have created a sql db on my hosting service and tried to change the code.
the code that is used on the local host is
<?php
$dbServername = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "loginsystem";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
and this is the code that i have got from my hosting.
<?php
$host_name = 'db682827654.db.1and1.com';
$database = 'db682827654';
$user_name = 'dbo682827654';
$password = '<Enter your password here.>';
$conn = mysql_connect($host_name, $user_name, $password, $database);
if (mysql_errno()) {
die('<p>Failed to connect to MySQL: '.mysql_error().'</p>');
} else {
echo '<p>Connection to MySQL server successfully established.</p >';
}
?>
however this brings up an errror message. I have changed the password to the password for my database but its still not connecting.
This is the error message.
Failed to connect to MySQL: Access denied for user 'dbo706265806'#'217.160.62.78' (using password: YES)
Any help would be greatly appreciated
use mysqli_connect (not mysql_connect) like localhost:
<?php
$dbServername = "db682827654.db.1and1.com";
$dbUsername = "dbo682827654";
$dbPassword = "<Enter your password here.=)>";
$dbName = "db682827654";
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);
?>
On your computer you are using mysqli_connect, but on the server you are trying to use mysql_connect. Just use the same file from your computer and simply change $dbServername, $dbUsername, $dbPassword and $dbName to match those that your hosting provided.

MySql connection not closing in php on window server

Our server is facing "Internal Server Error 500" problem and we think it is related to the mysql connection not closing. Mysql connection not closing despite connection close code written in each page.
Below is code the for connection opening & close:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'akaj#32';
$dbname = 'mydb_new';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if($conn) { mysql_close($conn);}
Can any one help?

Can't sql connect to mysql database in nginx (ubuntu 12.04)

I have the following sqlconnection.php
$mysqli = new mysqli('localhost', 'myuser', 'mypassword', 'mydb');
if ($mysqli->connect_error()) {
die('Connect Error (' . $mysqli->connect_rror() . ') '
. mysqli_connect_error());
}
with myuser, mypassword and mydb values being set correctly.
Anyway, although I am able to do operations with phpmyadmin (I got that installed aswell) I can't get to connect to the database. Any idea on why and what exactly I should put in localhost?
Just try this way~
$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'password';
$dbname = 'dbname';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname);
if all your db information is correct, should be working

Storage of database credentials in php.ini or a php file (ex. database_connection.php)?

Is one preferred over another?
And what would be php.ini equivalent syntax to connect to a database the same way this php script does?
database_connection.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
?>
The equivalent syntax would be:
$conn = mysql_connect() or die ('Error connecting to mysql');
There are no required parameters. If you do not provide any, the defaults from php.ini are used.

Categories