未选择错误数据库 [英] Getting error database not selected

查看:117
本文介绍了未选择错误数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

<?php
session_start();
$_SESSION['msg'] = "";
$con = mysql_connect('localhost','me','omglol');
mysql_select_db('test',$con);
$q = mysql_query(sprintf("select * from UserTable where (nick=\"%s\") AND (pass=SHA1(\"%s\"))",$_POST['nick'],$_POST['pass']),$con) or die(mysql_error());

在我看来,这很正确.是的,我知道测试"存在.并包含UserTable.

This looks right to me. And yes I know 'test' exists. And contains UserTable.

首先,感谢rid将php4添加到我忘记的标签中:(

First, Thanks rid for adding php4 to the tags I forgot :(

根据Laser_wizard的建议,我执行了以下操作:(整个代码):

As per Laser_wizard's recommendations I did the following: (Entire Code):

<?php
session_start();
$_SESSION['msg'] = "";
$con = mysql_connect('localhost','me','omglol');
if(!$con)
{
    die("The connection to mysql server is not being made.");
}
$db = 'test';
$selected = mysql_select_db($db,$con);
if(!$selected)
{
    die(sprintf("Cannot use database %s.",$db));
}
//$q = mysql_query(sprintf("select * from UserTable where (nick=\"%s\") AND (pass=SHA1(\"%s\"))",$_POST['nick'],$_POST['pass']),$con) or die(mysql_error());
$q = mysql_query("select * from UserTable",$con) or die("The query statement still isn't working");
$row = mysql_fetch_assoc($q);
$dest=0;
if(mysql_num_rows($q)==0)
{
    //$testn = mysql_query(sprintf("select * from UserTable where nick=(\"%s\")",$_POST['nick']),$con);
        $testn = mysql_query("select * from Category",$con) or die("The 2nd query statement still isn't working");
        if(mysql_num_rows($testn)==0)
        {
               $_SESSION['msg'] = "Nick ".$_POST['nick']." was not found. Check spelling or <a href=\\\"register.php\\\">register</a>";
        }
        else
        {
                $_SESSION['msg'] = "Password incorrect";
        }
        if(isset($_SESSION['attempts']))
        {
                $_SESSION['attempts'] = $_SESSION['attempts'] + 1;
        }
        else
        {
                $_SESSION['attempts'] = 1;
        }
    mysql_free_result($q);
    mysql_free_result($testn);
        mysql_close($con);
    $dest = 'Location:http://cs4.sunyocc.edu/~me/onestopshop/login.php';
}
else
{
        $_SESSION['nick'] = $_POST['nick'];
    $_SESSION['email'] = $row['email'];
    mysql_free_result($q);
    mysql_close($con);
    $dest = 'Location:http://cs4.sunyocc.edu/~me/onestopshop/index.php';
}
header($dest);
exit();
?>

与上面相同的错误.因此,设置了$ con,而$ selected读取为true,所以我很困惑接下来要检查的内容.我猜mysql_select_db($ db,$ con); $ testn还是不能正常工作,但仍然是真实的吗?我很困惑下一步该怎么做.

Same error as above. So $con is set and $selected reads true, so I'm confused what to check next. I'm guessing mysql_select_db($db,$con); Nor is $testn is still not working but still reading true? I'm confused what to do next.

推荐答案

使用一些die语句来测试连接并确保已建立连接. 除此之外,我还想注释掉您的查询行以查看是否引起问题.

Throw in some die statements to test the connection and make sure it's setting up. Other than than that I'd say to comment out your query line to see if that's causing a problem.

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

这篇关于未选择错误数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆