Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Dec 2003
    Location
    Ohio
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Small error I can't figure out

    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 Code:
    <?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:

    PHP Code:
    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

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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?

  • #3
    New Coder
    Join Date
    Dec 2003
    Location
    Ohio
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by marek_mar
    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...

  • #4
    Regular Coder dswimboy's Avatar
    Join Date
    Nov 2003
    Location
    mostly in Ann Arbor
    Posts
    458
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try adapting this code from www.php.net
    PHP Code:
    <?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);
    ?>
    Last edited by dswimboy; 10-20-2004 at 05:02 AM. Reason: forgot about [php] option
    "There is more than one way to do it."

  • #5
    New Coder
    Join Date
    Dec 2003
    Location
    Ohio
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by dswimboy
    try adapting this code from www.php.net
    PHP Code:
    <?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!

  • #6
    New Coder
    Join Date
    Dec 2003
    Location
    Ohio
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PERFECT, well kinda... I have some html editing to do it looks like...

    Here's the code that worked
    PHP Code:
    <?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

    Thanks for the help!!!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •