6 Replies - 1413 Views - Last Post: 10 January 2006 - 09:19 PM Rate Topic: -----

#1 dbfootballa3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 24-November 05

Issue Selecting Database

Posted 09 January 2006 - 04:11 PM

Okay I am making a simple guestbook for a tutorial on my website and it is giving me the error that a database hasn't been selected.

Here is the catch though, I do select the database at the beginning of the script.

<?php

//To test this out run this query!
/*
CREATE TABLE `db` (
`id` INT NOT NULL ,
`name` TEXT NOT NULL ,
`body` TEXT NOT NULL ,
 `date` TEXT NOT NULL
) TYPE = MYISAM;
*/

if( $_POST['name'] != "" &&  $_POST['body'] != "") {

$name = stripslashes($_POST['name']);
$body = stripslashes($_POST['body']);
$date = date("F j, Y. g:i A");

#-----You need to change these values, or you will want to strangle me :-)-----#
$sql_server = "localhost"; //This is the name of your server, usually it is 'localhost'
$sql_name = "root";
$sql_pass = ""; 
$sql_db = "guest";
#-----Don't edit below this, or you will want to strangle me again... :-)-----#

mysql_connect($sql_server,$sql_name, $sql_pass); 
mysql_select_db($sql_db); 
  
 $id = mysql_query( "SELECT `id` FROM `db`  ORDER BY `id` DESC" );
 $id = mysql_fetch_row( $id );

 for( $i=0; $i<count( $id ); $i++ ) {

$total = $id[$i] + 1;

mysql_query( "INSERT INTO db (id, name, body,date)  VALUES('$total', '$name', '$body','$date')" );
echo mysql_error();
echo "Your post has been added!";
$_POST['name'] = "";
$_POST['body'] = "";

 }

} else {

}

mysql_connect($sql_server,$sql_name, $sql_pass); 
mysql_select_db($sql_db); 

echo "<table>";
 $id = mysql_query(" SELECT `id` FROM `db`  ORDER BY `id` DESC" ) or die(mysql_error());
while( $id = mysql_fetch_row( $id )  ) {
echo "<tr>";
echo "<td>";
echo "<strong>$id[name]</strong>";
echo "</td>";
echo "<td>";
echo "$id[date]";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "</td>";
echo "<td>";
echo "$id[body]";
echo "</td>";
echo "</tr>";

}
echo "</table>";

?>
<form action="<?=$_SERVER['PHP_SELF'] ?>" method='post'>
 <table>
  <tr>
   <td>Name:
   </td>
   <td><input type='text' name='name'  size='25'>
   </td>
  </tr>
  <tr>
   <td>Body:
   </td>
   <td><textarea name='body' rows='15' cols='45'></textarea>
   </td>
  </tr>
  <tr>
   <td>
   </td>
   <td><input type='submit'> <input type='reset'>
   </td>
  </tr>
 </table>
</form>



Sorry that it is a little long..

Any help is greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Issue Selecting Database

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 247
  • View blog
  • Posts: 13,505
  • Joined: 12-July 02

Re: Issue Selecting Database

Posted 09 January 2006 - 05:03 PM

Have you tried appending an 'or die' statement to the connection, to see if it is made correctly?
Was This Post Helpful? 0
  • +
  • -

#3 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 64
  • View blog
  • Posts: 3,505
  • Joined: 31-March 03

Re: Issue Selecting Database

Posted 09 January 2006 - 09:48 PM

To do that, do this.

Replace:
mysql_connect($sql_server,$sql_name, $sql_pass);


With:
mysql_connect($sql_server,$sql_name, $sql_pass) or die(mysql_error());

Was This Post Helpful? 0
  • +
  • -

#4 dbfootballa3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 24-November 05

Re: Issue Selecting Database

Posted 10 January 2006 - 02:42 PM

Thanks for changing the Thread name :-)

hotsnoj, I don't think that is the problem, I think it is in the last query, but I will try that.

Anyways, the error is "Database is not Selected". I put the or die() in the query ( The Last one ).

EDIT: Acually the error is selecting the database in the beginning.
Here is the weird thing, This only happened after I added the code that shows the posts.

PS: Thanks.

This post has been edited by dbfootballa3: 10 January 2006 - 02:46 PM

Was This Post Helpful? 0
  • +
  • -

#5 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 64
  • View blog
  • Posts: 3,505
  • Joined: 31-March 03

Re: Issue Selecting Database

Posted 10 January 2006 - 05:32 PM

Read the code for my comments!
<?php

//To test this out run this query!
/*
CREATE TABLE `db` (
`id` INT NOT NULL ,
`name` TEXT NOT NULL ,
`body` TEXT NOT NULL ,
`date` TEXT NOT NULL
) TYPE = MYISAM;
*/

if( $_POST['name'] != "" &&  $_POST['body'] != "") {

$name = stripslashes($_POST['name']);
$body = stripslashes($_POST['body']);
$date = date("F j, Y. g:i A");

#-----You need to change these values, or you will want to strangle me :-)-----#
$sql_server = "localhost"; //This is the name of your server, usually it is 'localhost'
$sql_name = "root";
$sql_pass = "";
$sql_db = "guest";
#-----Don't edit below this, or you will want to strangle me again... :-)-----#

//
//You only need to use these functions once.
//Unless you're changing databases.
//
mysql_connect($sql_server,$sql_name, $sql_pass);
mysql_select_db($sql_db);

$id = mysql_query( "SELECT `id` FROM `db`  ORDER BY `id` DESC" );
$id = mysql_fetch_row( $id );

//
//What exactly are you trying to accomplish here?
//You're looping through the results of the last sql query.
//The thing is, there is only one result and the only
// thing you get is whatever the field 'id' holds.
//So this for loop will only loop once. (maybe twice?)
//The funny thing is that you could do all this outside a loop.
//
for( $i=0; $i<count( $id ); $i++ ) {
//
//$total will be an int, that is a number.
//
$total = $id[$i] + 1;

mysql_query( "INSERT INTO db (id, name, body,date)  VALUES('$total', '$name', '$body','$date')" );
echo mysql_error();
echo "Your post has been added!";
$_POST['name'] = "";
$_POST['body'] = "";


}
//
//Did you comment out an if statement?
//
} else {

}

//
//What is this doing here?! You don't need to connect to the database again!
//This may be part of your problem.
//
mysql_connect($sql_server,$sql_name, $sql_pass);
mysql_select_db($sql_db);

echo "<table>";
$id = mysql_query(" SELECT `id` FROM `db`  ORDER BY `id` DESC" ) or die(mysql_error());
//
//Now I haven't done any testing, but you are obviously 
// overwriting the mysql query's resource.
//I'd say to change $id to something else like $row.
//mysql_fetch_row() returns the results using numbers as array keys.
//Not alphanumeric names. I'd suggest to use mysql_fetch_assoc()
// for what you're trying to do here.
//Also, use ' or " marks surrounding the array keys.
//So $id[name] will be $id['name'].
//
while( $id = mysql_fetch_row( $id )  ) {
echo "<tr>";
echo "<td>";
echo "<strong>$id[name]</strong>";
echo "</td>";
echo "<td>";
echo "$id[date]";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "</td>";
echo "<td>";
echo "$id[body]";
echo "</td>";
echo "</tr>";

}
echo "</table>";

?>


Also read up on mysql_fetch_row() and mysql_fetch_assoc().
Was This Post Helpful? 0
  • +
  • -

#6 dbfootballa3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 238
  • Joined: 24-November 05

Re: Issue Selecting Database

Posted 10 January 2006 - 05:50 PM

Thanks for the help, I am going to recode it completely.

I am not new to PHP but I am to MySql.

Thanks again!
Was This Post Helpful? 0
  • +
  • -

#7 snoj  Icon User is offline

  • Married Life
  • member icon

Reputation: 64
  • View blog
  • Posts: 3,505
  • Joined: 31-March 03

Re: Issue Selecting Database

Posted 10 January 2006 - 09:19 PM

You're welcome! I'm so glad I can help.

btw, this comment:
//
//Did you comment out an if statement?
//



I missed the if statement earlier in the code. So please disregard that. Thank you!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1