Jump to content


Can't fetch data from database


3 replies to this topic

#1 Mr Data

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Location:127.0.0.1

Posted 06 July 2008 - 03:09 PM

Im kind of trying to make a shoutbox but I'm having troubles fetching the data from the databas:

Here is my code:

   <?php
   $dbhost = "localhost"; // The Host you are using
   $dbuser = "user"; // The username
   $dbpass = ""; // Password. If no password is set, leave this option blank.
   $dbname = "test"; // The name of the database
   mysql_connect("$dbhost", "$dbuser", "$dbpass", "$name") or die(mysql_error()); //We connect to the host
   mysql_select_db("$dbname"); //Select the database
   
   ?>
   <html>
   <head>
   <title>MikeX PHP</title>
   <style type="text/css">
   <!--
   * {
	   margin: 0;
	   padding: 0;
   }
   form {
	   width: 350px;
	   font-family: Arial, Helvetica, sans-serif;
	   font-weight:bold;
	   font-size : 12px;
	   margin-bottom:10px;
	   margin: auto;
	   
   }
   
   #comments {
	   width: 360px;
	   height: 500px;
	   border: 1px solid black;
	   margin: auto;
	   padding: 10px;
	   text-align: left;
   }
   
   p {
	   font-family: "Century Gothic", verdana, ariel, sans-serif;
	   font-size: 14px;
	   color: black;
	   border-bottom: 1px dotted black;
	   padding-bottom: 10px;
   }
   
   h1 {
	   font-family: "Century Gothic", verdana, ariel, sans-serif;
	   font-size: 16px;
	   color: black;
	   font-style: italic;
   }
   
   hr {
	   border: 1px dotted black;
	   height: 1px;
   }
   em {
	   font-family: "Century Gothic", verdana, ariel, sans-serif;
	   font-size: 10px;
	   color: black;
   }
   
   -->
   </style>
   </head>
   <body>
   
   <form action="index.php" method="post">
		   <table>
			<tr>
			   <td>Namn: </td><td class="error">
			   </td>
			</tr>
			<tr>
			   <td colspan="2"><input class="text" type="text" id="name" name="name" />
			   </td>
		   </tr>
		   <tr>
			   <td>Meddelande:</td>
			   <td class="error"></td>
		   </tr>
		   <tr>
			   <td colspan="2"><textarea cols="40" rows="6" name="comments" />
			   </textarea>
			   </td>
		   </tr>
		   <tr>
			   <td colspan="2">
			   
   
			 <input class="text" type="submit" name="id" value="Skicka" />
			 </td>
		   </tr>
		   </table>
		 </form>
	   
	   
   <div id="comments">
   <?php
	   $date = date("H:i:s");
	   $id = $_REQUEST['id'];
	   $name = $_REQUEST['name'];
	   $post = $_REQUEST['comments'];
		   if($_REQUEST['name'] && $_REQUEST['comments']){
			   $result = mysql_query("INSERT INTO `test` (id, name, post) VALUES('$id', '$name', '$post')") or die(mysql_error());
			   exit;
		   }
		   
	   while($row = mysql_query("SELECT `test` (id, name, post) VALUES('$id', '$name', '$post') ORDER BY `id`")){
	   echo stripslashes("<h1>$row[0]");
	   echo "</h1>";
	   echo "<em>$date</em>";
	   echo stripslashes("<p>$row[1]");
	   echo "</p>";
	   }
	   
	   
   
	   ?>
   
   <!--<h1></h1>
   <p></p>-->
   
   </div>
   
   </body>
   </html>
   
   
   
   
   Put the following SQL code in PhpMyadmin to make the table work:
   
   CREATE TABLE `test` (
	 `id` int(4) NOT NULL auto_increment,
	 `name` varchar(64) default NULL,
	 `post` text,
	 PRIMARY KEY  (`id`)
   ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9;

I can insert data into the database but i can't view them, and I can't seem to figure out what the problem is.

I am pretty new to PHP so there may be obvious misstakes :friends:

#2 chaoslight

    Young Padawan

  • Members
  • Pip
  • 220 posts
  • Gender:Male
  • Interests:Computer, coding, designing...

Posted 06 July 2008 - 06:02 PM

I think its because you left out mysql_fetch_array();

while($row = mysql_query("SELECT `test` (id, name, post) VALUES('$id', '$name', '$post') ORDER BY `id`")){

should be:

while($row = mysql_fetch_array(mysql_query("SELECT `test` (id, name, post) VALUES('$id', '$name', '$post') ORDER BY `id`"))){


#3 Mr Data

    Young Padawan

  • Members
  • Pip
  • 55 posts
  • Location:127.0.0.1

Posted 07 July 2008 - 09:39 AM

I have solved the problem now:P Don't worry :friends:

I had to read some more tutorials to figure out what the problem was

#4 JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 78 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 13 July 2008 - 09:58 PM

I'm pretty sure there were a few problems he had with that code...2 right off the bat were:
$query = mysql_query("SELECT id, name, post FROM test ORDER BY id"); \\ he had the wrong sql syntax, this is correct
while($row = mysql_fetch_array($query)) \\ he was trying to get values from an array without making the array, this is correct
{
}
I don't know where he got the original code from but that was definitely wrong sql syntax. lol I wish people would post their solutions to their problems once they figured it out though.

Edited by JoeyMagz, 13 July 2008 - 10:01 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users