Nikolay Nikolay -5 years ago 226
SQL Question

error: Database not selected

I'm stuck with this issue: No database selected. I roll over the same problems posted here, but after hours of reading I can't figure out why the database is not selected. I created a database

and a table
. I run the script with WAMP server. Sorry about the "everyday question." Please help!


// load Smarty library

$servername = "localhost";
$dbname = "job";

// Create connection
$conn = mysqli_connect($servername, $dbname);

// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());

$smarty = new Smarty;


$rows = array();
$sql = "SELECT * FROM job";
$result = mysqli_query($conn, $sql);

if (!$result) {
echo 'MySQL Error: ' . mysqli_error($conn);

while ($row = mysqli_fetch_assoc($result)) {
$rows[] = $row;

$smarty->assign('output', $rows);



Answer Source

Those aren't the right parameters to mysqli_connect. You have to pass host, username, password, and then database name. You are passing only the host and the database name, so you are not connecting correctly.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download