[英]Multiple MySQL queries in one PHP file
I'm trying to create a PHP file which can perform different MySQL queries according to the data which is sent via POST. 我正在尝试创建一个PHP文件,该文件可以根据通过POST发送的数据执行不同的MySQL查询。 If I add another case to the switch statement, which executes a MySQL query, I get a fatal error: call to a member function prepare() on null. 如果在执行MySQL查询的switch语句中添加另一种情况,则会发生致命错误:在null上调用成员函数prepare()。 The connection.php includes $conn. connection.php包含$ conn。
I don't really know, why this occurs, so I'd be glad if someone could help :) 我真的不知道为什么会这样,所以如果有人可以帮助我,我会很高兴:)
require_once('connection.php');
function namecheck($conn, $username)
{
try {
$sql = "SELECT username FROM users WHERE username = ?";
$stmt = $conn->prepare($sql);
$stmt->execute(array($username));
$result = $stmt->fetch();
} catch (PDOException $e) {
return $sql . "<br>" . $e->getMessage();
}
$conn = null;
if ($result !== false) {
return True;
} else {
return False;
}
}
if (isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch ($action) {
case 'namecheck': //works
$username = $_POST['username'];
$result = namecheck($conn, $username);
if ($result == True) {
echo 'True';
} elseif ($result == False) {
echo 'False';
}
case 'adduser': //script still works
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$username = $_POST['username'];
$password = $_POST['password'];
try {
$sql = "INSERT INTO users (username, password) VALUES (?, ?)";
$stmt = $conn->prepare($sql);
$stmt->execute(array($username, $password));
} catch (PDOException $e) {
return $sql . "<br>" . $e->getMessage();
}
$conn = null;
case 'test': //Same as above. if added, fatal error
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$username = $_POST['username'];
$password = $_POST['password'];
try {
$sql = "INSERT INTO users (username, password) VALUES (?, ?)";
$stmt = $conn->prepare($sql);
$stmt->execute(array($username, $password));
} catch (PDOException $e) {
return $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
}
You dont have break
statement in your case blocks. 您的case块中没有break
语句。 When "adduser" case is executed, control goes to "test" and $conn
is null there 当执行“ adduser”时,控制权转到“ test”,并且$conn
为空
https://www.tutorialrepublic.com/php-tutorial/php-switch-case-statements.php https://www.tutorialrepublic.com/php-tutorial/php-switch-case-statements.php
Replace case 'test':
with following: 将case 'test':
替换为以下内容:
break;
case 'test':
If I add another case to the switch statement, which executes a MySQL query, I get a fatal error: call to a member function prepare() on null. 如果在执行MySQL查询的switch语句中添加另一种情况,则会发生致命错误:在null上调用成员函数prepare()。
That's because you're executing $conn = null;
那是因为您正在执行$conn = null;
statement in each of your cases and primarily because there's no break;
在每种情况下都要陈述,主要是因为没有break;
statement in your switch-case. 开关盒中的语句。 Without any break;
不间断break;
statement, all the downstream cases(from the point where there's a match) will get executed. 语句,所有下游案例(从匹配点开始)将被执行。 So your code block should be like this: 因此,您的代码块应如下所示:
if(isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch($action){
case 'namecheck':
...
break;
case 'adduser':
...
break;
case 'test':
...
break;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.