SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with mysql_num_rows in PHP

    Hi all, I have the follwing code which doesn't work:

    PHP Code:
    function add_p_category($category) {
        
            
    $qLinkExists "SELECT * FROM al_p_category WHERE category='$category'";
            
    $qInsertLink "INSERT INTO al_p_category(category) 
                                            VALUES ('
    $category')";
                                                     
            if (
    mysql_num_rows($qLinkExists) > 0) {
                return 
    "Already exists";
            }
            else {
                
    $result mysql_query($qInsertLink); 
                return 
    "Category added";
            }
        } 
    // End: function addcategory 
    It inserts into the database fine, but when I already have an entry already in the database, "if (mysql_num_rows($qLinkExists) > 0) {" should pick it up, but it just inserts it anyway. Is there anything wrong with the above code? I couldn't see anything wrong, but someone else maybe able to.

    The other side of the code is this:

    PHP Code:
    <?php 
    if (isset($_POST['submittype'])) {
            
    $submittype $_POST['submittype'];
            
    $id $_POST['id'];
            
    $category $_POST['category'];
            
    $deleteid $_POST['deleteid'];
            
    $groupname $_POST['groupname'];

    $groupupdate = new permissions();
                if (
    $submittype == "new") {
                    
    $perm="add_permissions";
                    require(
    "../user_control/checkperm.php");
                    
    $id $groupupdate->add_permission($groupname$category);
                }
                if (
    $submittype == "edit") {
                    
    $perm="edit_permissions";
                    require(
    "../user_control/checkperm.php");
                    
    $message $groupupdate->modify_permission($id$category);
                }
                if (
    $submittype == "delete") {
                    
    $perm="delete_permissions";
                    require(
    "../user_control/checkperm.php");
                    
    $message $groupupdate->delete_permission($deleteid);
                }
                if (
    $submittype == "newc") {
                    
    $perm="add_perm_category";
                    require(
    "../user_control/checkperm.php");
                    
    $message $groupupdate->add_p_category($category);
                }
                if (
    $submittype == "delc") {
                    
    $perm="delete_perm_category";
                    require(
    "../user_control/checkperm.php");
                    
    $message $groupupdate->delete_p_category($deleteid);
                }
        }
    ?>
     <div id="newc">
      <div align="center">
        <p>&nbsp;</p>
        <p>Category name:</p>
      </div>
      
        <div align="center">
            <form name="addcategory" method="Post" action="main.php?p=showpermission">
            <input type="text" name="category">
        </div>
        <div align="center">
            <input type="hidden" name="submittype" value="newc">
          <input type="Submit" value="Submit">
          <input type="button" value="Cancel" onclick="MM_showHideLayers('newc','','hide')">
                </form>
        </div>
    </div>
    Any help appreciated.

    David

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quoting directly from the PHP manual:
    Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use mysql_affected_rows().
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,347
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    instead of doing a SELECT followed by an INSERT, why not just do a single REPLACE
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Its ok I figured it out.

  5. #5
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Me01273 View Post
    Its ok I figured it out.
    This type of post is not helpful. The beauty of a forum is someone else can learn from your mistakes. If you don't provide the solution when you figure it out, you're wasting everyone's time.

    The problem came because you fed mysql_num_rows() a string (SQL) instead of the result of the query run through mysql_query()


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
  •