Page 1 of 1

[SOLVED] PHP outputting data in a html table

Posted: Tue Oct 19, 2004 7:14 pm
by blkshirt
So I found a small piece of code from this book here page 13 is the example is the one I used if anybody has it and wants to use it for reference.

Basically here's the setup I have. I have a mySQL database that I have Track & Field Records stored in. I would like to display that data in a table on a php page. I used the example that was provided in the book and made some changes, mostly to the html code. The only difference in the php code from the book to what I keyed in is the mySQL connection string, otherwise it is copied line for line.

Well, I shouldn't have any problems, correct? Nope, I get the following error:
My PHP Error wrote:Fatal error: Call to a member function on a non-object in /home/j9d2k0/public_html/ohhstrack/phptest/index.php on line 34
I've looked at line 34 over and over again and I can't figure it out. Hopefully somebody here can help...

Here is all of the php code:

Code: Select all

<?php
   //connect to the database
   $dbh=mysql_connect ("localhost", "databasename", "databasepassword") or die ('I cannot connect to the database because: ' . mysql_error());
	mysql_select_db ("databasename");
	//}
	
	//query database for information
	$sql = "SELECT ohsaabdiv1.Event,ohsaabdiv1.Effort,ohsaabdiv1.Name,ohsaabdiv1.School,ohsaabdiv1.Location,ohsaabdiv1.Year
			FROM ohsaabdiv1
			ORDER BY ohsaabdiv1.Event ASC";
	$q = $dbh->query($sql);
	if (DB::iserror($q)){
		die($q->getMessage());
	}
	
	//generate the table
	 while ($q->fetchInto($row)){
	?>
	<tr><TD class=text3 onmouseover="this.style.background='#CCCCCC'" onmouseout="this.style.background='#efefef'"><?= $rowї0] ?></td>
		<TD class=text3 onmouseover="this.style.background='#CCCCCC'" onmouseout="this.style.background='#efefef'"><?= $rowї1] ?></td>
		<TD class=text3 onmouseover="this.style.background='#CCCCCC'" onmouseout="this.style.background='#efefef'"><?= $rowї2] ?></td>
		<TD class=text3 onmouseover="this.style.background='#CCCCCC'" onmouseout="this.style.background='#efefef'"><?= $rowї3] ?></td>
		<TD class=text3 onmouseover="this.style.background='#CCCCCC'" onmouseout="this.style.background='#efefef'"><?= $rowї4] ?></td>
		<TD class=text3 onmouseover="this.style.background='#CCCCCC'" onmouseout="this.style.background='#efefef'"><?= $rowї5] ?></td>
	</tr>
	<?php
	 }
	?>
Again I just used an example out of the book so if you have a better idea by all means let me know...

Here's the site where I'm just doing the testing.

Thanks for your help!

blkshirt

Posted: Tue Oct 19, 2004 8:10 pm
by LostMyLove

Code: Select all

$q = new ClassName;
u need to open the class :P

Posted: Tue Oct 19, 2004 8:13 pm
by blkshirt
LostMyLove wrote:

Code: Select all

$q = new ClassName;
u need to open the class :P
The example in the book doesn't have that... So was it an error by the publisher??? :?

If so, where does it go in my code? I'm a total noob when it comes to this... haha

Posted: Tue Oct 19, 2004 8:19 pm
by LostMyLove
its that...
you using OOP programming, but don't declared and class or trying to use OOP offside an class....
soo u need to declare the class, or if its declare, call it in my example

Code: Select all

$q = new ClassName]
where the ClassName (auto-explanatory) is the class that the function u need are!

Posted: Wed Oct 20, 2004 7:23 pm
by LostMyLove
read more about oop on php.net:

http://br2.php.net/oop

[php_man]oop[/php_man]

Posted: Wed Oct 20, 2004 7:30 pm
by blkshirt
I got it resolved it displays the data, now I have to play around with some html layout, which isn't a big deal...

Here's the end user view

Here's the code in case anybody is interested...

Code: Select all

<?php
	//connect to database
	$dbh = mysql_connect ("localhost", "dbname", "dbpass") or die ('I cannot connect to the database because: ' . mysql_error());
	//	or die ('Could not connect: ' .mysql_error());
	mysql_select_db ("dbname");
	
	//Perform SQL Query
	$query = "SELECT Event, Effort, Name, School, Location, Year FROM `ohhsaabdiv1` ORDER BY Event ASC";
	$result = mysql_query($query) or die ('SQL query failed: ' . mysql_error());
	
	
	//Printing results to html
	echo "<table width=760 border=1 align=center cellPadding=2 cellSpacing=0 borderColor=#5a5a5a bgColor=#efefef>\n";
	while ($line = mysql_fetch_array($result)) {
		echo "<center>
				<TR>
				<TD>$line[0]</TD>
				<TD>$line[1]</TD>
				<TD>$line[2]</TD>
				<TD>$line[3]</TD>
				<TD>$line[4]</TD>
				<TD>$line[5]</TD>	
				</TR>
			</center>";
			}
		echo "</table>\n";
		
	//Free result set
	mysql_free_result($result);
	
	//Close Connection String
	mysql_close($dbh)

?>