4 Replies - 654 Views - Last Post: 24 February 2013 - 04:59 PM Rate Topic: -----

#1 rjelliott_30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-February 13

PHP/MYSQL Won't Display Table

Posted 24 February 2013 - 03:57 PM

I'm new to PHP and MySQL and I ran into a little trouble with my program. I'm trying to insert data into a database and then display the contents of the database in a table. The funny part is I have the hard part of the program working. I just can't figure out how to display my table after insertion. I'm getting the Warning: mysql_fetch_row() expects parameter 1 to be resource, boolean given.

<!DOCTYPE html>

<html>
	<head>
		<meta charset = "utf-8">
		<title>Exercise 19.9</title>
		<style type = "text/css">
			body  { font-family: sans-serif;
			background-color: lightyellow; } 
			table { background-color: lightblue; 
			border-collapse: collapse; 
			border: 1px solid gray; }
			td    { padding: 5px; }
			tr:nth-child(odd) {
			background-color: white; }
		</style>
	</head>

	<body>
		<?php

			$url = $_POST["url"];
			$description = $_POST["description"];
			
			$query = "INSERT INTO urltable" . 
			"( URL, description )" . 
			"VALUES ('$url', '$description' )";

			if ( !( $database = mysql_connect( "localhost","root", "password" ) ) ) 
			die( "Could not connect to database </body></html>" );
   
			if ( !mysql_select_db( "urls", $database ) )
			die( "Could not open products database </body></html>" );

			if ( !( $result = mysql_query( $query, $database ) ) ) 
			{
				print( "<p>Could not execute query!</p>" );
				die( mysql_error() . "</body></html>" );
			}

			mysql_close( $database );
		?>

		<table>
			<caption>URL Table</caption>

			<?php

			while ( $row = mysql_fetch_row( $result ) )
			{
 
				print( "<tr>" );

				foreach ( $row as $value ) 
				print( "<td>$value</td>" );

				print( "</tr>" );
			} 
			?>
		</table>

	</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: PHP/MYSQL Won't Display Table

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: PHP/MYSQL Won't Display Table

Posted 24 February 2013 - 04:07 PM

You have closed the database connection before retrieving the data.

NB The mysql library is DEPRECATED and you should be using prepared statements - see my signature.

This post has been edited by andrewsw: 24 February 2013 - 04:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 rjelliott_30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-February 13

Re: PHP/MYSQL Won't Display Table

Posted 24 February 2013 - 04:12 PM

I removed the code but I'm still seeing the exact same warning.

mysql_close( $database );

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3175
  • View blog
  • Posts: 10,625
  • Joined: 12-December 12

Re: PHP/MYSQL Won't Display Table

Posted 24 February 2013 - 04:27 PM

.. you are using an INSERT query, you need a SELECT query if you want to return rows.

After you've inserted the data, create a new $query (SELECT) statement.

This post has been edited by andrewsw: 24 February 2013 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -

#5 rjelliott_30  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-February 13

Re: PHP/MYSQL Won't Display Table

Posted 24 February 2013 - 04:59 PM

Thanks for the help. Sorry it turned out to be so easy. The code below now works perfectly.

<h1>URL Table</h1>
		<table>
			<tr>
				<th>ID</th>
				<th>URL</th>
				<th>Description</th>
			</tr>
			<?php

			$query2 = "SELECT * FROM urltable";

			$result1 = mysql_query($query2);

			while ( $row = mysql_fetch_row( $result1 ) )
			{
 
				print( "<tr>" );

				foreach ( $row as $value ) 
				print( "<td>$value</td>" );

				print( "</tr>" );
			} 

			mysql_close( $database );
			?>
		</table>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1