Hi!
I'm going to tear your code apart, but I'm not being mean 🙂
<?php
function freeCots(){
// when you say `global`, it's probably a bad idea.
global $cottracker;
// no reason to use `sprintf()` here - you're not formatting anything.
// using * can lead to problems later - better to *know* which columns you're asking for.
$query_rsFreeCots = sprintf("SELECT * FROM history");
// DO NOT DIE().
// while it's more-or-less "okay" during development,
// doing this on a live site is a security risk (as well as bad user experience).
// DO NOT USE mysql_* functions.
// they are long outdated and will be deprecated soon.
$rsFreeCots = mysql_query($query_rsFreeCots, $cottracker) or die(mysql_error());
// you don't know if your query worked or not.
// if it didn't, you'll just trigger an error when you try to fetch the rows.
$row_rsFreeCots = mysql_fetch_assoc($rsFreeCots);
// you're returning an array here, but later (in your code) you're trying to treat it like a result resource...?
return $row_rsFreeCots;
}
?>
<table>
<?php $row_rsFreeCots=freeCots();
do { ?>
<tr><td><?php echo $row_rsFreeCots['record'];?></td></tr>
<?php
// while the `echo` above *might* make sense, this makes none at all:
// 1) you're overwriting your row with the return value of `mysql_fetch_assoc()`, which
// 2) will be FALSE because you don't provide any arguments -
// basically, this is the same as `while( FALSE )`, except it throws an error.
} while ($row_rsFreeCots = mysql_fetch_assoc());
?>
</table>
Try something more like this:
<?php
// USE mysqli instead of mysql_* functions.
$mysqli = new mysqli( 'host','username','password','tablename' );
// the function freeCots() returns one of:
// 1) an array with all the results from the database
// 2) FALSE if there was an error
// don't forget to pass in the database connection (`$mysqli`)
$all_rows = freeCots( $mysqli );
// pass `$all_rows` to the function `freeCotsTable()`
// the function returns HTML markup for your table
$table = freeCotsTable( $all_rows );
// when you're ready to print the <table>, do so
echo $table;
Here's the definitions for the functions:
function freeCots( $mysqli ){
// list your field names in the query
$query = "SELECT `col1`,`col2`,`etc` FROM `history`";
// how many records are likely to be in your `history` table?
// it would probably be a good idea to make sure you don't try to get too many rows at once
// (e.g., LIMIT the number of results you return)
// query the database
// ($mysqli is your database connection - passed in when you call the function - no `global`!)
$result = $mysqli->query( $query );
// check if you got a result
if( $result !== false ){
// loop through the rows.
while( $row = $result->fetch_assoc() ){
// add the row to an array
$all_rows[] = $row;
}
// if there were results, they're all in this array now
if( !empty( $all_rows ) ){
return $all_rows;
}
// if not, do nothing right now (see below)
}
// if you didn't get a result, you can return false, and deal with it when you build your <table>
return false;
}
function freeCotsTable( $all_rows=FALSE ){
// check if $all_rows is an array
if( is_array( $all_rows ) ){
// loop through the rows
foreach( $all_rows as $row ){
// make sure each index exists:
if( isset( $row['col1'] ) && isset( $row['col2'] ) && isset( $row['etc'] ) ){
// if all info exists, write a row for your <table>
$tr[] = "
<tr>
<td>{$row['col1']}</td>
<td>{$row['col2']}</td>
<td>{$row['etc']} </td>
</tr>";
}
// (if info is missing, don't write a <tr>)
}
// if $all_rows is not an array (e.g., FALSE),
}else{
// then we can give an error message
$tr[] = "<tr><td>No results found.</td></tr>";
}
// build the <table>
// (`implode()` puts all of the rows in `$tr` together into a single string)
$table = "<table>".implode( "\n",$tr )."</table>";
// return the table
return $table;
}