<?php
$dbcon=mysqli_connect("localhost","root","","simple_login");
mysqli_select_db($dbcon,"");
?>
以上代码位于db_conection.php
中在另一个文件中,我尝试访问一个名为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>';
}
}
?>
即使在require()之后它也会抛出错误没有选择数据库。请帮忙。
答案 0 :(得分:0)
尝试检查连接并粘贴消息
$dbcon=mysqli_connect("localhost","root","","simple_login") or die ("Connection error!!!");
maeby你需要一个关于mysql服务器连接的传递
以及为什么要将第二行放在连接中?
和1个时刻......如果你包含ore要求带有连接的文件到DB更好的想法是声明DB连接变量就像全局 例如:
$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);
在您的查询中仅使用$ GLOBALS连接到数据库[&#39; connect&#39;];
include(my_conection_file.php);
mysqli_query($GLOBALS['connect'],"AND HERE INPUT YOUR QUERY");
如果你使用mysqli连接,你的查询必须是mysqli而不是mysql ... thas是你的mysqli_connect问题,查询是mysql_query