问题
<?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