表格输入校正和数据库查询 - Codeigniter

时间:2022-10-06 19:20:47

Hey guys. I'm re-creating a registration for my website using codeigniter. On my previous page I did it in simple php and HTML. Now that I'm trying to recreate it in codeigniter I seem to be running into difficulties.

大家好。我正在使用codeigniter为我的网站重新创建注册。在我的上一页,我用简单的PHP和HTML做到了。现在我正试图在codeigniter中重新创建它,我似乎遇到了困难。

The following code is a function that is called to validate an email address that the user puts in. It is supposed to query the database and check if the email already exists. If it does, print an error message, if not, everything is hunky dorey.

以下代码是一个函数,用于验证用户输入的电子邮件地址。它应该查询数据库并检查电子邮件是否已存在。如果是的话,打印一条错误信息,如果不是,那么一切都很糟糕。

function email_check($str)
    {
       $num = $this->db->count_all("SELECT email FROM mytable WHERE email='$str'");
        if ($num > 0)
        {
            $this->validation->set_message('username_check', 'Email already in use');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }

However I keep on getting mysql errors. Along the lines of

但是我继续得到mysql错误。沿着这条路线

Error Number: 1064

错误号码:1064

You have an error in your SQL syntax;

您的SQL语法有错误;

or

alternatively when i use

或者当我使用时

function email_check($str)
    {
      $database_email_check = ("SELECT email FROM myTable WHERE email='$str'");
      $email_check = $this->db->query($database_email_check);
      $num = mysql_num_rows($email_check);
        if ($num > 0)
        {

            $this->validation->set_message('username_check', 'Email already in use');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

gives the error

给出了错误

mysql_num_rows(): supplied argument is not a valid MySQL result resource

mysql_num_rows():提供的参数不是有效的MySQL结果资源

Any help would be appreciated. Including a facepalm and correction in my code. I'm convinced it's something stupid that I just can't see.

任何帮助,将不胜感激。在我的代码中包括facepalm和更正。我确信这是我看不到的蠢事。

Thanks

1 个解决方案

#1


The function you're looking for is count_all_results, and you are only supposed to put the table name as your parameter for count_all_results.

您正在寻找的函数是count_all_results,您只应将表名作为count_all_results的参数。

$this->db->where('email', $str);
$num = $this->db->count_all_results('mytable');

See CodeIgniter's Active Record page for more info

有关详细信息,请参阅CodeIgniter的Active Record页面

#1


The function you're looking for is count_all_results, and you are only supposed to put the table name as your parameter for count_all_results.

您正在寻找的函数是count_all_results,您只应将表名作为count_all_results的参数。

$this->db->where('email', $str);
$num = $this->db->count_all_results('mytable');

See CodeIgniter's Active Record page for more info

有关详细信息,请参阅CodeIgniter的Active Record页面