First of all i think your doing it all wrong .. and you should post here the sql table to check out what you got in there. if you don't want to have duplicates in mysql you could use a unique field that will help you keep your data unique in the table. and it might be a duplicate of Find duplicate records in MySQL
Onther way of doing ( i'm not sure from where i have this snippet, never used it so far i had another one )
SELECT my_column, COUNT(*) as count
FROM my_table
GROUP BY my_column
HAVING COUNT(*) > 1
Here is a tutorial about this problem Find duplicates example
And if you would use google a little Google find mysql duplicates
Another i think this one is the most simple way of doing it.
SELECT * FROM table WHERE name = '$somevalue';
And in php you would have something like this
if($result) {
throw PDOException("Sorry Mate, Can\'t Do This Insert Duplicate Found");
} else {
// Let's make a run for it and insert the new data.
}
My example is just for the purpose of the post of course you should do it a little bit more elegant but my way works just fine :D
Example for mysql select like
I have the following products in my table sorry it's not in english but you will get the ideea i hope.
$bex_products = array(
array( // row #0
'ProductName' => 'Hartie Copiator A4 Artist ',
),
array( // row #1
'ProductName' => 'Hartie Copiator A4 Xerox Bussines',
),
array( // row #2
'ProductName' => 'Hartie Copiator A3 Xerox Bussines',
),
array( // row #3
'ProductName' => 'Hartie Copiator A3 Maestro',
),
array( // row #4
'ProductName' => 'Hartie Copiator A3 Artist ',
),
array( // row #5
'ProductName' => 'Hartie Copiator A3 Maestro',
),
array( // row #6
'ProductName' => 'Hartie Copiator A3 Xerox Bussines',
),
array( // row #7
'ProductName' => 'Hartie Copiator A4 Artist ',
),
array( // row #8
'ProductName' => 'Hartie Copiator A4 Global',
),
array( // row #9
'ProductName' => 'Hartie Copiator A4 Xerox Bussines',
),
array( // row #10
'ProductName' => 'Hartie Copiator Color A4, 160 Gr, - Culori Pale',
),
array( // row #11
'ProductName' => 'Hartie Copiator Color A4, 80 Gr, - Culori Pale',
),
);
For that output i used the following SQL:
SELECT ProductName FROM bex_products WHERE ProductName LIKE 'Hartie Copiator%'
As you can see it returned me all the rows containing the string 'Hartie Copiator' not only one row as i think you would expect too, and that would cause a fail in your code logic that will almost always return a false positive if you get more than few entries in your database.
Output None:
For this output i used the following SQL:
SELECT ProductName FROM bex_products WHERE ProductName LIKE BINARY 'Hartie copiator%'
This one will not return a result because i used binary comparasion and 'Hartie Copiator' !== 'Hartie copiator' because i don't have the 'C' i have 'c'
And now if i do for an exact match:
SELECT ProductName FROM bex_products WHERE ProductName LIKE 'Hartie Copiator A4 Artist%'
Will return
$bex_products = array(
array( // row #0
'ProductName' => 'Hartie Copiator A4 Artist ',
),
array( // row #1
'ProductName' => 'Hartie Copiator A4 Artist ',
),
);
And as you can see i have a duplicate entry in the mysql for this product ( that's because they were imported from an excel table and who wrote them added twice there but this will be fixed when we finish with adding products and everything)
For the php part an example.
public function UserNameExists($username) {
$this->database->query('SELECT UserName FROM tbl_users WHERE UserName = :username');
$this->database->bind(':username', $username);
if($this->database->single()) {
return true;
}
return false;
}
I'm sorry but i'm not very good at coding so far.. still learning and some people might find this method written in a bad way ( maybe ) but anyway .. for me it works and .. if it works it's ok. if it's a bad aproach please comment.
Anyway what i do there. First i'm using prepared statements then i make a select from table users to check if any record exists there that is equal to the username i got from the form that the user submited. i make the biding then i ask for a single result from the database.
The return value of this function on success depends on the fetch type. In all cases, FALSE is returned on failure.
That's from the php manual. well now that we know it returns true or false we can check with an if it's true return true; or "global" return false. and then i use like this:
if($this->userHelper->UserNameExists('JoeDoe') {
// set error and show it to the user
} else {
//run code here
}
I hope this helps you understand the idea with select like and select exact same thing.