PHP Easy mysql查询if语句

时间:2022-11-11 20:43:02

My Code:

mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS);
mysql_select_db(SQL_DB);
$result = mysql_query("SELECT id FROM ".TB_PREFIX."users WHERE tribe = '2'");
if (!$result) {
die("Tribe Error");
}

In this code all user tribes get error. But i want only '2' tribe get error. What is wrong in this code?

在此代码中,所有用户部落都会收到错误。但我只想'2'部落得到错误。这段代码有什么问题?

Thank you.

EDIT: Please edit on my code what is wrong, please.

编辑:请编辑我的代码有什么问题,请。

3 个解决方案

#1


0  

You are checking if the query returned an error, not if the query returned 1 (or more...) rows, which is probably what you want. A query that returns 0 rows is still a valid query.

您正在检查查询是否返回错误,而不是查询返回1(或更多...)行,这可能是您想要的。返回0行的查询仍然是有效查询。

Apart from that you should switch to PDO (or mysqli) and prepared statements with bound variables.

除此之外,您应该切换到PDO(或mysqli)并使用绑定变量准备语句。

In PDO you can probably get the number of returned rows rowCount(), check the manual for more details on how to rewrite your code to PDO.

在PDO中,您可以获得返回行rowCount()的数量,请查看手册以获取有关如何将代码重写到PDO的更多详细信息。

#2


0  

I found this for you : http://php.net/manual/en/mysqli.query.php

我找到了这个:http://php.net/manual/en/mysqli.query.php

Here you can learn how to use mysqli

在这里,您可以学习如何使用mysqli

And the code here under, you can modefy to you own

而这里的代码,你可以模仿你自己

EDIT: this is not the right way but try this

编辑:这不是正确的方法,但试试这个

mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS);
mysql_select_db(SQL_DB);
$result = mysql_query("SELECT id FROM ".TB_PREFIX."users WHERE tribe = 2");
$fetch = mysql_fetch_assoc($result);
if (count($fetch)<1) {
if (!$result) {
die("Tribe Error");
}
else{
var_dump($fetch);
}

#3


0  

Mysqli on PHP official site

PHP官方网站上的Mysqli

Modify the code mentioned below to suit your application

修改下面提到的代码以适合您的应用程序

mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS);
mysql_select_db(SQL_DB);
$result = mysql_query("SELECT id FROM ".TB_PREFIX."users WHERE tribe = 2") or die(mysql_error());
$fetch = mysql_num_rows($result);
if ($fetch > 0) {
    while($dets = mysql_fetch_assoc($result)){
        //displaying all info about current record
        var_dump($dets);
    }
}
else
    echo "No records";

#1


0  

You are checking if the query returned an error, not if the query returned 1 (or more...) rows, which is probably what you want. A query that returns 0 rows is still a valid query.

您正在检查查询是否返回错误,而不是查询返回1(或更多...)行,这可能是您想要的。返回0行的查询仍然是有效查询。

Apart from that you should switch to PDO (or mysqli) and prepared statements with bound variables.

除此之外,您应该切换到PDO(或mysqli)并使用绑定变量准备语句。

In PDO you can probably get the number of returned rows rowCount(), check the manual for more details on how to rewrite your code to PDO.

在PDO中,您可以获得返回行rowCount()的数量,请查看手册以获取有关如何将代码重写到PDO的更多详细信息。

#2


0  

I found this for you : http://php.net/manual/en/mysqli.query.php

我找到了这个:http://php.net/manual/en/mysqli.query.php

Here you can learn how to use mysqli

在这里,您可以学习如何使用mysqli

And the code here under, you can modefy to you own

而这里的代码,你可以模仿你自己

EDIT: this is not the right way but try this

编辑:这不是正确的方法,但试试这个

mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS);
mysql_select_db(SQL_DB);
$result = mysql_query("SELECT id FROM ".TB_PREFIX."users WHERE tribe = 2");
$fetch = mysql_fetch_assoc($result);
if (count($fetch)<1) {
if (!$result) {
die("Tribe Error");
}
else{
var_dump($fetch);
}

#3


0  

Mysqli on PHP official site

PHP官方网站上的Mysqli

Modify the code mentioned below to suit your application

修改下面提到的代码以适合您的应用程序

mysql_connect(SQL_SERVER, SQL_USER, SQL_PASS);
mysql_select_db(SQL_DB);
$result = mysql_query("SELECT id FROM ".TB_PREFIX."users WHERE tribe = 2") or die(mysql_error());
$fetch = mysql_num_rows($result);
if ($fetch > 0) {
    while($dets = mysql_fetch_assoc($result)){
        //displaying all info about current record
        var_dump($dets);
    }
}
else
    echo "No records";