doupang1917 2009-08-03 18:17
浏览 33
已采纳

检查表中是否存在数据

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.

  • 写回答

4条回答 默认 最新

  • dsjz1119 2009-08-03 18:23
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据