MySQL connection error in configuration file - php

I want to connect to mysql database using php and following is my configuration file.
<?php
$host = "localhost";
$db = "payroll"
$username ="root";
$password = "";
mysql_connect ($host,$username,$password);
mysql_select_db($db,$username);
?>
but when I run my program it gives me this error:
SQL error: No database selected SQL errno: 1046
SQL: select language, admin from user where username='admin' and password='abc123'
What's wrong with my code?

You forgot a semicolon here
$db = "payroll";
^--- Here
Don't forget to enable error reporting on your code. This is how you do it.
This(mysql_*) extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. Switching to PreparedStatements is even more better to ward off SQL Injection attacks !
Switch to Prepared Statements..
A kickstart example..
<?php
$dsn = 'mysql:dbname=payroll;host=localhost';
$user = 'root';
$password = '';
try
{
$dbh = new PDO($dsn, $user, $password ,array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
Read more here on PHP Manual

$mysqlhost="localhost"; // MySQL-Host
$mysqluser="user"; // MySQL-User
$mysqlpwd="password"; // Password
$connection=mysql_connect($mysqlhost, $mysqluser, $mysqlpwd) or die
("CouldnĀ“t connect");
$mysqldb="database"; // Your Database
mysql_select_db($mysqldb, $connection) or die("Couldnt select database");
I always use this. Here you get every errormessage you need to find your error.

Try this
<?php
$host = "localhost";
$db = "payroll"
$username ="root";
$password = "";
$con = mysql_connect ($host,$username,$password);
mysql_select_db($db,$con);
?>

Related

PDO ODBC MYSQL : i try to connect and it just gives me a fatal error

Im trying to connect to odbc using the following php pdo code
$ligacao = new PDO("odbc:Driver={MYSQL ODBC 8.0 ANSI Driver };Server=localhost;Database=samsic; Uid=root;Pwd='';")
And it just gives me the following error:
Any thoughts on this?
As your database is local, as RiggsFolly said you don't need ODBC.
$db_host = "localhost";
$db_name = "samsic";
$db_user = "root";
$db_pass = "";
try{
$dbh = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'success';
}
catch(PDOException $e){
die('ERROR: ' . $e->getMessage());
}

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
}

Trying to connect sql database in xampp

<?php
$host = 'localhost';
$user = 'root'
$password = '';
$db ='members';
$connection = mysqli_connect("localhost", "user", "password") or die("Unable to connect to the server!");
mysqli_select_db("members", $connection) or die("Couldn't connect to the database!");
I have installed xampp and create database named "members". I tried to connect it to phpmyadmin but didn't work. I try to google all the answers since three days but in vain. Please help me...
<?php
$host = 'localhost';
$user = 'root';
$password = '';
$db ='members';
$connection = mysqli_connect($host,$user,$password,$db);// you can select db separately as you did already
if($connection){
// do all your stuff that you want
}else{
echo "db connection error because of".mysqli_connect_error();
}
Are your credentials for username and password correct?
By default, the localhost has username = root and password as blank.
Also, what's the issue? Is it showing "Unable to connect to the server!"?
You are missing a semicolon after $user = 'root' and you are using a mixture of mysql_ and mysqli_. Also, you could select a table by passing a fourth argument to mysqli_connect()
$host = 'localhost';
$user = 'root';
$password = '';
$db ='members';
$connection = mysqli_connect($host,$user,$password,$db);// you can select db separately as you did already
if($connection){ echo "Connected Successfully";}else{ echo "Error connecting: . mysqli_connect_error()"; }
Use mysqli_ to do queries:
mysqli_query($connection, "INSERT INTO user_login (uname,upassword,email) VALUES ('$uname','$upassword','$email')");
I recommend you to use prepared statements to avoid SQL injection.
So the above query would look like:
$stmt->prepare("INSERT INTO user_login (uname,upassword,email) VALUES (?,?,?)");
$stmt->bind_param('sss', $uname, $upassword, $email);
$stmt->execute();

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");

Transition from mysql to PDO

I'm in the process to learn PDO. I started changing my database connection from this:
$dbname = "database1";
mysql_connect(
':/Applications/MAMP/tmp/mysql/mysql.sock',
'root',
'root'
) or die( mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
to PDO connection:
$username = "root";
$password = "root";
try {
$conn = new PDO('mysql:host=localhost;dbname=database1', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
the connection seems to be working but when I try to login on the website (localhost) it doesn't find my username and password.
Do I need to change all my mysql_query to PDO since I changed the database connection to PDO?
Can I please ask you some reference for a good tutorial to help me during this transition phase?
thanks
Yes you do.
Here is a good PDO tutorial and this one

Categories