Jump to content

need help with connecting to database


kevinritt

Recommended Posts

I keep getting a 'No database selected' error when I try to enter data. Here's the code for the dbconn.php file in the connections folder:

<?php
$hostname_dbconn = "localhost";
$database_dbconn = "pickering";
$username_dbconn = "name";
$password_dbconn = "password";
$dbconn = mysql_pconnect($hostname_dbconn, $username_dbconn, $password_dbconn) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

 

and here's the query:

<?php
require_once('connections/dbconn.php');

if ($_POST['submit']) 
{ 
// data from form
$date = $_POST['date'];
$comments = $_POST['comments'];

# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE 
$query ="INSERT INTO detention (date,comments) VALUES ('$date','$comments')";
$result = mysql_query($query) or die ("Sql Error" .mysql_error());
echo 'data entered' <br />
<meta http-equiv="refresh" content="2; url=./index.php">'; 
}
else
{
?>
rest of page ...

I tried changing the path to the dbconn from this connections/dbconn.php to this /connections/dbconn.php

still cannot get it to work

Link to comment
https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/
Share on other sites

Hi kevinritt,

 

You're not selecting the database, use the mysql_select_db function to select your database. Are you sure you want to use mysql_pconnect?  Try using mysql_connect instead, mysql_pconnect opens a persistent connection to the database.

 

Hope this helps.

:-[ I get the dope slap for today

 

Still working on it but missing something. Here's what I changed:

$connection = mysql_connect("$dbhost","$dbuser","$dbpasswd") 
or die ("Couldn't connect to server.");

$db = mysql_select_db("$database", $connection)
or die("Couldn't select database.");

Here's the query:

$query ="INSERT INTO detention (date,comments) VALUES ('$date','$comments')";
$result = mysql_query($query) or die ('Data not entered');

didn't mean to remove that - I added

$query ="INSERT INTO detention (date,comments) VALUES ('$date','$comments')";
$result = mysql_query($query) or trigger_error(mysql_error(),E_USER_ERROR);

I get this error: Fatal error: No database selected in C:\wamp\www\Detention\index.php on line 10

Line 10 is the result variable $result

I'm sure that it's something simple that I'm missing but I don't see it

 

Here's the dbconn file:

<? 

/* -- Configure the Variables Below --*/
$dbhost = 'localhost';
$dbuser = 'user';
$dbpasswd = 'pw';
$database = 'pickering';

/* Database Stuff, do not modify below this line */
$connection = mysql_connect("$dbhost","$dbuser","$dbpasswd") 
or die ("Couldn't connect to server.");

$db = mysql_select_db("$database", $connection)
or die("Couldn't select database.");

?>

 

 

Your dbconn file is using short open tags, so the code in it is not being executed and you apparently have a connection being made at some point, someplace else in your code, but not the mysql_select_db();

 

Only use full <?php tags to avoid wasting your time.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.