数据不是来自mysql

时间:2015-04-17 04:22:36

标签: php mysql connection

显示已创建连接。但我不知道查询中的问题是什么。在那个查询之后,我试图回应一些东西,它在浏览器中可见。

<html>
    <head>

    </head>
<body>

<?php
$servername = "localhost:8888";
$username = "root";
$password = "";
$dbname = "employees";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}else {
    echo "Connected successfully";
}

$res = mysqli_query($conn,"select * from dept_manager");
echo"dept_manager";
while($row=mysqli_fetch_assoc($res))
{
?>
<table>
    <tr>
        <td><?php echo $row['emp_no'] ?></td>
        <td><?php echo $row['dept_no']?></td>
        <td><?php echo $row['from_date'] ?></td>
        <td><?php echo $row['to_date'] ?></td>  
    </tr>
</table>
<?php
}
?>
</body>
</html>

3 个答案:

答案 0 :(得分:1)

$sql = "select * from dept_manager";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()){
      // your code here

}

试试这个..

答案 1 :(得分:1)

在while循环的位置试试这个:

<?php
    $res = mysqli_query($conn,"select * from dept_manager");
    echo"dept_manager";
    echo "<table>";
    while($row=mysqli_fetch_assoc($res))
    {
     echo "<tr><td>" . $row['emp_no'] . "</td><td>" . $row['dept_no'] . "</td><td>" . $row['from_date'] . "</td><td>" . $row['to_date'] . "</td></tr>";
    }
echo "</table>";
?>

答案 2 :(得分:0)

  

您提到了servername:localhost:8888只需检查一次删除   8888并尝试。

我已尝试在我的本地系统中使用您的代码,相同的代码工作正常。

我刚输入了我的数据库名称和表名,它工作正常。

所以你只需按照我的例子进行一次测试。

<强>代码: -

<html>
<head>
</head>
<body>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydashboard";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
else{
echo "Connected successfully";
}

$res = mysqli_query($conn,"select * from content_values");

while($row=mysqli_fetch_assoc($res))
{
?>
<table>
    <tr>
        <td><?php echo $row['name'] ?></td>
    </tr>
</table>
<?php
}
?>
</body>
</html>

<强>输出: -

Connected successfully

Name:
Test QA AndroidSD
image sampl
GMAIL
test PDF
Nat Geo Video