doxzrb8721 2016-03-23 22:34
浏览 51
已采纳

连接到mysql数据库但无法打印数据[重复]

This question already has an answer here:

I connected successfully to my database and the I tried to print my data but I can only see the "Connected succesfully" line in the browser. Here is my code:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

$db=mysqli_connect("host","root","pass","dbase");

if (!$db) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}
echo "Connected successfully";
$sql = "SELECT username FROM students";
$result = $db->query($sql);

if ($result->num_rows > 0) {
   // output data of each row
   while($row = $result->fetch_assoc()) {
   echo "username: " . $row["username"].  "<br>";
   }
} else {
  echo "0 results";
}
$db->close();
?>

So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server. The setsebool -P httpd_can_network_connect=1 in the terminal did the trick.

</div>
  • 写回答

2条回答 默认 最新

  • dongshang1934 2016-03-23 22:43
    关注

    Edit: Note to future readers. The OP edited their question with the corrected syntax after answers were posted and this answer was based on their original post:

    and added:

    "So the issue was that SELinux was blocking remote connections from PHP scripts executed by Apache web server. The setsebool -P httpd_can_network_connect=1 in the terminal did the trick."

    Firstly, you didn't select a database.

    and if (!db) needs to be changed to if (!conn) and if ($db->connect_error) to if ($conn->connect_error). The same variable needs to be used throughout your code.

    You're also using the wrong variable $db it's $conn (or whatever you want to use, but as I said above, you need to use the same variable for your connection and for the query.

    Error reporting http://php.net/manual/en/function.error-reporting.php would have told you about an undefined variable.

    Therefore:

    $conn = @mysqli_connect("host","root","pass", "your_database");
    

    Sidenote: host I take it is only representative of the host you're using. If you're on your own PC, you would use localhost and accessing that file as http://localhost/file.php instead of file///file.php if that is what you're doing and will not work. The web browser will not parse PHP directives like that and you need to install a webserver/PHP in order for it to work, including MySQL.

    Read the manual on connecting:

    and remove the @ symbols while testing. They are error suppressors.

    Check for errors with http://php.net/manual/en/mysqli.error.php

    Example from the manual:

    Sidenote: 127.0.0.1 or localhost or if hosted, use the setting they gave you to use.

    <?php
    $link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
    
    if (!$link) {
        echo "Error: Unable to connect to MySQL." . PHP_EOL;
        echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
        echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
        exit;
    }
    
    echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
    echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;
    
    mysqli_close($link);
    

    Add error reporting to the top of your file(s) which will help find errors.

    <?php 
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    // Then the rest of your code
    

    Sidenote: Displaying errors should only be done in staging, and never production.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题