-1

I have been stuck for the past week trying to learn MySQL on my computer. I have a database called users and a table called users with data in the table. But I have this PHP code below and it will not get anything for results and I don't know why.

<?php

//Connect to database
$link = mysqli_connect('localhost', 'root', '');

//Detect if it was unable to connect to database
if (mysqli_connect_error()) {
    die('Unable to connect to database.');
}



//Works?
$query = 'SELECT * FROM users';

//Not working...
if ($result = mysqli_query($link, $query)) {
    echo 'It Works!';
}

?>
Dharman
  • 26,923
  • 21
  • 73
  • 125
Amy N
  • 1
  • 1
  • you login into the mysql server with root user but doesn't specify which database to query on – Fnr Sep 12 '18 at 17:01
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code – tadman Sep 12 '18 at 17:01
  • Note: Try and get out of the habit of declaring SQL statements in throw-away variables that are used only once. It's a lot easier to follow code where the query is supplied directly to the function, and there's no longer a chance of messing up and sending in `$sql3` instead of the visually similar `$sql8`. – tadman Sep 12 '18 at 17:01

1 Answers1

1

You are not selecting the database.

mysqli_connect("localhost", "root", "my_password", "users");
Pedro Antônio
  • 412
  • 1
  • 6
  • 18