gege gege - 2 months ago 5
MySQL Question

check if text match php mysql (beginner)

Sorry for this very beginner question... I have a table like

| id | block_1 | block_2|
-------------------------
| 1 | Mark | John |
| 2 | Antony | |
-------------------------


i want check if text in a cell match my variable for example $name:

...
$sql = "SELECT * FROM my_table WHERE id='1'";
$res = mysql_query($sql, $connection);
$row = mysql_fetch_array($res);


$block1 = $row['block_1'];
$name = 'Mark';

if ($block1 == $name)
{
echo 'yes';
}
else
{
echo 'no';
}


but i think my reasoning is wrong, im beginner... all solutions are welcome

Answer

You can count results with requested variables.

$sql = "SELECT COUNT(*) as total FROM my_table WHERE id='1' AND block_1='Mark'";
$res = mysql_query($sql, $connection);
$row = mysql_fetch_assoc($res);
echo $row['total'] > 0 ? 'yes' : 'no'; 
Comments