简体   繁体   English

对mysql使用一两个PHP脚本

[英]Use one or two php scripts for mysql

I have one script of php which should check if given user exists in Users table, and if not it creates it, if yes then it updates the existing user with new information. 我有一个php脚本,应该检查Users表中是否存在给定的用户,如果不存在,它将创建它,如果是,则使用新信息更新现有用户。

Should I create all in one sql query to do the checks and insert/update or should I first use one php script to get row count and then use second script to insert/update new user? 我应该在一个sql查询中创建全部以进行检查和插入/更新,还是应该首先使用一个php脚本获取行数,然后使用第二个脚本来插入/更新新用户?

I know SQL but only the basics, so it is not my strong side. 我知道SQL,但仅了解基础知识,因此这不是我的强项。

Also which solution is better towards client/server communication? 还有哪个解决方案更适合客户端/服务器通信?

I really like to use this function: 我真的很喜欢使用此功能:

function exists($detail, $table, $column, $value) {
        $query = mysqli_query($this->connect, "SELECT `$detail` FROM `$table` WHERE `$column` = '$value'");
        $count = mysqli_num_rows($query);

        if($count >= 1) {
            return true;
        } else {
            return false;
        }
    }

So if the user exists it will return true else false. 因此,如果用户存在,它将返回true,否则返回false。 After this check I would run another function / query to update a user. 检查之后,我将运行另一个函数/查询来更新用户。

the above function could look like this: 上面的函数看起来像这样:

//SELECT `username` FROM `users` WHERE `username` = '$username'
if(exists('username', 'users', 'username', $username)) {
   //run this code if true
} else {
   //run this code if false
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM