php - MySQL connection error in configuration file

276

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?

777



578
votes

Answer

Solution:

$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.

375

Answer

Solution:

Try this

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

People are also looking for solutions to the problem: Sending errors to syslog in php-fpm

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.