1

Is it possible using mysql and php to check if the values are already inserted inside a ROW instead of COLUMN?

for example:

id       name       surname
1        jonh       smith

is it possible to check if both john and smith are already registered inside the same row?

2
  • Do you mean to check if there is a row where either name or surname is jonh and either name or surname is smith? Commented Jun 5, 2014 at 10:49
  • no, i mean if there is a row that has already jonh as name and smith as surname Commented Jun 5, 2014 at 10:53

8 Answers 8

4

Yes it is. You can just use multiple where clause like this :

SELECT * FROM `users` WHERE `name` = "john" and `surname` = "smith";

For more complex possibilities, read the select documentation.

Sign up to request clarification or add additional context in comments.

6 Comments

Do you think that this is the best way to check if the values of some textboxes are already saved inside the database? or what will you suggest for doing this?
That's one way to do it, the safer in my opinion. Doing the request then checking for error is a lazy and error-prone way of doing anything.
So for example if i have INSERT into "name","surname" VALUES ("textbox.name", "textbox.surname") WHERE "name" = null and "surname" = null Will it work?
@user3668732 That is doing an insert, but the WHERE doesn't have any rows to check the values in the WHERE clause (ie, WHERE is invalid)
You have to check if the row exist first, then do your insert.
|
0
SELECT 1 FROM 'MYTABLE' WHERE 'NAME' = 'JHON' AND 'SURNAME' = 'SMITH'

Comments

0

You can check the count of the rows

select count(id) from table

also you can check if the data has been inserted. reference

$result = mysql_query("INSERT INTO PEOPLE (NAME ) VALUES ('COLE')")); if($result) { echo "Success";

} else { echo "Error";

}

Comments

0

please try this. in this query i added lcase in name and check both side so u can find which one inserted in upper and lower latter words.

$qry="SELECT * FROM Table WHERE LCASE(name) = lcase('john') and LCASE(surname) = lcase('smith')";
$run=mysql_query($qry) or die(mysql_error());
if(mysql_num_rows($run)>0)
{
     echo "already registered";
}

Comments

0

You can check with IN clause

SELECT * FROM `MYTABLE` WHERE (`NAME`, `SURNAME`) IN (('jonh', 'smith'));

Comments

0

You can use GROUP_CONCAT to join all cells in a row:

SELECT * FROM TableA
WHERE FIND_IN_SET('john', GROUP_CONCAT(Id, Name , SurName separator ',')) > 0
AND FIND_IN_SET('smith', GROUP_CONCAT(Id, Name , SurName separator ','))  > 0;

This will not care which column appeared the name JOHN and SMITH

3 Comments

I like the flexibility, but if looking for a full word (so if checking for smith but not wanting it to return smithe) it might be better to use FIND_IN_SET rather than LIKE.
TRUE. Updated my answer, thanks! Very good use of GROUP_CONCAT and FIND_IN_SET. But it will raise performance issue :)
Performance when checking one field or another is not going to be anything pleasant at the best of times!
0

By using this code:

$id=$_POST['id'];

$name=$_POST['name'];


$surname=$_POST['surname'];

$result = mysql_query("SELECT id,name,surname FROM tablename WHERE id='".$id."'name='".$name."' and surname='".$surname."'");


$row = mysql_fetch_array($result);


$cnt = mysql_num_rows($result);



if($cnt == 0)
{ 

    enter code here

(Statement);
}
    else
{ 
(Statement);
}

Comments

0

This sounds like you want to insert a record, but want to check if it exists first.

If you don't care if the insert fails or not (ie, you just want to do the insert and know a value has been inserted) then you could set up a unique index on name and surname.

To do the insert you could then just use INSERT IGNORE blah . Thus if there already is that combination of name and surname the INSERT will silently fail:-

INSERT IGNORE INTO sometable(id, name, surname) 
VALUES(NULL, '$name', '$surname') 

If you need to know the id of the record

INSERT IGNORE INTO sometable(id, name, surname) 
VALUES(NULL, '$name', '$surname') 
ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id)

This will return the id of the new or existing row (as appropriate) in the normal mysql functions to return the inserted row id.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.