MySQL wont connect - php

Im having trouble with MySQLi.
Every time I run this code it returns an error on line 13(mysql_select_bd()).
I cant figure out where the problem is.
Code:
<?php
$conn_error = 'Could not connect';
$mysqli_host = 'localhost';
$mysqli_user = 'root';
$mysqli_password = '';
$mysql_db = 'a_database';
#$mysqli_conn = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_password);
mysqli_select_db('a_database', $mysqli_conn);
?>

You have an incorrect usage of the function:
mysqli_select_db('a_database', $mysqli_conn);
The connection must come first before the database name in the arguments:
mysqli_select_db($mysqli_conn, 'a_database');
// ^ connection object, then database name
Alternatively, you could also do this:
$mysqli_conn = mysqli_connect($mysqli_host, $mysqli_user, $mysqli_password, $mysql_db);
Or the object oriented interface:
$mysqli_conn = new mysqli($mysqli_host, $mysqli_user, $mysqli_password, $mysql_db); // personal preference

Instead of doing that you can do something like this:
$conn = mysqli_connect('localhost', 'root', '', 'a_database');

Related

MySQL error message, mysql_connect(), any way to fix it?

So this is the error message:
PHP Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
This is the affected piece of code:
class wdClient {
var $dbLink; // The database link
var $prefix; // Table prefix
var $script; // The script running
/**
* Construct a new directory object.
*/
function wdClient() {
error_reporting(E_ALL ^ E_NOTICE);
$this->prefix = WDDBPREFIX;
// Connect to the database
$this->dbLink = mysql_connect(WDDBHOST, WDDBUSER, WDDBPASSWD);
// Select the database
mysql_select_db(WDDBNAME, $this->dbLink) or die('Sorry, The site is currently unavailable!');
}
where WDDBPREFIX, WDDBHOST, WDDBUSER, WDDBPASSWD, WDDBNAME are already defined in a config file.
I have tried simply using mysqli_connect instead of mysql_connect but it's not working.
Note: Never use MySQL, use this method!
//MySQLi information
$db_host = "localhost";
$db_username = "username";
$db_password = "password";
//connect to mysqli database (Host/Username/Password)
$connection = mysqli_connect($db_host, $db_username, $db_password) or die("Error " . mysqli_error());
//select MySQLi dabatase table
$db = mysqli_select_db($connection, "table") or die("Error " . mysqli_error());
Good luck!
well, as pointed in here http://php.net/manual/en/function.mysqli-connect.php , you should make something like this:
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
Apparently, in your case it will look like this:
$link = mysqli_connect(WDDBHOST, WDDBUSER, WDDBPASSWD, WDDBNAME);
And then you can continue with your code...
if (!link){
die('Sorry, The site is currently unavailable!');
} else{
// write your SQL here and fetch it
}

How can I connect PHP to Bluemix database?

I have an php file, and I want to connect it to Database
usually I use localhost but I never use php in Bluemix
what should I fill these attributes ?
$DB_HOST = 'localhost'; // what should I write instead of
$DB_USER = '';
$DB_PASSWORD = '';
$DB_DATABASE = 'MyDataBase';
please help
First: You Create a folder .bp-config/options.json in the parent folder
and add below line, of code in the options.json folder, so sqli connect will working.
{
"PHP_EXTENSIONS": ["mysqli"]
}
Now make a connection:
$DB_HOST = '127.0.0.1:3307'; // insert ip with port number
$DB_USER = 'user';
$DB_PASSWORD = 'password';
$DB_DATABASE = 'dbname';
$mysqli = new mysqli($DB_HOST,$DB_USER,$DB_PASSWORD,$DB_DATABASE);
$result = $mysqli->query(" SELECT * FROM table_name ");
$row = $result->fetch_assoc();
Create your database service on Bluemix and BIND it to your application. Go to your applications dashboard and you should see the database service instance below. Click on Show Credentials.
However, the right way to do this is to programmatically get the credentials by parsing the VCAP_SERVICES environment variable. See this example:
https://github.com/IBM-Bluemix/php-mysql See db.php
You have to parse db information from VCAP_SERVICES.
If your database service is "SQL Database" you can connect to SQLDB using this example code:
//parse VCAP_SERVICES Environment variable
$vcap_services = $_ENV["VCAP_SERVICES"];
$services_json = json_decode($vcap_services,true);
$sqldb = $services_json["sqldb"];
if (empty($sqldb)) {
echo "No sqldb service instance is bound. Please bind a sqldb service instance";
return;
}
//Get Credentials object (db,host,port,username,password)
$sqldb_config = $services_json["sqldb"][0]["credentials"];
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=".
$sqldb_config["db"].
";HOSTNAME=".
$sqldb_config["host"].
";PORT=".
$sqldb_config["port"].
";PROTOCOL=TCPIP;UID=".
$sqldb_config["username"].
";PWD=".
$sqldb_config["password"].
";";
$conn = db2_connect($conn_string, '', ''); //db connection

Connect PHP with mysql database using mysql_connection

i need to know how to declare the database connection using mysql_connection in php
and this is sample of code
$password = "";
$localhost = "localhost";
$username = "root";
$this->connection = mysql_connect($this->$localhost,$this->$username,$this->$password);
Note i am using wamp server
Use mysqli instead. Start like this:
$db = new MySQLi("host", "username", "password", "db");
if(!$db)
{
die("your_error_msg" . mysqli_error($db));
}
$db->set_charset("utf8");

Cannot add values to table using mysql and php

I have a problem with my code. I'm trying to add new post to the table events. I'm confused because I have used this code in other place on the same website (but it was using mysqli_query to register new user). mysqql_error returns "No database selected"
This is the code:
<?php
$add_title = $_POST['add_title'];
$add_happen = $_POST['add_happen'];
$add_created = date('Y-m-d');
$add_content = $_POST['add_content'];
$add_author = $_POST['add_author'];
//connect to
//localhost
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "zhp2";
$db_con = mysql_connect($db_host, $db_username, $db_password, $db_dbname);
$query = "
INSERT INTO events ( title, happen, created, content, author )
VALUES ( '$add_title', '$add_happen', '$add_created', '$add_content', '$add_author') )
";
$retval = mysql_query($query, $db_con);
if(! $retval ){
die('Could not enter data: ' . mysql_error());
}
else{
echo "Entered data successfully\n";
}
mysql_close($db_con);
//header('Location: ../../index.php?link=events');?>
I've tried to fix it using trial and error method playing with different combinations both mysql_query and mysqli_query
You are confusing mysql_connect and mysqli_connect functions in the way you pass those parameters. In your example:
$db_con = mysql_connect($db_host, $db_username, $db_password, $db_dbname);
you are passing a fourth parameter which is the database name but that wont work as you should only pass the three first (host,username,password) and then call mysql_select_db():
$db_con = mysql_connect($db_host, $db_username, $db_password);
mysql_select_db( $db_dbname, $db_con );
In mysqli which is the BETTER way of doing it since mysql_ functions are very vulnerable and being deprecated from php you could pass four elements like here:
$db_con = mysqli_connect($db_host,$db_username, $db_password, $db_dbname) or die("Error " . mysqli_error($link));
which is close to what you are trying to do, but in a correct mysqli_ way.
Well then, you need to select the database! ;) The fourth parameter of mysql_connect() is not the database name. You need to do this separate of connecting to the MySQL server.
Using mysql_select_db() function:
$db_host = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "zhp2";
$db_con = mysql_connect($db_host, $db_username, $db_password );
mysql_select_db( $db_dbname, $db_con );
And of course all the obligatory warnings about SQL injection, sanitizing your data, deprecation of mysql_* functions.
You need to select which database to connect to using the mysql_select_db function:
// make $db_dbname the current db
$db_selected = mysql_select_db($db_dbname, $db_con);
if (!$db_selected) {
die ("Can't use $db_dbname : " . mysql_error());
}
See the PHP manual for more info: http://php.net/manual/en/function.mysql-select-db.php

Mysql connect = No database selected? then Access denied

I am working on a php mysql connect script.
I wanted it to use functions so I can keep a track what is what, so Mysql connect got one function.
When I run it, I get first "No database selected" and when I specify it manually, it says "Access denied for # localhost".
Code
<?php
/* Mysql Data */
$MySqlUser = "root";
$MySqlPass = "**********";
$MySqlHost = "localhost";
$MySqlDataBase = "serveradmin";
/* End Mysql Data */
function MySqlConnect() {
$Connect = mysql_connect($MySqlHost, $MySqlUser, $MySqlPass);
$Database = mysql_select_db($MySqlDataBase);
if (!$Connect | !$Database) {
die("Cannot connect ".mysql_error());
}
}
MySqlConnect()
?>
So the problem, what causes this? I want the script to be nice and clean, and not sure if function() causes it.
This will fix it, but please look into PDO or mysqli
<?php
/* Mysql Data */
$MySqlUser = "root";
$MySqlPass = "**********";
$MySqlHost = "localhost";
$MySqlDataBase = "serveradmin";
/* End Mysql Data */
function MySqlConnect($MySqlUser, $MySqlPass, $MySqlHost, $MySqlDataBase ) {
$Connect = mysql_connect($MySqlHost, $MySqlUser, $MySqlPass);
$Database = mysql_select_db($MySqlDataBase);
if (!$Connect | !$Database) {
die("Cannot connect ".mysql_error());
}
return $Database;
}
$Database = MySqlConnect($MySqlUser, $MySqlPass, $MySqlHost, $MySqlDataBase );
?>

Categories