...

View Full Version : Small error I can't figure out



blkshirt
10-19-2004, 12:28 PM
I found a little snippet of code I've modified (only to a very small extent) from the book "Programming PHP" by O'Reilly. I get a Fatal error: Call to a member function on a non-object in /home/j9d2k0/public_html/ohhstrack/phptest/index.php on line 34
in the code and I can't figure out why or what it is....

Here's the code itself:



<?php
//connect to the database
$dbh=mysql_connect ("localhost", "j9d2k0_j9d2k0ohh", "<password here>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("j9d2k0_j9d2k0ohhstrack");
//}

//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
}
?>


Here is line 34 on its own:


while ($q->fetchInto($row)){

Any ideas what is wrong with this? I've been trying to figure this out for a day now and it isn't happening... I'm very new to PHP so if you have any advice I'd appreciate it...

Thanks,

blkshirt

marek_mar
10-19-2004, 03:46 PM
The error means that $q is NOT a class therfore can not have methods.

Are you sure that $dbh 's query() method returns a class?

blkshirt
10-19-2004, 11:35 PM
The error means that $q is NOT a class therfore can not have methods.

Are you sure that $dbh 's query() method returns a class?

I don't know, I just retyped the code that was in the book... I just made some slight modifications to the HTML code and that was it...

dswimboy
10-20-2004, 04:01 AM
try adapting this code from www.php.net


<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
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)) {
echo "<TR>
<TD>$line[0]</TD>
<TD>$line[1]</TD>
</TR>";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

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

blkshirt
10-20-2004, 12:26 PM
try adapting this code from www.php.net


<?php
// Connecting, selecting database
$link = mysql_connect('mysql_host', 'mysql_user', 'mysql_password')
or die('Could not connect: ' . mysql_error());
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)) {
echo "<TR>
<TD>$line[0]</TD>
<TD>$line[1]</TD>
</TR>";
}
echo "</table>\n";

// Free resultset
mysql_free_result($result);

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

Thanks, I'll check it out later today!

blkshirt
10-21-2004, 01:50 AM
PERFECT, well kinda... I have some html editing to do it looks like...

Here's the code that worked


<?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)
?>


Result of the code is here (http://www.ohhstrack.com/phptest/index2.php)

Thanks for the help!!!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum