kevinritt Posted October 5, 2009 Share Posted October 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/ Share on other sites More sharing options...
Bricktop Posted October 5, 2009 Share Posted October 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930860 Share on other sites More sharing options...
kickstart Posted October 5, 2009 Share Posted October 5, 2009 Hi Need to select the actual database:- $db_selected = mysql_select_db($database_dbconn, $dbconn); All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930862 Share on other sites More sharing options...
kevinritt Posted October 5, 2009 Author Share Posted October 5, 2009 I get the dope slap for today Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930922 Share on other sites More sharing options...
kevinritt Posted October 5, 2009 Author Share Posted October 5, 2009 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'); Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930951 Share on other sites More sharing options...
kickstart Posted October 5, 2009 Share Posted October 5, 2009 Hi What's the error you are getting now? All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930954 Share on other sites More sharing options...
kevinritt Posted October 5, 2009 Author Share Posted October 5, 2009 Data not entered Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930961 Share on other sites More sharing options...
PFMaBiSmAd Posted October 5, 2009 Share Posted October 5, 2009 You previously has mysql_error() as part of your die(...) statement. Why did you remove that. It would be telling you why the query failed to execute. Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930975 Share on other sites More sharing options...
kevinritt Posted October 5, 2009 Author Share Posted October 5, 2009 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."); ?> Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930983 Share on other sites More sharing options...
PFMaBiSmAd Posted October 5, 2009 Share Posted October 5, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930990 Share on other sites More sharing options...
kevinritt Posted October 5, 2009 Author Share Posted October 5, 2009 Thanks - good catch Typical rookie mistake Quote Link to comment https://forums.phpfreaks.com/topic/176574-need-help-with-connecting-to-database/#findComment-930992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.