2

I have a table

id h_id t_id
 1   3   1
 2   3   2
 3   3   3
 4   4   2
 5   4   3

id is the primary key. I have not created a JTable for this table. Now I want to delete rows by h_id. Are there any method like which I can use without writing a sql DELETE query?

$db = JFactory::getDBO();
$row =& $this->getTable('tablename');
$row->delete($pk);

Any better solution will be greatly appreciated.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sara
  • 14,098
  • 13
  • 34
  • 50
  • 1
    "Are there any mehtod like which I can use without writing a sql delete query?". I think you cannot delete a record or row from a table without using a delete query. Better to go with what you have. – vinay Dec 08 '11 at 16:18
  • 1
    @vinayjg ok If I create a JTable for this table. can I use $row->delete() method but how to mention the field name? bcoz **h_id** is not primary key and it is not a unique key. If this is not possible I will go for a query. any idea? – Sara Dec 08 '11 at 16:29
  • 1
    Again it needs a primary key. I had the same problem when I was at my first project. I used the query like delete a record from the table where condition is blah blah and executed it using query() method. It was suggested by my senior. Still I use the same. – vinay Dec 08 '11 at 17:43

1 Answers1

6
         $db = & JFactory::getDBO();   
         $query = $db->getQuery(true);
         $query->delete($db->nameQuote('tablename'));             
         $query->where($db->nameQuote('h_id').'='.$db->quote($key));             
         $db->setQuery($query);
         $db->query(); 
Sara
  • 14,098
  • 13
  • 34
  • 50