Author Topic: No database selected error  (Read 433 times)

mev9669

  • New Member
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
No database selected error
« on: May 18, 2012, 12:19:16 PM »
Hi,

I am very new to PHP and trying to write a small bit of code to insert a record to the database. The code gives me a No database Selected error. I have tried to check the connection to the database and it does get connected. 

Thanks for your help in advance

PHP Code: [Select]


<?php require_once('../Connections/connect.php'); ?>
<?php

$today 
date("Y-m-d H:i:s");


if (
$_POST['form_submitted'] == '1') {
##User is registering, insert data until we can activate it

$activationkey =  mt_rand().mt_rand();
$user_name mysql_real_escape_string($_POST[user_name]);
$user_last mysql_real_escape_string($_POST[user_last]);
$user_pass mysql_real_escape_string (md5($_POST[user_pass]));

$user_email mysql_real_escape_string($_POST[user_email]);
$telephone mysql_real_escape_string($_POST[telephone]);


$sql="INSERT INTO local_advertisers (user_name, user_last, user_pass, user_email, telephone, registerdate, activationkey, registrationstatus) VALUES ('$user_name', '$user_last', '$user_pass', '$user_email', '$telephone', '$today', '$activationkey', 'verify')";


if (!
mysql_query($sql))

  {

  die(
'Error: ' mysql_error());

  }

?>

g0dzuki99

  • Regular Member
  • **
  • Posts: 78
  • Karma: 5
    • View Profile
    • www.chaoscontrol.org
Re: No database selected error
« Reply #1 on: May 19, 2012, 10:43:15 AM »
What's in /Connections/connect.php?

You need to connect to the database with a valid username/password and select the database you want to use.

From php.net

PHP Code: [Select]
<?php

$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());
}
?>


The error tells you what's wrong.... check your conenct.php
http://www.chaoscontrol.org
Have you minified your CSS/JS lately?

mev9669

  • New Member
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
Re: No database selected error
« Reply #2 on: May 21, 2012, 04:30:11 AM »
My connect. php contains the following. the connect to the database does work as I am able to connect to the database and do a select query.

connect. php

PHP Code: [Select]

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_connect "localhost";
$database_connect "Mydatabase";
$username_connect "username";
$password_connect "password";
$connect mysql_pconnect($hostname_connect$username_connect$password_connect) or trigger_error(mysql_error(),E_USER_ERROR); 
?>


g0dzuki99

  • Regular Member
  • **
  • Posts: 78
  • Karma: 5
    • View Profile
    • www.chaoscontrol.org
Re: No database selected error
« Reply #3 on: May 21, 2012, 08:45:07 AM »
You're connecting the database server but not opening a database to use. Notice your unused $database_connect variable.


Try adding this to bottom of your existing code:

PHP Code: [Select]
$db_selected mysql_select_db($database_connect$connect);
if (!
$db_selected) {
    die (
'Can\'t use foo : ' mysql_error());
}


http://www.chaoscontrol.org
Have you minified your CSS/JS lately?

mev9669

  • New Member
  • *
  • Posts: 3
  • Karma: 0
    • View Profile
Re: No database selected error
« Reply #4 on: May 21, 2012, 10:19:01 AM »
Still does not work.

I have a login with the same connection and it worked without the code you mentioned to be added in the connect.php

the below is my login.php

PHP Code: [Select]

<?php require_once('../Connections/connect.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}
?>
<?php
// *** Validate request to login to this site.
if (!isset($_SESSION)) {
  
session_start();
}

$loginFormAction $_SERVER['PHP_SELF'];
if (isset(
$_GET['accesscheck'])) {
  
$_SESSION['PrevUrl'] = $_GET['accesscheck'];
}

if (isset(
$_POST['user_email'])) {
  
$loginUsername=$_POST['user_email'];
  
$password=md5($_POST['user_pass']);
  
$MM_fldUserAuthorization "";
  
$MM_redirectLoginSuccess "/event-advertising/myaccount/";
  
$MM_redirectLoginFailed "/event-advertising/login.php";
  
$MM_redirecttoReferrer false;
  
mysql_select_db($database_connect$connect);
  
  
$LoginRS__query=sprintf("SELECT user_email, user_pass FROM local_advertisers WHERE user_email=%s AND user_pass=%s",
    
GetSQLValueString($loginUsername"text"), GetSQLValueString($password"text")); 
   
  
$LoginRS mysql_query($LoginRS__query$connect) or die(mysql_error());
  
$loginFoundUser mysql_num_rows($LoginRS);
  if (
$loginFoundUser) {
     
$loginStrGroup "";
    
    
//declare two session variables and assign them
    
$_SESSION['MM_Username'] = $loginUsername;
    
$_SESSION['MM_UserGroup'] = $loginStrGroup;
	
      

    if (isset(
$_SESSION['PrevUrl']) && false) {
      
$MM_redirectLoginSuccess $_SESSION['PrevUrl'];
	

    }
    
header("Location: " $MM_redirectLoginSuccess );
  }
  else {
    
header("Location: "$MM_redirectLoginFailed );
  }
}
?>


sajan

  • Regular Member
  • **
  • Posts: 54
  • Karma: 4
    • View Profile
Re: No database selected error
« Reply #5 on: June 12, 2012, 01:21:41 AM »
In the login.php you are using the code
PHP Code: [Select]
 mysql_select_db($database_connect$connect); to select database. Same code you need to use this code while inserting record also
Looking for FREEEEE HOSTING? Here it is http://www.000webhost.com/594131.html