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

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

Related

MySQL and PHP connection failing

I have an issue with PHP script not connecting to MySQL database. Below code always generates error:
$dbconn = #mysql_connect('SFDC1', '<username>', '<password>');
if (!$dbconn)
{
die('Error connecting to DB!');
}
There is no issue if I connect to the database using MySQL workbench with same credentials. Issue only occurs during the communication between PHP and MySQL.
Any help on debug of this issue?
Try this one;
$dbhost = "localhost";
$dbuser = "username";
$dbpass = "pass";
$dbname = "DBname";
if(!#mysql_connect($dbhost,$dbuser,$dbpass)) {
echo "Cannot connect";
die();
} else
mysql_select_db($dbname);// or report();
Use this code to connect mysql.
<?php
$dbconn = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$dbconn ) {
die('Could not connect: ' . mysql_error());
}
?>

I can't connect to phpmyadmin using 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.

Issue with PHP (Warning: PDO::__construct() )

Hello guys,
I know this might seem to be a duplicate question but i am having an issue connecting to my database with PDO. I tried interchanging the server name from localhost to '127.0.0.1' but to no avail i have gained success. I am here testing and trying to create my own database for my own personal use and to get an understanding of how querying a database works, but my lack of knowledge brought me here. Can someone just point me in the right direction.
a snippet of my code is below
<?php
require_once('CONFIG.php');
session_start();
try {
$database_handler = new PDO('mysql:host=' . $databasehost . 'houses', 'root', '');
}
catch (PDOException $e) {
print "Error: " . $e->getMessage();
}
?>
So i have my Config.php file which has
<?php
$databasehost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'house';
$conn = mysql_connect($databasehost , $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'house';
mysql_select_db($dbname);
?>
Any help would be greatly appreciated
The format of your DSN is wrong
<?php
require_once 'CONFIG.php';
session_start();
try {
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8', $databasehost, $dbname);
$database_handler = new PDO($dsn, $dbuser, $dbpass, array(
PDO::ATTR_EMULATE_PREPARES=>false,
PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
}
catch (PDOException $e) {
print "Error: " . $e->getMessage();
}
and drop the lines
$conn = mysql_connect($databasehost , $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'house';
mysql_select_db($dbname);
from your Config.php

Open Shift Mysql data connection using php

I am fresher for Open shift.
Here is my problem in Open shift.
I completed creating application with php, Mysql and PhpMyAdmin
Then I went to phpMyAdmin and created a new database 'Students_list'
Now I am unable to connect to my database with the following code in php pages.
$db_host = $_ENV['127.12.123.2']; //sample host
$db_user = $_ENV['adminnxUpXA6'];
$db_pass = $_ENV['lyPfbtHYpDFcU'];
$db_name = $_ENV['students_list']; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
May I please know where I am comitting the error.
Try this instead:
$db_host = getenv('OPENSHIFT_MYSQL_DB_HOST'); //sample host
$db_user = getenv('OPENSHIFT_MYSQL_DB_USERNAME');
$db_pass = getenv('OPENSHIFT_MYSQL_DB_PASSWORD');
$db_name = 'students_list'; //this is the database I created in PhpMyAdmin
$db = new mysqli($db_host, $db_user, $db_pass);
if ($db->connect_errno) {
die('Connect Error (' . $db->connect_errno . ') '
. $db->connect_error);
}
mysqli_select_db($db,$db_name);
You are using $_ENV, which contains environment variables from the operating system. Most probably what you wanted to do is this:
$db_host = 'ip';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'students_list';
The structure for the mysqli connection string is this:
mysqli(Hostname,Username,Password,DatabaseName);

php syntax database connect

I'm trying to learn this stuff. Please be gentle.
Something is wrong here:
$dbhost = "localhost";
$dbname = "something_dbname";
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ($dberror1);
$select_db = mysql_select_db($dbname . $dbconnected) or die ($dberror1);
where is my mistake? I want $dbconnected to show...
I can just as easily use
echo "hello";
it shows that I connect but I'm trying to get familiar with using multiple variables.
would this be better?
$dbhost = "localhost";
$dbname = "something_dbname";
$dbuser = "something_user";
$dbpass = "pwpwpwpw";
$dberror1 = "Could not connect to the database!";
$dbconnected = "you are connected!";
if ($mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname))
echo $dbconnected;
else die($dberror1);
Right now you are trying to connect to a database called something_dbnameyou are connected. The . concatenates variables into one string.
To fix your immediate problem, try this:
First, define $dbhost - I don't see it in your code.
Then change the last line to this:
$select_db = mysql_select_db($dbname) or die ($dberror1);
Then, just echo $dbconnected;
If you are not connected, the page will have called die, and will never reach the line that echos $dbconnected. If you are connected, the program will proceed to this next line and echo your success message.
Or you can do it more explicitly like this:
if ($select_db = mysql_select_db($dbname))
echo $dbconnected;
else die($dberror1);
To fix the bigger problem, DON'T use mysql_*. Read this for more information.
mysqli or pdo are far better options, and you can accomplish the same task easier, for instance, connecting to a db with mysqli is just:
$mysqli = new mysqli($dbhost, $dbuser, $dbpassword, $dbname);
Or you can do it procedural style, which is closer to your current code. The following snippet is from the php manual, on the page I linked in the comment below.
$link = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
if (!$link) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
echo 'Success... ' . mysqli_get_host_info($link) . "\n";
mysqli_close($link);
I'd strongly recommend using PDO. The connection string is similar and can be done using:
// I do not see $dbhost defined in your code. Make sure you have it defined first
try {
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo $dbconnected; // will print out the connection success message
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
To answer your question about not using mysql_* functions, you can check out this

Categories