PHP新手,代码复杂
这应该很简单,但似乎对我不起作用。我用好密码和坏密码对它进行了测试。不管怎样,它都不会进入else语句。我不确定我错过了什么 我的代码:PHP新手,代码复杂,php,mysql,Php,Mysql,这应该很简单,但似乎对我不起作用。我用好密码和坏密码对它进行了测试。不管怎样,它都不会进入else语句。我不确定我错过了什么 我的代码: $mysqli = mysqli_connect("localhost", "joeuser", "somepass", "testDB"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit();
$mysqli = mysqli_connect("localhost", "joeuser", "somepass", "testDB");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
} else {
$sql = "SELECT * FROM login_info";
$res = mysqli_query($mysqli, $sql);
if ($res) {
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$id = $newArray['first_name'];
$testField = $newArray['last_name'];
echo "The ID is ".$id." and the text is ".$testField."<br/>";
}
} else {
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
}
mysqli_free_result($res);
mysqli_close($mysqli);
}
$mysqli=mysqli_connect(“localhost”、“joeuser”、“somepass”、“testDB”);
if(mysqli\u connect\u errno()){
printf(“连接失败:%s\n”,mysqli_Connect_error());
退出();
}否则{
$sql=“从登录信息中选择*”;
$res=mysqli\u查询($mysqli,$sql);
如果($res){
而($newArray=mysqli\u fetch\u array($res,mysqli\u ASSOC)){
$id=$newArray['first_name'];
$testField=$newArray['last_name'];
echo“ID是“$ID”,文本是“$testField”。
”;
}
}否则{
printf(“无法检索记录:%s\n”,mysqli_错误($mysqli));
}
mysqli_免费_结果($res);
mysqli_close($mysqli);
}
如果我给它发送一个确实存在的用户和密码,它会执行If语句,但是如果我给它发送一个不在db中的测试,它仍然不会执行else语句?为什么?
为了成功地选择、显示、描述或解释查询mysqli\u query()
将返回mysqli\u结果
对象
只有出现严重错误时,查询才会失败。仅仅因为SELECT
查询与任何行都不匹配,并不意味着查询不成功。您仍然会得到一个MySQLi\u结果
对象。您需要检查结果集是否包含任何行
顺便说一句,保存一些嵌套:
if (!$something) {
exit;
}
// continue as usual
这里不需要else
,因为您正在退出ing。使事情更简单
为了成功地选择、显示、描述或解释查询mysqli\u query()
将返回mysqli\u结果
对象
只有出现严重错误时,查询才会失败。仅仅因为SELECT
查询与任何行都不匹配,并不意味着查询不成功。您仍然会得到一个MySQLi\u结果
对象。您需要检查结果集是否包含任何行
顺便说一句,保存一些嵌套:
if (!$something) {
exit;
}
// continue as usual
这里不需要else
,因为您正在退出ing。使事情更简单。像这样:
if ($res) {
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$id = $newArray['first_name'];
$testField = $newArray['last_name'];
echo "The ID is ".$id." and the text is ".$testField."<br/>";
}
}
if (!isset($id)) {
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
}
if($res){
而($newArray=mysqli\u fetch\u array($res,mysqli\u ASSOC)){
$id=$newArray['first_name'];
$testField=$newArray['last_name'];
echo“ID是“$ID”,文本是“$testField”。
”;
}
}
如果(!isset($id)){
printf(“无法检索记录:%s\n”,mysqli_错误($mysqli));
}
像这样:
if ($res) {
while ($newArray = mysqli_fetch_array($res, MYSQLI_ASSOC)) {
$id = $newArray['first_name'];
$testField = $newArray['last_name'];
echo "The ID is ".$id." and the text is ".$testField."<br/>";
}
}
if (!isset($id)) {
printf("Could not retrieve records: %s\n", mysqli_error($mysqli));
}
if($res){
而($newArray=mysqli\u fetch\u array($res,mysqli\u ASSOC)){
$id=$newArray['first_name'];
$testField=$newArray['last_name'];
echo“ID是“$ID”,文本是“$testField”。
”;
}
}
如果(!isset($id)){
printf(“无法检索记录:%s\n”,mysqli_错误($mysqli));
}
哪个if语句?第一个?哪个if语句?第一个?虽然我更喜欢返回而不是退出(希望最终的代码将使用函数),尽管我更喜欢返回而不是退出(希望最终的代码将使用函数),但是太棒了!虽然我尝试了“if(empty($id)),但没有成功,但是“if(!isset($id))”成功了,非常感谢!令人惊叹的!虽然我尝试了“if(empty($id)),但没有成功,但是“if(!isset($id))”成功了,非常感谢!