简体   繁体   English

php中的mysql查询,来自两个表的条​​件更多

[英]mysql query in php with more conditions from two tables

First of all, I am absolute beginner with PHP and SQL. 首先,我绝对是PHP和SQL的初学者。 I have two tables: users(userID, fullname, username, email, pass, userlevel) and games(gameID, userID, club, result, created_time) . 我有两个表: users(userID, fullname, username, email, pass, userlevel) games(gameID, userID, club, result, created_time) users(userID, fullname, username, email, pass, userlevel)games(gameID, userID, club, result, created_time) In table games I have userID , same as in table users , but it's not foreign key. 在桌上games我具有userID ,与桌上users相同,但不是外键。 When I do this query in MySQL it works fine: 当我在MySQL中执行此查询时,它工作正常:

DELETE FROM games 
WHERE EXISTS 
(SELECT * FROM users 
WHERE userlevel=2 
AND users.userID=games.userID)

It removes anything that users.userID matches with games.userID and if that user is userlevel 2 . 它改变了什么users.userID与匹配games.userID ,如果该用户是userlevel 2

I need this in PHP, but only difference would be that userID will match user's ID that is logged,and user will be able to delete only the data that is input with its userID . 我在PHP中需要它,但唯一的区别是userID将与记录的用户ID匹配,并且用户将只能删除使用userID输入的数据。

Also, how to allow everyone else, with userlevel 1 to be able to delete everything in table games no matter who entered on the same submit? 另外,如何允许其他用户(用户userlevel 1删除桌面游戏中的所有内容,而不管谁在同一提交中输入内容?

I have this, but its not working....it keeps givin' me the same error: 我有这个,但它不起作用...。它使我一直遇到相同的错误:

Fatal error: Call to a member function bind_param() on a non-object in C(...) 致命错误:在C(...)中非对象上调用成员函数bind_param()

require ('db_con.php');     
session_start(); 
$userID=$_SESSION["UserID"];

if (isset($_POST['delete'])){                       
        $stmt=$con->prepare("DELETE FROM games WHERE  EXISTS (SELECT FROM users AS u WHERE u.userID = ? AND u.userlevel = 2 "));        

       $stmt->bind_param("s",$_POST['userID']));
       $stmt->execute();
}

Even if I put $userlevel=2 and replace in query u.userlevel = '$userlevel' , it gives the same error... 即使我将$userlevel=2并替换为查询u.userlevel = '$userlevel' ,它u.userlevel = '$userlevel'出现相同的错误...

Any suggestions would be greatly appreciated. 任何建议将不胜感激。 thanx! 谢谢!

userID is an integer value. userID是一个整数值。 Change bind_param line to bind_param行更改为

$stmt->bind_param('i', $user_id);

One reason prepare() can fail is - prepare()失败的原因之一是-

if the sql statement sent to it is not valid in the current DB. prepare() will then return false if the sql statement sent to it is not valid in the current DB. prepare() will then return false . if the sql statement sent to it is not valid in the current DB. prepare() will then return false

Eg - if the table name is not correct or one or more field in the query does not exist . 例如-如果表名不正确或者查询中的一个或多个字段不存在

You got a bracket mismatch: 您的括号不匹配:

    $stmt=$con->prepare("DELETE FROM games WHERE  EXISTS (SELECT FROM users AS u WHERE u.userID = ? AND u.userlevel = 2 "));        

should really be 应该真的是

$stmt=$con->prepare("DELETE FROM games WHERE EXISTS (SELECT * FROM users AS u WHERE u.userID = ? AND u.userlevel = 2 )");

Anyway, the error you received means that $con->prepare did not return an object. 无论如何,您收到的错误意味着$con->prepare没有返回对象。 Most likely it returned false . 它很可能返回false I assume you are using PDO, so according to the docs: 我假设您正在使用PDO,因此根据文档:

If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling). 如果数据库服务器无法成功准备该语句,则PDO :: prepare()返回FALSE或发出PDOException(取决于错误处理)。

So that's the case here. 就是这种情况。 Your MySQL-server was unable to bind the statement. 您的MySQL服务器无法绑定该语句。 In order to debug this better (eg see more helpful error messages) set PDO to throw exceptions on errors. 为了更好地进行调试(例如,查看更多有用的错误消息),请将PDO设置为在错误上引发异常。

You could probably do it like this: 您可能会这样做:

$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

Just add that line right after you connect to the database. 连接到数据库后,只需添加该行即可。

i found the error, here is the wright code: 我发现了错误,这是赖特代码:

$stmt=$con->prepare("DELETE FROM games WHERE EXISTS (SELECT * FROM users WHERE users.userID = ? AND users.userlevel = 2 )"); $ stmt = $ con-> prepare(“从游戏存在的地方删除(选择*从用户的地方WHERE users.userID =?AND users.userlevel = 2)”);

but its not working what i wanted, it deleted all from my games table :D i guess, SELECT * FROM has to be specific! 但它不能正常工作,它从我的游戏表中删除了所有内容:D我猜,SELECT * FROM必须具体!

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

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