No database selected error

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]

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

[/php]

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]<?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());
}
?>[/php]

The error tells you what’s wrong… check your conenct.php

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]

<?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); ?>

[/php]

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]$db_selected = mysql_select_db($database_connect, $connect);
if (!$db_selected) {
die ('Can’t use foo : ’ . mysql_error());
}[/php]

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]

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

[/php]

In the login.php you are using the code [php] mysql_select_db($database_connect, $connect);[/php] to select database. Same code you need to use this code while inserting record also

Sponsor our Newsletter | Privacy Policy | Terms of Service