dsfds656545 2017-07-01 15:31
浏览 50

检查MySQLi数据库和PHP中是否存在用户名

I have been working on a website which has a xampp server and a database called users with a table called AccountDetails. About a year ago I got it to work perfectly, but the server I was using then required MySQL not MySQLi. Now I have to use MySQLi and can't even get the simplest of sql's SELECT function to work, any ideas would be much appreciated.

<?php
$link = mysqli_connect("localhost:3306", "root","", "users");

if(mysqli_connect_errno($link)){
    echo "MySql Error: " . mysqli_connect_error();
} else {
    echo"Connection Successful <br></br>";
}

echo("Check if still working <br></br>");

// -----------------------------//
echo("Its running <br></br>");
$result = $link->query("SELECT ID, UserName FROM AccountDetails");
return $result->result();
var_dump($result);

mysqli_close($link);
?>

The Query itself works when I plug it into the phpmyadmin SQL section and it returns the values that I expect it too.

I've spent days looking online for different answers but none of them work, and the var_dump only gives me "bool(false)" which I don't think I should be getting.

  • 写回答

1条回答 默认 最新

  • duangouhui0446 2017-07-01 15:41
    关注

    You can try this code

    <?php
    $link = mysqli_connect("localhost:3306", "root","", "users");
    
    if(mysqli_connect_errno($link)){
        echo "MySql Error: " . mysqli_connect_error();
    } else {
        echo"Connection Successful <br></br>";
    }
    
    echo("Check if still working <br></br>");
    
    // -----------------------------//
    echo("Its running <br></br>");
    $sql_select = "SELECT * FROM AccountDetails";
    $result = $link->query($sql_select);
    
    if ($result->num_rows > 0) {
    
    while($row = $result->fetch_assoc()) {
        echo "UserName: " . $row['UserName']. "<br>";
    }
    
    } else {
        echo "No Records";
    }
    $link->close();
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据