I've used this code with a different host with no difficulties. I'm trying to use this on a new host and it's not working. I am connecting to the database for sure, and the memberid is definitely in the table.
我已经在不同的主机上使用了这段代码。我试着在新主机上使用它,但它不工作。我确实正在连接数据库,而且memberid肯定在表中。
The server is running PHP 5.4 Can anyone see a problem with this code?
服务器正在运行PHP 5.4,有人看到这段代码有问题吗?
Any help is appreciated.
任何帮助都是感激。
<?php
include("../../connection.php");
if ($_POST) {
$memberid = $_POST["memberid"];
$memberid = mysqli_real_escape_string($connection, $memberid);
$query = "SELECT * FROM members WHERE memberid = '{$memberid}'";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) >= 1) {
header("Location: www.example.com");
} else {
header("Location: www.example.com");
}
}
?>
Here's the connection code:
这里的连接代码:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$connection = mysqli_connect($servername, $username, $password);
mysqli_select_db ("database_name");
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
2 个解决方案
#1
3
Your problem is here:
你的问题是:
$connection = mysqli_connect($servername, $username, $password);
mysqli_select_db ("database_name");
With MySQLi rather than MySQL, you need to add the database name in with the connection details, so rewrite it as:
使用MySQLi而不是MySQL,您需要在连接细节中添加数据库名,因此将其重写为:
$connection = mysqli_connect($servername, $username, $password, "database_name");
And this should work correctly for you now, what was happening previously was that your connection was ok but no database was being specified, now you have the database specified on the same mysqli_connect
call so the PHP knows where to put/take the data .
这对你现在应该是正确的,之前发生的是你的连接是ok的,但是没有指定数据库,现在你有在同一个mysqli_connect调用中指定的数据库,这样PHP就知道在哪里放置/获取数据。
#2
0
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$memberid = $_POST["id"];
$memberid = mysqli_real_escape_string($connection, $memberid);
$query = "SELECT * FROM `members` WHERE memberid= '$memberid'";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
echo "Success";
} else {
echo "Error";
}
}
?>
Connection code:
连接代码:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$connection = mysqli_connect($servername, $username, $password, database_name);
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}else{
echo "Connected successfully";
}
?>
#1
3
Your problem is here:
你的问题是:
$connection = mysqli_connect($servername, $username, $password);
mysqli_select_db ("database_name");
With MySQLi rather than MySQL, you need to add the database name in with the connection details, so rewrite it as:
使用MySQLi而不是MySQL,您需要在连接细节中添加数据库名,因此将其重写为:
$connection = mysqli_connect($servername, $username, $password, "database_name");
And this should work correctly for you now, what was happening previously was that your connection was ok but no database was being specified, now you have the database specified on the same mysqli_connect
call so the PHP knows where to put/take the data .
这对你现在应该是正确的,之前发生的是你的连接是ok的,但是没有指定数据库,现在你有在同一个mysqli_connect调用中指定的数据库,这样PHP就知道在哪里放置/获取数据。
#2
0
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$memberid = $_POST["id"];
$memberid = mysqli_real_escape_string($connection, $memberid);
$query = "SELECT * FROM `members` WHERE memberid= '$memberid'";
$result = mysqli_query($connection, $query);
if (mysqli_num_rows($result) > 0) {
echo "Success";
} else {
echo "Error";
}
}
?>
Connection code:
连接代码:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$connection = mysqli_connect($servername, $username, $password, database_name);
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}else{
echo "Connected successfully";
}
?>