hi, im trying to compare the string from my new $_POST['title'] and in my database. If my new post is the same in database, it must notify it is the same. I am trying to use strcmp if it is better. Or maybe anyone has better or simpler code.

$newtitle = $_POST['title'];

$resultdbtitle = mysql_query("SELECT * FROM table WHERE title = {$newtitle}");

while($titledbinfo = mysql_fetch_array($resultdbtitle)){
    $titledb = $titledbinfo['newtitle'];

$checktitle = strcmp($vtitle,$titledb);
if ($checktitle == 0){
} else {
    echo "NOT THE SAME";
4 Years
Discussion Span
Last Post by kanoy83

You can use an the md5() MySQL function and do everything within the query:

"select id from table where md5(title) = md5({$newtitle}) limit 1"

Then just check with mysql_num_rows() > 0. Or better: create a unique index for the title column.

Edited by cereal


interesting cereal...

I get that you are doing that because they would validate, but wouldnt this require that it compares each record by hashing (and reading, anyway) it in order to compare?

How would hashing the string be faster than simply checking if the string is the same, especially when you are dealing with potentially thousands or hundreds of thousands of records?

why not just sanitize the post and then

select * from table where title = 'POST' limit 1

and then, I agree, check for mysql_num_rows() > 0;

This way, I believe, you are allowing the database to work off indexes naturally, and if you create a clustered index on ID then you can just replace * with ID and that should work too...


I get that you are doing that because they would validate, but wouldnt this require that it compares each record by hashing (and reading, anyway) it in order to compare?

Yes, that's the reason why I'm also suggesting to use a unique index over the title column: the insert query will return the SQL state and the user will know that the title is already in use:

$q = $conn->exec('insert into table (title) values("hello world")');
if($conn->errorCode() == 23000) echo 'this title already exists';

More can be done: as removing the punctuation and creating the url slug of the title and use that to create the unique key:

# on mysql client
> alter table tablename add url_slug varchar(255) not null unique;

# on php script
$q = $conn->exec('insert into table (title, url_slug) values("hello world", "hello_world")');
if($conn->errorCode() == 23000) echo 'this title already exists';

Then it depends on the applications features, if you have to deal with big numbers and the slug is not requested, it can be used an hash which has always a fixed length and can be saved as binary:

# on mysql client
> alter table tablename hash binary(16) not null unique;

# on php script
$hash = md5($url_slug);
"insert into table (title, hash) values('$title', unhex('$url_slug'))"

And it should be more efficient than a table scan.



SELECT `somefield` FROM `table` WHERE STRCMP('yourtext', `comparisonfield`) = 0 LIMIT 1;

Whether that would be quicker I don't know.

If you decide to go with a php loop on the whole recordset, then I think
=== is quicker than strcmp(). I'm assuming the SQL will be quicker as it stops querying once it gets a hit - the LIMIT 1. Interesting.


guys, i really appreciate for your suggestions

just for a quick question summary:

I want to add new data in my table however, i want to check if my new data name has already same name, if not same name we can proceed to adding new data/post. if it has same name with $field_title and $newtitle

in my database table:

ID | title | remarks
01 | orange | fruit
02 | mango | fruit


$newtitle = mysql_real_escape_string($_POST['title']);
$newtitle = htmlentities($vtitle);

$sqldbtitle = "SELECT * FROM tblmainfile WHERE strcmp('$newtitle','title') = 0 LIMIT 1;
$getdbtitle = mysqli_query($con,$sqldbtitle);


<td><input type="text" name="title" value="<?php echo $newtitle; ?>"></td>


1. will this work?
2. is there another concise explanation or code that will work??

thanks a lot!


hi got it,

Function: to see and find if data exist in table before Insert
Table structure:
id - auto increment
artist - specific id number
title - unique title

if (isset($submit)) {

    $newtitle =  $_POST['title'];
    $querytitle = mysql_real_escape_string($_POST['title']);
    $queryalbum = mysql_real_escape_string($_POST['album']);

     // col_title is name of column title in table
     // indexid is the id of the table

    $sqldbtitle = "SELECT * from tblmainfile WHERE col_title = '$newtitle' ";
     $checktitle = mysqli_query($con, $sqldbtitle);

    // This is assuming  query only returns one result
     $resultcheck = mysqli_fetch_array($checktitle);

     // now check the value for 'title'
     if ($resultcheck['col_title'] == $newtitle) {

     } else {
          echo "NOT THE SAME TITLE";

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.