Jump to content

[SOLVED] Php newbie, stuck with displaying seperate sets of data from sql database


patsypoopa

Recommended Posts

Hi

I'm a php newbie and am currently stuck with a problem on an assignment i've been given. The problem is that I am struggling to display columns and records from a table. The thing thats confusing is that due to the nature of my site every table is different so I cannot display columns just by saying their names and using

col["bleh"]

.

I've managed to get the code so I can display 1 record from the table but I cat get it to display more than one. I'm convinced I've made a simple mistake but i've been staring at this all day and am now brain dead.

 

Thanks either way

 


<?

$i = 0;
$j = 0;

do{
echo "<table>";
while ($row = mysql_fetch_array($result_rec, MYSQL_NUM))
{
	while ($i < mysql_num_rows($result_col)) 
	{
		$col[$i] = mysql_tablename($result_col, $i);
		echo( "<tr><td><b>" . $col[$i] . ":</b></td><td>" . $row[$i] . "</td></tr>"); 
		$i++;
	}
}

echo "</table>";

$j++;
}while ($j < mysql_num_rows($result_rec));

 

Sorry I must of cut it when copying and pasting:

 


$sql = "SELECT * FROM $chosencollection";
$result_rec = mysql_query($sql);

$sql_col = "SHOW COLUMNS FROM $chosencollection";
$result_col = mysql_query($sql_col);

Here is a good tutorial on displaying results with headers from the column name.  I don't think you need a separate statement to show the column names, as shown in the tutorial.

 

http://www.anyexample.com/programming/php/php_mysql_example__display_table_as_html.xml

I feel your pain I was writing a program that did a similar function. What I ended up doing was writing an entire relay script to interact with my databases. This is the function i setup to pull data out of tables dynamically.

 

// get - retrieve data from the database. Gets stored to a singe array.
function f_get_all($db_name, $db_user, $db_pword, $db_table)
{
/// Connect To Database
$con = mysql_connect($db_name, $db_user, $db_pword, $col);
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}
mysql_select_db($db_user, $con);

//Pull the data from the database, and store it to an array.
$result=mysql_query("SELECT * FROM $db_table");
$i = 0;
while($rows = mysql_fetch_array($result))
  	{
	for($j = 0; $j<$col; $j++)
	{
  			$rows_temp[$i][$j] = $rows[$i][$j];
	}
	$i++;
  	}
// Disconnect from database.
mysql_close($con);

//Return the array
return $rows_temp;
}

//This is the call to the function, which stores data in a multi-dimensional array
$rows = f_get_all($db_name, $db_user, $db_pword, $db_table);

 

That should pull all fields out of the database and store it to a multi-dimensional array. You then run a loop to count how many rows, and how many columns the array has. Set up an (i,j) for loop to push them all to a table. Hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.