Checking if record already exists in MySQL?

Discussion in 'PHP' started by misterdh, Jul 26, 2010.

  1. #1
    Hi

    I want to check if a record already exist in MySQL database. If it does it shuld echo "It does exist" and if it dont it shuld do nothing..

    Now I figured out the following code but it will not work or give me a error message :
    if(mysql_num_rows(mysql_query("SELECT * FROM engines WHERE keyword = '$GKeyword'"))){
    echo "Its there";
    }
    PHP:
    Can anyone help me? :confused:
     
    misterdh, Jul 26, 2010 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    
    $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword'") or die(mysql_error());
      if(mysql_num_rows($exists) == 1) { // 1 or 0 and change the echo's around
    echo "It does exist";
      } else {
    //echo "It doesn\'t exist";  
     }
    
    PHP:
     
    Last edited: Jul 26, 2010
    MyVodaFone, Jul 26, 2010 IP
  3. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Nice thank you :)

    How can I make it stop if it finds a match?

    I tried exit; but it wouldnt work ..

    What I am trying to do is to make it not add any new table if it finds a copy of "keyword"
     
    misterdh, Jul 26, 2010 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    Without seeing your code I have no idea what your doing.... but if its a mysql thing you could add a LIMIT

    
    $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword' LIMIT 0, 10") or die(mysql_error());
    
    PHP:
    Should only get the first 10 results
     
    MyVodaFone, Jul 26, 2010 IP
  5. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No I am adding records to the table..

    And if the value already exist I dont want it to add anything..

    Here is the code:

    
    <?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    include("../password_protect.php");
    
    include ("../../connect.php");
    
    $GName = $_POST['name'];
    $GUrl = $_POST['baseurl'];
    $GSearchurl = $_POST['searchurl'];
    $GLanguages = $_POST['languages'];
    $GKeyword = $_POST['keyword'];
    
    $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword'") or die(mysql_error());
      if(mysql_num_rows($exists) == 1) { // 1 or 0 and change the echo's around
    echo "It does exist";
      } else {
    //echo "It doesn\'t exist";  
     }
    
    $write = mysql_query("INSERT INTO engines VALUES ('', '$GName', '$GUrl', '$GSearchurl', '$GLanguages', '$GKeyword')");
    
    
    echo "<br><br>The following data has been added to the database:<br><br><b>Name:</b> $GName<br><b>URL:</b> $GUrl<br><b>Search URL:</b> $GSearchurl<br><b>Language Code:</b> $GLanguages<br><b>Keyword:</b> $GKeyword<br><br><a href='http://www.gedoo.com/admin/addtable'>Go back</a>";
    PHP:
     
    misterdh, Jul 26, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    I just moved your $write code under if it does exist so if it doesn't then your code will input it to the database, is that what you wanted ?

    
    <?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    include("../password_protect.php");
    
    include ("../../connect.php");
    
    $GName = $_POST['name'];
    $GUrl = $_POST['baseurl'];
    $GSearchurl = $_POST['searchurl'];
    $GLanguages = $_POST['languages'];
    $GKeyword = $_POST['keyword'];
    
    $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword'") or die(mysql_error());
      if(mysql_num_rows($exists) == 1) { 
    echo "It does exist";
      } else {
    $write = mysql_query("INSERT INTO engines VALUES ('', '$GName', '$GUrl', '$GSearchurl', '$GLanguages', '$GKeyword')");  
    echo "<br><br>The following data has been added to the database:<br><br><b>Name:</b> $GName<br><b>URL:</b> $GUrl<br><b>Search URL:</b> $GSearchurl<br><b>Language Code:</b> $GLanguages<br><b>Keyword:</b> $GKeyword<br><br><a href='http://www.gedoo.com/admin/addtable'>Go back</a>";
     }
    
    PHP:
     
    Last edited: Jul 26, 2010
    MyVodaFone, Jul 26, 2010 IP
  7. human

    human Peon

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Make it $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword' LIMIT 1") or die(mysql_error());
    because you check if it is = to 1 and if they are more than one results, it will say that it doesn't exists.
     
    human, Jul 26, 2010 IP
  8. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Personally, I wouldn't do this with PHP. Let MySQL handle it. Create a unique index on the keyword field, then use 'insert ignore'.
    If you try to insert a row that has the same keyword, MySQL will quietly discard it :).
     
    Deacalion, Jul 26, 2010 IP
  9. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #9
    @human in that case do it this way...
    
    <?php
    ini_set('display_errors', 1);
    error_reporting(E_ALL);
    
    include("../password_protect.php");
    
    include ("../../connect.php");
    
    $GName = $_POST['name'];
    $GUrl = $_POST['baseurl'];
    $GSearchurl = $_POST['searchurl'];
    $GLanguages = $_POST['languages'];
    $GKeyword = $_POST['keyword'];
    
    $exists = mysql_query("SELECT keyword FROM engines WHERE keyword = '$GKeyword'") or die(mysql_error());
      if(mysql_num_rows($exists) == 0) {
    $write = mysql_query("INSERT INTO engines VALUES ('', '$GName', '$GUrl', '$GSearchurl', '$GLanguages', '$GKeyword')"); 
    echo "<br><br>The following data has been added to the database:<br><br><b>Name:</b> $GName<br><b>URL:</b> $GUrl<br><b>Search URL:</b> $GSearchurl<br><b>Language Code:</b> $GLanguages<br><b>Keyword:</b> $GKeyword<br><br><a href='http://www.gedoo.com/admin/addtable'>Go back</a>";
      } else {
    echo "The Keyword already exist"; 
     }
    
    PHP:
     
    MyVodaFone, Jul 26, 2010 IP
  10. misterdh

    misterdh Peon

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks MyVodaFone :)

    Its working now!
     
    misterdh, Jul 26, 2010 IP
  11. vocongdanh

    vocongdanh Member

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #11
    You have use this code !

     
    vocongdanh, Jul 27, 2010 IP