Note that there are some explanatory texts on larger screens.

plurals
  1. POCapture mysql database error and use it in php
    primarykey
    data
    text
    <p>I created the following table for user to user subscriptions.</p> <pre><code>CREATE TABLE IF NOT EXISTS `subscriptions` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `subscribed_to` int(11) NOT NULL, `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `unique_subscription` (`user_id`,`subscribed_to`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=76 ; </code></pre> <p>I'm disallowing identical rows by making the columns <code>user_id</code> and <code>subscribed_to</code> unique. If a user tries to resubmit the same data I get:</p> <blockquote> <p>A Database Error Occurred Error Number: 1062</p> <p>Duplicate entry '62-88' for key 'unique_subscription'</p> <p>INSERT INTO subscriptions (user_id, subscribed_to, date) VALUES ('62', '88', '2011-07-11 19:15:13')</p> <p>Line Number: 330</p> </blockquote> <p>I'm preventing the database error by checking if an identical row exists before trying to insert data.</p> <pre><code>$query = "SELECT COUNT(*) FROM subscriptions WHERE (user_id = '62' AND subscribed_to = '88')"; if ($query &gt; 0) { //display already subscribed message } else { //insert new row } </code></pre> <p>The database already checks the table and returns an error. The <code>select count(*)</code> query above seems redundant. Do I really need to check the table once more in my application? Is there a way to capture the database error if it occurs, and do something with that in my application?</p> <p>If you have an idea how please share an example. I haven't a clue how this is done..!</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