Jesse Jesse - 1 month ago 5
MySQL Question

checking to see if data exist with in a table

How do i go about looking into a table and searching to see if a row exist. the back gorund behind it is the table is called enemies. Every row has a unique id and is set to auto_increment. Each row also has a unique value called monsterid. the monster id isn't auto_increment.

when a monster dies the row is deleted and replaced by a new row. so the id is always changing. as well the monsterid is changed too.

I am using in php the $_GET method and the monsterid is passing through it,
basically i am trying to do this

$monsterID = 334322 //this is the id passed through the $_GET

checkMonsterId = "check to see if the monster id exist within the enemies table"

if monsterid exist then

{RUN PHP}

else

{RUN PHP}

If you need anymore clarity please ask. and thanks for the help in advance.

Answer

Use count! If it returns > 0, it exists, else, it doesn't.

select count(*) from enemies where monsterid = 334322

You would use it in PHP thusly (after connecting to the database):

$monsterID = mysql_real_escape_string($monsterID);
$res = mysql_query('select count(*) from enemies where monsterid = ' . $monsterid) or die();
$row = mysql_fetch_row($res);
if ($row[0] > 0)
{
    //Monster exists
}
else
{
    //It doesn't
}