Mysqli No database selected Error

浪尽此生 提交于 2020-12-27 07:16:06

问题


<?php

$dbcon=mysqli_connect("localhost","root","","simple_login");

mysqli_select_db($dbcon,"");

?>

The above code is in db_conection.php

In another file i try to accesss a table known as school

if(!$_SESSION['username'])
{

    header("Location: index.php");//redirect to login page to secure the welcome page without login access.
}
else
{
$user=$_SESSION['username'];
     require('db_conection.php');
    $ex0=mysql_query("SELECT * FROM school WHERE uid='$user'") or die(mysql_error());
                $count=mysql_num_rows($ex0);
                if($count)
                {
               echo '<script>';
               echo 'alert("Profile there");';
               echo '</script>';
               }
 }
?>

Even after require () it throws error No database selected. Please help.


回答1:


try to check a connection and paste a message if die

$dbcon=mysqli_connect("localhost","root","","simple_login") or die ("Connection error!!!");

maeby you need a pass for conection on mysql server

and why you put the second row in your conection?

and 1 moment... if you include ore require thefile with conection to DB better idea is declare the DB conection variable like global example:

$host = "localhost";
$user = "root"; 
$pass= "";
$db = "simple_login";
$GLOBALS['connect'] = mysqli_connect($host, $user, $pass, $db) or die ("Connection error!!!");
mysqli_set_charset ($GLOBALS['connect'], "utf8" );
unset($host,$root,$pass,$db);

after in your query connect to db only with $GLOBALS['connect'];

include(my_conection_file.php);
mysqli_query($GLOBALS['connect'],"AND HERE INPUT YOUR QUERY");

and if you conected with mysqli your querys must be mysqli and not mysql... thas is your problem conetion with mysqli_connect and query is mysql_query



来源:https://stackoverflow.com/questions/33482918/mysqli-no-database-selected-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!