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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts

    Can someone tell what's wrong in this code

    Hello

    I have Sql Tabel.
    PHP Code:
    CREATE TABLE comment (
      
    xid int(11NOT NULL auto_increment,
      
    name varchar(100NOT NULL default '',
      
    email varchar(100NOT NULL default '',
      
    font_color varchar(100NOT NULL default '',
      
    comment mediumtext NOT NULL,
      
    submitter int(11NOT NULL default '0',
      
    ip varchar(100NOT NULL default '',
      
    date int(10NOT NULL default '0',
      
    PRIMARY KEY  (xid)
    TYPE=MyISAM
    This for post Comments

    Now i use this to try get the total of Comments

    PHP Code:
    $sql =mysql_query("SELECT * FROM comment")
      or die(
    "can't select the comment");
       
    $total_comment=mysql_affected_rows($sql); 
    But i get this error

    PHP Code:
    Warningmysql_affected_rows(): supplied resource is not a valid MySQL-Link resource in file 
    i tried to use another function but same error
    PHP Code:
    $total_ehdaa=mysql_num_rows($query); 
    For the Records . wht i'm trying to do is , delete a Number of Comment in Certin limits

    Full code
    PHP Code:
    $limit"1";


     
    $sql =mysql_query("SELECT * FROM comment")
      or die(
    "can't select the comment");
       
    $total_ehdaa=mysql_affected_rows($sql);


                
    $diff$total_ehdaa $limit ;

                   if (
    $diff<=0) { echo"No old Comment";}else{
       
    $query2mysql_query("delete from comment LIMIT $diff") or die ("cann't delete");


      if (!
    $query2){
      echo
    "<center>There SOme errors </center><META HTTP-EQUIV=\"Refresh\" Content=3;URL=\"index.php\">";
      }else{
      echo
    "<center>Delted!!<br>Will Take you Back to where u come from</center><META HTTP-EQUIV=\"Refresh\" Content=5;URL=\"index.php\">";
        }
     } 
    Last edited by ajloun; 09-11-2009 at 08:38 AM.

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    $sql =mysql_query("SELECT * FROM comment"
      or die(
    "can't select the comment"); 
       
    $total_comment mysql_fetch_array($sql); 

  • Users who have thanked Phil Jackson for this post:

    ajloun (09-11-2009)

  • #3
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    Thx Phil

    The Error Gon But Now is Fatal Error
    PHP Code:
    Fatal errorUnsupported operand types in delete.php on line 21 
    and wht is in line 21 is :
    PHP Code:
    $diff$total_ehdaa $limit 
    wht i try to do is get the total # of comments then Minus it from whatever $limit set to equal ..
    Last edited by ajloun; 09-11-2009 at 10:22 AM.

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    echo out both of these variables and see what you get.

    EDIT: wait there

  • #5
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    New Update .. it seem it was Right the first i did it but the error was with the Connection to Mysql . i fixed the Connection and now it works . with the same way i posted it first .


    the problem Now is that Code suppose to delete the old Comment but it deletes the new Ones .. any idea
    Thx Phil
    Last edited by ajloun; 09-11-2009 at 10:43 AM.

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,555
    Thanks
    57
    Thanked 148 Times in 147 Posts
    if you have `id` set in your table you could try this:

    PHP Code:
    $limit"1"


    $sql mysql_query("SELECT * FROM comment order by id DESC") or die("can't select the comment"); 
    $total_ehdaa mysql_affected_rows($sql); 

    $diff$total_ehdaa $limit 

    if (
    $diff<=0

        echo
    "No old Comment";
    }
    else

        while(
    $array mysql_fetch_array($sql))
        {
            
    $commentID $array["id"];
            
    $query2mysql_query("DELETE FROM `comment` WHERE id='$commentID'") or die ("cann't delete"); 
            if (!
    $query2)
            { 
                echo
    "<center>There SOme errors </center><META HTTP-EQUIV=\"Refresh\" Content=3;URL=\"index.php\">"
            }
            else
            { 
                echo
    "<center>Delted!!<br>Will Take you Back to where u come from</center><META HTTP-EQUIV=\"Refresh\" Content=5;URL=\"index.php\">"
            } 
            break;
        }


  • #7
    Regular Coder
    Join Date
    Aug 2009
    Posts
    215
    Thanks
    74
    Thanked 0 Times in 0 Posts
    Thx Phil But that didt Work quit well , it deletes One Comment a time .. wht i want is delete all the Old ..and keep only the last New 5 comments..
    and had some problem with useing
    PHP Code:
    mysql_fetch_array 
    so i had to use
    PHP Code:
    mysql_num_rows 
    the tabel uses ID i rever to it as xid .. this is my Tabel

    PHP Code:
    CREATE TABLE comment (
      
    xid int(11NOT NULL auto_increment,
      
    name varchar(100NOT NULL default '',
      
    comment mediumtext NOT NULL,
      
    submitter int(11NOT NULL default '0',
      
    ip varchar(100NOT NULL default '',
      
    date int(10NOT NULL default '0',
      
    PRIMARY KEY  (xid)
    TYPE=MyISAM
    Now the the delete file , and Works But somthing not right , this stupid code i Modfied from the first Code i posted , Deletes Randomly Comments .. Sometime the oldest sometime the Newest .. i do not know wht i'm messing here..

    PHP Code:
    <?php
     $con
    =mysql_connect($host,$dbuser,$dbpass)
     or die(
    "CONNECTION FAILED");
     
    mysql_select_db($dbname,$con)
     or die(
    "can't conect to DB");

     
    $limit"5";  // will be left and delete the Oldest
     
     
    $query =mysql_query("SELECT * FROM comment")
      or die(
    "can't select the comment");
       
    $total_ehdaa=mysql_num_rows($query);


                
    $diff$total_ehdaa $limit ;

                   if (
    $diff<=0) { echo"No Old Comment";}else{
       
    $query2mysql_query("delete from comment LIMIT $diff") or die ("cann't delete");


      if (!
    $query2){
      echo
    "<center>There is Some error </center><META HTTP-EQUIV=\"Refresh\" Content=3;URL=\"index.php\">";
      }else{
      echo
    "<center>Deleted<br>Taking U back </center><META HTTP-EQUIV=\"Refresh\" Content=5;URL=\"index.php\">";
        }

    }
     
    ?>
    Last edited by ajloun; 09-11-2009 at 01:05 PM.


  •  

    Posting Permissions

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