Error: database not selected

I am stuck with this problem: no database selected. I look through the same problems as here, but after several hours of reading, I cannot understand why the database is not selected. I created a job database and a job table. I am running a script using a WAMP server. Sorry for the "everyday question". Please, help!

 <?php // load Smarty library require('C:/wamp/www/smarty-3.1.21/libs/Smarty.class.php'); $servername = "localhost"; $dbname = "job"; // Create connection $conn = mysqli_connect($servername, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $smarty = new Smarty; $smarty->setTemplateDir('C:\wamp\www\app\templates'); $smarty->setCompileDir('C:\wamp\www\app\templates_c'); $smarty->setConfigDir('C:\wamp\www\app\configs'); $smarty->setCacheDir('C:\wamp\www\app\cache'); $rows = array(); $sql = "SELECT * FROM job"; $result = mysqli_query($conn, $sql); if (!$result) { echo 'MySQL Error: ' . mysqli_error($conn); exit; } while ($row = mysqli_fetch_assoc($result)) { $rows[] = $row; } $smarty->assign('output', $rows); $smarty->display('result.tpl'); mysqli_close($conn); ?> 
+6
source share
3 answers

These are not valid parameters for mysqli_connect . You must pass the host, username, password, and then the database name. You only pass the host name and the database, so you are not connecting correctly.

+13
source

mysqli_connect() requires four parameters, which are: -

host name, user name, password, database name (optional) .

So, you need to provide all these parameters. If you do not, you will not be able to connect, and you will receive errors.

Note: - the database name is optional, you can use mysqli_select_db() to further select the database.

+6
source

Your mysqli_connect () function is missing some parameters, this is how it should be:

 $dbHost = '127.0.0.1'; $dbUser = 'username'; $dbPass = 'password'; $dbDb = 'database'; $conn = mysqli_connect($dbHost, $dbUser, $dbPass, $dbDb); 
+6
source

Source: https://habr.com/ru/post/988266/


All Articles