对mysql数据库中的某些数据执行php检查

对mysql数据库中的某些数据执行php检查,php,mysql,Php,Mysql,我正在尝试使用if函数(或一些熟悉的函数)来检查特定字段、特定行中的特定数据 大概是 include ('db_connection_data.php') mysql_connect($hostname,$username,$password); @mysql_select_db($dbname) or die( "Unable to select database"); $select = mysql_query("SELECT my_field FROM your_table WHERE

我正在尝试使用if函数(或一些熟悉的函数)来检查特定字段、特定行中的特定数据

大概是

include ('db_connection_data.php')

mysql_connect($hostname,$username,$password);
@mysql_select_db($dbname) or die( "Unable to select database");

$select = mysql_query("SELECT my_field FROM your_table WHERE name = 'John' AND Unique_id = "54213568545145554542365647862")

while($my_var = mysql_fetch_array($select)) { $name = $my_var["name"]; $unique_id = $my_var["unique_id"]; }

//here's where i get lost...

    include ('sponsor_details.php')

if ($sponsor_name = $name AND $sponsor_id = $unique_id)
//if the info is correct in the database, carry on processing the rest of the script
这就是我所能想到的,我知道这可能是错的。我希望php检查数据库中的数据是否正确(与“赞助商_details.php”中的数据相同)。如果数据正确,它可以完成对脚本其余部分的处理,但如果数据不正确,则向用户回显一条解释情况的短消息

有什么想法吗


谢谢

在示例代码中,您不是在比较值,而是在分配值

更改此项:

if ($sponsor_name = $name AND $sponsor_id = $unique_id)
比较:

if ($sponsor_name == $name AND $sponsor_id == $unique_id)

如果您将与db进行比较的数据来自赞助商_details.php,则应在select语句之前包含该数据。

两件事:

// you did not properly quote 54213568545145554542365647862
$select = mysql_query("SELECT my_field FROM your_table WHERE ".
             // add semi-colon
             "name = 'John' AND Unique_id = '54213568545145554542365647862'");

// no need for while($my_var = mysql_fetch_array($select)). Just do:
$my_var = mysql_fetch_array($select);
// note: these should be 'John' and 54213568545145554542365647862 
// (you hard-coded them into the query).
$name = $my_var["name"]; $unique_id = $my_var["unique_id"];

//You are using assignment when you probably mean equality test
// note '==' instead of '=', more on this below
if ($sponsor_name == $name AND $sponsor_id == $unique_id)
PHP对“=”有三种用法。一是指赋值$a=$b将$a设置为$b的值。第二个是“松散相等”:$a==b意味着$a等于$b,但对于TRUE==1、FALSE==NULL和(最卑鄙的)0==FALSE等情况,这也将返回TRUE。最后的意思是严格相等——当“松散相等”为0==FALSE返回true时,为0==FALSE返回FALSE

你在条件句中使用它的方式相当于说:

$sponsor_name = $name; $sponsor_id = $unique_id;
// NOTE: && means AND.
if( $sponsor_name && $sponsor_id )
// also could be: if( $name && $unique_id )