Marcel
Marcel

Reputation: 1191

MySQL "no database selected"

I am using the following code to connect to my database. I get a no database selected error.

    require '../dbconnect.php';

session_start();

$username_input = $_POST['username_input'];
$password_input = $_POST['password_input'];

$sql = "SELECT id, username, password, salt FROM users WHERE username = '$username_input' LIMIT 0, 30";
$query = mysqli_query($dbcon, $sql);

printf("Error: %s\n", mysqli_error($dbcon));
exit();

    $row = mysqli_fetch_row($query);
    $user_number = $row[0];
    $username = $row[1];
    $password = $row[2];
    $salt = $row[3];

I know similar questions have been asked in the past, but looking through those, I could not find my problem.

Upvotes: 1

Views: 4008

Answers (2)

if you are using one single database most of the time, it's better to supply your database name in your mysql_connect as follows:

$link = mysqli_connect("hostname_or_ip","user","passw","db_name") or die("Error " . mysqli_error($link)); 

Upvotes: 1

t j
t j

Reputation: 7294

You are trying to query MySQL without selecting a database. To fix this you need to specify which DB your are connecting to by using the USE statement before executing any queries.

$query = mysqli_query($dbcon, 'USE mydatabase');

Upvotes: 1

Related Questions