SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error in CMS "Edit" script using class MySQL in SPLIB?

    I am converting a CMS from a procedural script to OOP using MySQL in SPLIB. Part of the edit articles script displays category checkboxes with the appropriate ones checked. The old procedural script works fine, but the OOP script displays a warning message when mysql_num_rows tries to give the number of entries in a result set.

    It is supposed to work that if the result contains one or more rows, the condition below will evaluate to true to indicate that the article does belong to the category, and the checkbox should be checked.
    PHP Code:
     if (mysql_num_rows($result)) 
      {
          echo(
    "<input type='checkbox' checked name='cats[]' value='$cid' />$cname<br />\n");
        } else {
          echo(
    "<input type='checkbox' name='cats[]' value='$cid'>$cname<br />\n");
        } 
    The error message is:
    "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/clickbas/public_html/test_v1/admin/edit_article.php on line 211"

    Line 211 is:

    PHP Code:
    if (mysql_num_rows($result
    The code for providing the basis for $result is:

    PHP Code:
    // Get list of categories of article/text for checkboxes.

    //The query
        
    $cats "SELECT category_id, cat_name FROM categories" ;
    //Perform the query
      
    $cat=$db->query($cats);
      
    // Fetch the results and display in form
    $row $cat->fetch();
    $cid $row['category_id'];
    $cname htmlspecialchars($row['cat_name']);

    // Check if the text/ article is in this category
    //The query 
      
    $result "SELECT * FROM art_lookup " .
           
    "WHERE art_id=$id AND cat_id=$cid;
        
    //Perform the query
      
    $result=$db->query($result); 
    Any clues about what is causing the warning?
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  2. #2
    SitePoint Enthusiast EspenA's Avatar
    Join Date
    May 2004
    Location
    Norway, Oslo
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi paulcj2,

    I don't know how the db class works (especially the $db->query function), but it's interesting if the query successfully were executed without any problems. If not (and the $db->query function hide all errors), mysql_num_rows will also fail.

    What does "if (mysql_num_rows($result)) " mean? "Execute the code below if $result returned more than one hit"? If yes, try this code:

    PHP Code:
    if (mysql_num_rows($result) > 0) { 

  3. #3
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Espen,

    I tried

    PHP Code:
    [font=Courier New][color=#007700]if ([/color][color=#0000bb]mysql_num_rows[/color][color=#007700]([/color][color=#0000bb]$result[/color][color=#007700]) > [/color][color=#0000bb]0[/color][color=#007700]) { [/color][/font] 
    and got the same warning message.

    Please ignore the font tags. The forum code adds that. I used the same code you suggested.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  4. #4
    SitePoint Enthusiast EspenA's Avatar
    Join Date
    May 2004
    Location
    Norway, Oslo
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi paulcj2,

    I think I've found the problem.

    PHP Code:
      $result "SELECT * FROM art_lookup " .
           
    "WHERE art_id=$id AND cat_id=$cid
    Try this one:

    PHP Code:
    $result "SELECT * FROM `art_lookup` WHERE `art_id` = '$id' AND `cat_id` = '$cid'"
    Are you sure that $id contains a value? You can also try to change:

    PHP Code:
    //Perform the query
      
    $result=$db->query($result); 
    To:

    PHP Code:
    //Perform the query
    $result=$db->query($result) or die(mysql_error()); 

  5. #5
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Espen, thanks for all your suggestions. I tried them all, but still got the warning message.

    Concerning the value of 'id', it gets its value from further up in the script at:

    PHP Code:
     $id $HTTP_GET_VARS["id"]; 
    I double checked by putting in a new variable...

    PHP Code:
     $id2 '1'
    ...just above the query as you rewrote it changing only 'id' to 'id2'.

    PHP Code:
     $result "SELECT * FROM `art_lookup` WHERE `art_id` = '$id2' AND `cat_id` = '$cid'"
    I also tried...

    PHP Code:
     $result=$db->query($result) or die(mysql_error()); 
    ...and still got the same Warning message:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/clickbas/public_html/test_v1/admin/edit_article.php on line 281
    Line 281 is now:

    PHP Code:
     if (mysql_num_rows($result) > 
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  6. #6
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    New Jersey
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check if your queries return anything, using phpMyAdmin or the mysql command line. It looks like one of the fields (art_id, cat_id) or the table name (art_lookup) may be misspelled or not properly formatted (upper and lower case).

    Also, the > 0 clause is not needed, as if there are no rows returned, mysql_num_rows() will return 0, causing the expression to be false (the equivalent of if (0) {). Otherwise, it'll be the same as if (4) {, where 4 is the return value from mysql_num_rows(), which is true.

  7. #7
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    plugged, I checked the database table and fields. All spelled and formatted correctly. I changed nothing in the query from when I was using it successfully in procedural code. However, when I printed the query to the screen...

    PHP Code:
     $result=$db->query($result); 
      echo(
    ' SQL: ' $result); 
    it printed out...

    SQL: Object
    instead of what I would expect. Wonder why "Object?"
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  8. #8
    SitePoint Enthusiast EspenA's Avatar
    Join Date
    May 2004
    Location
    Norway, Oslo
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It prints "Object" because your're overwriting the $result variable. Try this:

    PHP Code:
    echo(' SQL: ' $result);
    $result $db->query($result); 

  9. #9
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks EspenA,

    I did as you suggested, I tried...

    PHP Code:
     echo(' SQL: ' $result); 
      
    $result $db->query($result); 
    with the result

    SQL: SELECT * FROM `art_lookup` WHERE `art_id` = '1' AND `cat_id` = '1'
    There is in the datbase art_lookup a record WHERE `art_id` = '1' AND `cat_id` = '1'

    Nevertheless, I get the message:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/clickbas/public_html/test_v1/admin/edit_article.php on line 295
    Line 295 is:

    PHP Code:
     if (mysql_num_rows($result) ) 
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  10. #10
    SitePoint Enthusiast EspenA's Avatar
    Join Date
    May 2004
    Location
    Norway, Oslo
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi paulcj2,

    Hmm, strange... What if you try to change:

    PHP Code:
    $result $db->query($result); 
    To:

    PHP Code:
    $result mysql_query($result) or die(mysql_error()); 
    ?

  11. #11
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That got rid of the warning message, but produced the following message instead:

    Fatal error: Call to a member function on a non-object in /home/clickbas/public_html/test_v1/admin/edit_article.php on line 269
    Line 269 is...

    PHP Code:
     if(!$result->isError()) { 
    of the OOP error checking block...

    PHP Code:
     //Check for errors and display status (OOP)
          
    if(!$result->isError()) {
         echo 
    '';
        } else {
          echo 
    '<h2>Error fetching article details</h2>';
       exit();
        } 
    If I comment out that block, the Fatal error message goes away, but only one checked checkbox appears instead of the list of all the category checkboxes.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  12. #12
    SitePoint Enthusiast EspenA's Avatar
    Join Date
    May 2004
    Location
    Norway, Oslo
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi paulcj2,

    Are you sure that you include the file that contains the class (and functions)? It sounds like the script not find the functions (isError, query etc.).

  13. #13
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, I have not changed the include. Seems like the conflict comes between the two types of error generating functions:

    PHP Code:
    $result mysql_query($result) or die(mysql_error()); 
    and

    PHP Code:
      $result $db->query($result); 
     
      
    //Check for errors and display status (OOP)
          
    if(!$result->isError()) {
         echo 
    '';
        } else {
          echo 
    '<h2>Error fetching article details</h2>';
       exit();
        } 
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  14. #14
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Silly me. I left out the "while" loop. It displays all checkboxes now with no errors or warnings. I'll look at the processing part now.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  15. #15
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm. Processing an edit I get the message:

    Error updating article details: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Object' at line 1
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  16. #16
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the print out of the query that supposedly has the error.

    SQL: UPDATE articles SET art_title='Test, test, test edit.' , art_descrip='Test, test, test.' , keywords='Test, test, test.' , art_text='Test, test, test. Changed categories from "current" and "another new category added" to "join us" and "events."' , art_date='2004-05-24' , art_order='1' WHERE article_id=12
    Looks good to me. Why "near 'Object' at line 1 in the above error message?
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  17. #17
    Web Enthusiast
    Join Date
    Jul 2000
    Location
    Western Massachusetts, USA
    Posts
    1,389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got rid of the last message, but the rest of the processing is messed up. Need to work on this more before I ask for more help. Thanks for all your help.
    Paul C.
    ClickBasics
    http://www.clickbasics.com

  18. #18
    SitePoint Zealot
    Join Date
    Mar 2004
    Location
    New Jersey
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're replacing the $result MYSQL RESULT RESOURCE with your own $result Result object. Thus, you can't call mysql_num_rows() passing your $result object but you should be able to use mysql_num_rows($result->getResource()) or whatever getter you use.


Bookmarks

Posting Permissions

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