<!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>
PHP/MYSQL Won't Display Table
Page 1 of 14 Replies - 182 Views - Last Post: 24 February 2013 - 04:59 PM
#1
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.
Replies To: PHP/MYSQL Won't Display Table
#2
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.
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
#3
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 );
#4
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.
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
#5
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>
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|