问题
Been trying for ages but just can't get my php script to connect to a mysqli database on my GoDaddy server. I've checked the username, password, database name countless times. Here's the script. Grateful for any advice.
$con = mysqli_connect("localhost","username","password","Villa1234");
$sql = "SELECT * FROM Prospects WHERE email = '$loginEmail'";
if ($result = mysqli_query($con,$sql)) {
$rows = mysqli_num_rows($result);
if ($rows == 1) {
$firstRow = mysqli_fetch_assoc($result);
$status = $firstRow['status'];
$access = $firstRow['access'];
$pid = $firstRow['prospect_ID'];
if ($status == "Pending") {
$loginResult = "pending";
} else if ($access == "locked") {
$loginResult = "locked";
} else {
$storedPassword = $firstRow['password'];
if ($storedPassword === $password) {
$loginResult = "success";
$firstName = $firstRow['firstname'];
$_SESSION['loginEmail'] = $loginEmail;
} else {
$loginResult = "incorrect password";
};
};
} else if ($rows == 0) {
$loginResult = "email not registered";
};
};
mysqli_free_result($result);
mysqli_close($con);
回答1:
You could try this: (Even if it doesn't work, it will atleast display the error you are facing)
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
error_reporting(E_ALL);
$con = new mysqli("localhost","username","password","Villa1234");
$sql = "SELECT * FROM Prospects WHERE email = ?";
$stmt = $con->prepare($sql);
$stmt->bid_param("s", $loginEmail);
$stmt->execute();
$result = $stmt->bind_result();
if($result->num_rows==1){
while($firstRow=$result->fetch_assoc()){
$status = $firstRow['status'];
$access = $firstRow['access'];
$pid = $firstRow['prospect_ID'];
if ($status == "Pending") {
$loginResult = "pending";
}
else if ($access == "locked") {
$loginResult = "locked";
}
else {
$storedPassword = $firstRow['password'];
if ($storedPassword === $password) {
$loginResult = "success";
$firstName = $firstRow['firstname'];
$_SESSION['loginEmail'] = $loginEmail;
} else {
$loginResult = "incorrect password";
}
}
}
}
else if ($rows == 0) {
$loginResult = "email not registered";
}
?>
来源:https://stackoverflow.com/questions/40689520/cant-connect-to-godaddy-mysqli-database-via-php-script