Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You do not need to do a select to check existence and then choose either Update or Insert.</p> <p>You can use MySQL's <a href="http://dev.mysql.com/doc/refman/5.5/en/insert-on-duplicate.html" rel="nofollow"><code>ON DUPLICATE KEY UPDATE</code></a> Feature like this.</p> <pre><code>$query = 'INSERT INTO i_visited (userid,tid,dateline) VALUES (' . $user['id'] . ',' . intval($_GET['id']) . ', unix_timestamp(now())) ON DUPLICATE KEY UPDATE dateline = unix_timestamp(now())'; mysql_query($query); </code></pre> <p>This query will insert a new row if there is now <code>KEY</code> conflict, and in case a duplicate key is being inserted, it will instead execute the update part.</p> <p>And as you have a <code>KEY userid (userid,tid)</code> in your CREATE Statement the above query is equivalent to your <code>if...else</code> block.</p> <p>Try this and see if there are any gains</p> <p>You can also use <code>REPLACE INTO</code>, as there are only the specified 3 columns, like this</p> <pre><code>$query = 'REPLACE INTO i_visited (userid,tid,dateline) VALUES (' . $user['id'] . ',' . intval($_GET['id']) . ', unix_timestamp(now()))'; mysql_query($query); </code></pre> <p>But I would suggest looking at <code>ON DUPLICATE KEY UPDATE</code> as it is more flexible, as it can be used on a table with any number of columns, whereas <code>REPLACE INTO</code> would only work in some limited cases as other column values would also need to be filled in the <code>REPLACE INTO</code> statement unnecessarily </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload