code:

I am trying to debug the code, I can't understand why (or how) the same error is being thrown despite the if statements, dies, and different mysql_queries. I don't get it.

<?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();
?>
Member Avatar for LastMitch

@dancks

PHP4:'Database not slected error' being thrown with no die(mysql_error()) ?

Can you provide the error of the line?

I can't provide a line number. All I get is

No database selected

as the webpage.

I would've thought

die(sprintf("Cannot use database %s.",$db));

Would've executed if that was the issue. But its not. I think the error message is misleading or false and something else is going on.

I am using a student account on a server, the mysql account and database were made for me since I don't have permission, but I can verify that I know how to use a mysql database, use insert into, use select statements, etc. If its truly an issue of there is no connection why aren't the if statements executing? That's my main question.

Member Avatar for LastMitch

@dancks

I am using a student account on a server, the mysql account and database were made for me since I don't have permission, but I can verify that I know how to use a mysql database, use insert into, use select statements, etc. If its truly an issue of there is no connection why aren't the if statements executing

In order for the query to function it has to be connected to a database.

The query only works if you have a database.

The query function is to get the info from your database at the same time it update the database and save the info on the database at the same time

I am using a student account on a school server. The mysql account and database were made for me since I don't have permission. I know how to do insert into and select statements. I briefly looked at your link and the code is almost a spitting image of mine, at least at the beginning. Something else is happening here I can't figure out what. Do you have any ideas how I can further debug my code to detect any failure of the php code itself? why are the if and die statements being bypassed?

Member Avatar for LastMitch

The mysql account and database were made for me since I don't have permission.

I can't help you with that.

Something else is happening here I can't figure out what. Do you have any ideas how I can further debug my code to detect any failure of the php code itself?

No, the only way to make it work is to have a database!

You need to connect to the database for your code to work.

Do you know someone or anyone beside the school who has access or have to a database so you can test it out?

why are the if and die statements being bypassed?

It's not working at all

ok I read the error message for the wrong page. I misspelled the database name. The above code is ok. oops.

Member Avatar for LastMitch

@dancks

ok I read the error message for the wrong page. I misspelled the database name. The above code is ok. oops.

So everything is fine? I glad you find the error! Can you click Mark Question Solve on the bottom left corner so the thread is close and solve so noone can add stuff to it. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.