Page 1 of 1

No database selected

Posted: Mon May 05, 2008 12:00 am
by tabatsoy
Please help!!!

when i run my program it say's that "No database selected"
here is my code:

Code: Select all

<?php 
    session_start();
    $db = mysql_connect("localhost") or die(mysql_error() . mysql_errno());
        mysql_select_db("examination",$db);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Admin Login</title>
<link href="Layout.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
-->
</style></head>
 
<body>
<table width="800" border="0" align="center">
  <tr>
    <td colspan="2"><div align="center"><img src="images/logo.jpg" width="800" height="200" />
    </div></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center"> <span class="session"><br />Log in as an Administrator <br /></span><br />
    </div></td>
  </tr>
  <tr></tr>
  <tr>
    <td colspan="2"><div align="center">
      <?php
    $var1 = $_POST["uname"];
    $var2 = $_POST["pword"];    
    if($var1 && $var2){
            $db = mysql_connect("localhost") or die(mysql_error() . mysql_errno());
        mysql_select_db("examination",$db);
 
        $result = mysql_query("SELECT *
                                FROM admin
                                WHERE username = '".$var1."'
                                AND password = '".$var2."'")or die(mysql_error());
        if(mysql_num_rows($result) > 0){
            $_SESSION['loggedin'] = $var1;          
            echo '<span class = "style1"><strong>Welcome, </strong></span><font color="#003399" face="verdana" size="3" style="text-transform: capitalize"><strong>'.$var1 .".</strong></font><br><br>";
            echo "<a href ='admin.php'><div align = 'center' class = 'session'><font size = '1'>Click here to go to Administrator's Main Page</font></div></a><br>";
            echo "<a href='logout.php'><div align = 'center' class = 'session'><font size = '1'>click here to logout</font></div></a><br><br>";
            exit;   
        }
        else{
            echo '<font face = "verdana" size = "2" color = "red">Invalid username or password</font><br>';
        }
    }
    else if($var1 || $var2){
        echo '<font face = "verdana" size = "2" color = "red">Please Fill in all fields.</font><br>';
    }
?>
    </div></td>
  </tr>
  <tr>
    <td width="221">&nbsp;</td>
    <td width="569"><form id="form1" name="form1" method="post" action="login.php">
      <p><span class="style1">username:</span>
        <input name="uname" type="text" id="username" maxlength="20" />
      </p>
      <p><span class="style1">password:</span>
        <input name="pword" type="password" id="password" maxlength="10" />
      </p>
      <p>
        <input type="submit" name="Submit" value="login" />
      </p>
    </form></td>
  </tr>
</table>
<p>&nbsp;</p>
</body>
</html>
 

Re: No database selected

Posted: Mon May 05, 2008 12:10 am
by dhiraj
dear use this
$con=mysql_connect("localhost","username(if available otherwise left blank)","password(if available otherwise left blank)");
$db=mysql_select_db("Your Data Base name",$con);

i hope yr problem solved by using the above code.........

:) :) :):) :) :):) :) :):) :) :):) :) :):) :) :):) :) :):) :) :):) :) :):) :) :):) :) :):) :) :):) :) :):) :) :)

Re: No database selected

Posted: Mon May 05, 2008 12:53 am
by tabatsoy
and still "No database selected"....

Re: No database selected

Posted: Mon May 05, 2008 1:09 am
by John Cartwright

Code: Select all

mysql_select_db("examination",$db) or die(mysql_error());
sounds like you mysql_select_db() is failing. What does this output?