Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Output MySQL data into normal table?

Status
Not open for further replies.

d3sol4t3

Programmer
Oct 27, 2005
40
GB
Hi all

here is my site:

As you can see there are 3 tables running down the middle (Vulnerabilities / Advisories, Exploit POC / Shellcode and Technews)

I have data stored in MySQL database and output it into these tables, how can this be done?

Thank You
 
read the manual.

from
Code:
<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
   or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "\t<tr>\n";
   foreach ($line as $col_value) {
       echo "\t\t<td>$col_value</td>\n";
   }
   echo "\t</tr>\n";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Hey thank you jeff, just one problem. (btw im complete beginner to PHP so be gentle)

The script above does not ouput the data into the tables i have already made :(.

Here look:


^^ That creates the table contacts

Using your script you posted, this displays that table:


I tried putting your script onto my page same thing happens :(.

Can anybody help me?
 
all the script does is connect to a database, retrieve a result set and echo html from that result set.

i did notice that the example iterates over every field retrieved by "select *", which is not necessarily the best or clearest way to do things, so we'll explicitly echo specific fields instead.

one way to achieve what you're after is to take the above example, remove the lines
echo "<table>\n";
and
echo "</table>\n";

and place the script inside your existing table tags, perhaps preserving the header <th>s if you have them, e.g.
Code:
<table>
 <tr>
  <th>my</th>
  <th>header</th>
  <th>columns</th>
 </tr>
<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
   or die('Could not connect: ' . mysql_error());
echo 'Connected successfully';
mysql_select_db('my_database') or die('Could not select database');

// Performing SQL query
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

// Printing results in HTML
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
   echo "<tr>\n";
   echo "<td>${row['my_field_1']}</td>\n";
   echo "<td>${row['my_field_2']}</td>\n";
   echo "<td>${row['my_field_3']}</td>\n";
   echo "</tr>\n";
}

// Free resultset
mysql_free_result($result);

// Closing connection
mysql_close($link);
?>
</table>

making sure that the number of <td>s per <tr> that you generate matches up with the number of <th>s in your header row. also replace "my_field_x" etc with your table field names.

take a look at

to learn how the various mysql functions work.

-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
i like your sleeves...they're real big
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top