Results 1 to 2 of 2

Thread: How to prevent duplicate entrys in MySql

  1. #1
    Join Date
    Jun 2011

    How to prevent duplicate entrys in MySql

    Okay guys, this might be very simple, but i just cant find the answer.
    What i want to do is prevent users from submiting entries that already exist in the database using php.

    This is the example code i have:


    $mysql = new mysqli('localhost', 'ernesto', 'asdf', 'db') or die('your\'re dead');

    $query = "SELECT * FROM myTable WHERE name='asdf'";

    $result = $mysql->query($query) or die($mysql->error);

    $num = mysql_num_rows($result);

    if($num != 0) {
    echo 'name already exist';
    } else {
    echo 'okay';


    All want is to be sure that if the there is an existing row in name with an entry of 'asdf' (example) then it will echo something like name already exists, but if it doesnt i want it to echo okay. What happens is that it ALWAYS echos okay, in other words it always says there is no row named 'name' with the value of 'asdf'

    Thanks in advanced!!!
    Last edited by javascript?; 07-20-2011 at 09:35 PM.

  2. #2
    Join Date
    Feb 2011
    Try to replace in your code with this:
    PHP Code:
    $num $result->num_rows;

    $num 0) {
    'name already exist';
    } else {

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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