FIXED: How to get Column names from mySQL in php w/ PEAR DB

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
duncanwilkie
Forum Newbie
Posts: 17
Joined: Wed Feb 28, 2007 3:26 pm
Location: Bristol, UK

FIXED: How to get Column names from mySQL in php w/ PEAR DB

Post by duncanwilkie »

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Duly Noted, apologies. - Duncan.

Overview:  A MySQL database has been setup which requests the results from the database, currently the table headings are typed manually.

Problem:  
How would I request and display the column heading directly from the database (mySQL)?

Area: See the "// Column Headings" section in the code
Note: Using PHP and PEAR DB.

Any help would be appreciated, many thanks.
- Duncan

Code: Select all

<?php

//
// PHP:  This queries the database for all properties on the database, and displays the results. 
// Includes:  Alternating colours for rows on the table. 
//

		require_once('db_login.php');
		
		$query = ("SELECT * 
				  FROM `properties`
				  NATURAL JOIN `available`");
							
		$result = $db->query($query);
	
		if(DB::isError($result))	{
				die("Could not query the database: <br />".DB::errorMessage($result));
		}
	
		$alternateRows=false;
	
	// Table
		echo	('<table border="1">'); // Display results in table
		echo  '<tr class="cellcolor">

// Column Headings
					<td>Property<br />Id No
					<td>Status<br />(Available)
					<td>Property<br />Type
					<td>Living<br />Room
					<td>Living Room<br />Size
					<td>Electric
					<td>Gas
					<td>Electric or<br />Gas Cooker?
					<td>Garden
					<td>Broadband<br />Installed
					<td>Broadband Company
					<td>When<br />Available
					<td>Useable
					<td>Additional Information<tr>';  //Table Headings
// END OF Column Headings

	// While - Display all results
		while($myrow = $result->fetchRow()) {														


	// Alternate Row colours
				if($alternateRows==false) {				// Alternate Rows for change of colour on row option of true/false.
						echo '<tr><td>';
						$alternateRows=true;
				}
				elseif($alternateRows==true)	{
						echo '<tr class="altrowcolor"><td>';	// altrowcolor class selected here; under "true" for alternateRows.
						$alternateRows=false;
				}
			

				echo $myrow[1] . '</td><td>';		// 
				echo $myrow[15] . '</td><td>';
				echo $myrow[3] . '</td><td>';
				echo $myrow[5] . '</td><td>';
				echo $myrow[6] . '</td><td>';
				echo $myrow[7] . '</td><td>';
				echo $myrow[8] . '</td><td>';
				echo $myrow[9] . '</td><td>';
				echo $myrow[10] . '</td><td>';
				echo $myrow[11] . '</td><td>';
				echo $myrow[12] . '</td><td>';
				echo $myrow[13] . '</td><td>';
				echo $myrow[14] . '</td><td>';
				echo $myrow[16] . '</td>';

		}	// end While statement
	
		echo ('</table>') ; // end Table
	
		$db->disconnect();
	
?>

Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Last edited by duncanwilkie on Wed Feb 28, 2007 7:36 pm, edited 2 times in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

duncanwilkie
Forum Newbie
Posts: 17
Joined: Wed Feb 28, 2007 3:26 pm
Location: Bristol, UK

Post by duncanwilkie »

Code: Select all

$info =$db->tableInfo('properties');
		
	while($myCol=$db->fetch())	{
		echo $myCol[1];
	}
OK the "$info =$db->tableInfo('properties');" - works

fetch() doesn't work by itself, I know that, but not sure what else to put it.
$myCol[1]; - not sure if thats right either.

But I'm getting confused at how to put that into a while statement, and displaying it.

fetchRow() - gets "Fatal error: Call to a member function fetchRow() on a non-object"
duncanwilkie
Forum Newbie
Posts: 17
Joined: Wed Feb 28, 2007 3:26 pm
Location: Bristol, UK

Fixed it - Thank You

Post by duncanwilkie »

Thank you, I just figured out the final pieces, I was getting error messages when trying to display the columns results.

Inserted this to get TableInfo from Table 'properties':

Code: Select all

$info =$db->tableInfo('properties');
Then to display:

Code: Select all

echo $info[1]['name'].'</td>';
And it worked, thanks for the link to tableInfo, I had looked at it before, but hit my head against the wall when trying to get it too actually work though. - oh well sorted now.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Glad it worked for you.
Post Reply