mr_badger Posted May 1, 2007 Share Posted May 1, 2007 I have an error, not a php syntax error, everything is working fine but I get this error "Error in query. No database selected", does anyone have any idea on how to solve this. Quote Link to comment https://forums.phpfreaks.com/topic/49439-solved-error-in-query-no-database-selected/ Share on other sites More sharing options...
Gobbo Posted May 1, 2007 Share Posted May 1, 2007 What's the code you use to connect to the database? Quote Link to comment https://forums.phpfreaks.com/topic/49439-solved-error-in-query-no-database-selected/#findComment-242278 Share on other sites More sharing options...
DaveEverFade Posted May 1, 2007 Share Posted May 1, 2007 When connecting make sure you have the mysql_select_db mysql_connect('host', 'user', 'password'); @mysql_select_db("db_name") or die(mysql_error); Quote Link to comment https://forums.phpfreaks.com/topic/49439-solved-error-in-query-no-database-selected/#findComment-242279 Share on other sites More sharing options...
jitesh Posted May 1, 2007 Share Posted May 1, 2007 <?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()); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49439-solved-error-in-query-no-database-selected/#findComment-242280 Share on other sites More sharing options...
paul2463 Posted May 1, 2007 Share Posted May 1, 2007 When connecting make sure you have the mysql_select_db mysql_connect('host', 'user', 'password'); @mysql_select_db("db_name") or die(mysql_error); be wary of this, the @ sign says that you do not want any errors brought to your attention, then you go and ask it to produce one at the end, incorrectly mysql_select_db("db_name") or die("cannot connect to the database" . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/49439-solved-error-in-query-no-database-selected/#findComment-242334 Share on other sites More sharing options...
DaveEverFade Posted May 1, 2007 Share Posted May 1, 2007 Yeah, sorry about that one.. I'm always leaving the () off. There were some much more in depth solutions after mine, so take your pick.. Quote Link to comment https://forums.phpfreaks.com/topic/49439-solved-error-in-query-no-database-selected/#findComment-242355 Share on other sites More sharing options...
mr_badger Posted May 7, 2007 Author Share Posted May 7, 2007 Thanks for your replies, but it still hasn't solved the problem, here is the code. <?php // includes include('conf.php'); include('functions.php'); // form not yet submitted // display initial form with values pre-filled if (!$_POST['submit']) { // check for record ID if ((!isset($_GET['id']) || trim($_GET['id']) == '')) { die('Missing record ID'); } //open database connection $connection = @mysql_connect($host, $user, $pass) or die ('Unable to connect'); // select database $id = $_GET['id']; $query = "SELECT title, content, contact FROM news WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query. " . mysql_error()); // generate and execute query $id = $_GET['id']; $query = "SELECT title, content, contact FROM news WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // if a result is returned if (mysql_num_rows($result) > 0) { // turn it into an object $row = mysql_fetch_object($result); // print form with values pre-filled ?> <table cellspacing="5" cellpadding="5"> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <input type="hidden" name="id" value="<?php echo $id; ?>"> <tr> <td valign="top"><b><font size="-1">Title</font></b></td> <td> <input size="50" maxlength="250" type="text" name="title" value="<?php echo $row->title; ?>"> </td> </tr> <tr> <td valign="top"><b><font size="-1">Content</font></b></td> <td> <textarea name="content" cols="40" rows="10"><?php echo $row->content; ?></textarea> </td> </tr> <tr> <td valign="top"><font size="-1">Contact Person</font></td> <td> <input size="50" maxlength="250" type="text" name="contact" value="<?php echo $row->contact; ?>"> </td> </tr> <tr> <td colspan=2> <input type="Submit" name="submit" value="Update"> </td> </tr> </form> </table> <?php } // no result returned // print graceful error message else { echo '<font size=-1>That press release could not be located in our database.</font>'; } } else { //form submitted //start processing it } ?> <?php if (!$_POST['submit']) { // display initial form with values pre-filled } else { // set up error list array $errorList = array(); $title = $_POST['title']; $content = $_POST['content']; $contact = $_POST['contact']; $id = $_POST['id']; // check for record ID if ((!isset($_POST['id']) || trim($_POST['id']) == '')) { die ('Missing record ID'); } // validate text input firlds if (trim($_POST['title']) == '') { $errorList[] = 'Invalid entry: Title'; } if (trim($_POST['content']) == '') { $errorList[] = "Invalid entry: Content"; } // set default value for contact person if (trim($_POST['contact']) == '') { $contact = $def_contact; } // check for errors // if none found... if (sizeof($errorList) == 0) { // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect'); // select database @mysql_select_db($db) or die ('Unable to select database'); // generate and execute query $query = "UPDATE news SET title = '$title', content = '$content', contact = '$contact', timestamp = NOW() WHERE id = '$id'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); //print result echo '<font size=-1>Update successful.'; echo '<a href=list.php>Go back to main menu</a>/</font>'; //close database connection mysql_close($connection); } else { // errors occured // print as list echo '<font size=-1>The following errors were encountered:'; echo'<br>'; echo'<ul>'; for ($x=0; $x<sizeof($errorList); $x++) { echo "<li>$errorList[$x]"; } echo '</ul></font>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49439-solved-error-in-query-no-database-selected/#findComment-247246 Share on other sites More sharing options...
trq Posted May 7, 2007 Share Posted May 7, 2007 After this line... $connection = @mysql_connect($host, $user, $pass) or die ('Unable to connect'); You need a call to mysql_select_db. Quote Link to comment https://forums.phpfreaks.com/topic/49439-solved-error-in-query-no-database-selected/#findComment-247257 Share on other sites More sharing options...
mr_badger Posted May 8, 2007 Author Share Posted May 8, 2007 Thankyou very much for pointing that out, don't know how I missed that. Quote Link to comment https://forums.phpfreaks.com/topic/49439-solved-error-in-query-no-database-selected/#findComment-247695 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.