动态查询数据库以检查值 [英] Dynamically Query a Database to check for value

查看:145
本文介绍了动态查询数据库以检查值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要动态检查我的mysql数据库中是否有某个值"SheetNumber".我已经在受欢迎的网站上看到了它的实现,并且我正在尝试找到一个很好的教程为我概述它.我对Java相当陌生,但我了解很多PHP.

I need to check if a certain value "SheetNumber" is in my mysql database dynamically. I have seen it done on popular web sites and I am trying to find a good tutorial to outline it for me. I am fairly new to Javascript but i know a lot of PHP.

我很想学习如何做到这一点.

I am anxious to learn how to do this.

  • 克里斯

推荐答案

您将需要使用Ajax进行此操作.我建议使用 Jquery 库.使用Jquery文档安装它,然后使用类似以下的内容:

You will need to do this using Ajax. I recommend the Jquery library. Install it using the Jquery documentation, and then use something like the following:

Javascript:

function makeAjaxRequest()
{
   var url="script-that-checks-db.php";
   $.get(url,{},verifyDb);
}

function verifyDb(response)
{
    if (response==1)
    {
       //The value exists, do what you want to do here
    }

    else
    {
      //The value doesn't exist
    }
}

当有人单击链接,单击按钮或其他任何东西时,您可以调用makeAjaxRequest(),例如:

You can have makeAjaxRequest() invoked when someone clicks a link, clicks a button, or anything else, e.g:

<a href="#" onclick="makeAjaxRequest();">Check database</a>

文件script-that-checks-db.php的php代码(当然,将其命名为不同的名称)将负责检查数据库.代码看起来像这样.

The php code of the file script-that-checks-db.php (of course, name it something different) will be responsible for checking the db. The code would look something like this.

PHP:

<?php

//Do the mysql query and find out if the value exists or not.

if ($exists==true)
   echo "1"; //1 will indicate to javascript that the value exists.
else
   echo "0";
?>

您还可以在这里使用JSON而不是0/1方法,但是因为您是新手,所以我认为这对您来说足够简单.

You could also use JSON here rather than the 0/1 method, but because you are new I think this will be simple enough for you.

希望,如果您有任何疑问,请随时帮忙.另外,请随时更改功能和文件名.

Hope this helps, if you have any questions feel free to ask. Also, feel free to change the function and file names.

这篇关于动态查询数据库以检查值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆