在 Cloud9 上使用 Php 进行数据库字段检索

Database field retrieval using Php on Cloud9

在在线 IDE 云中显示我的数据库中的信息时遇到了一些问题。我已经按照一些关于连接到数据库和检索信息的指南进行操作,但它似乎没有进入 while 循环这实际上是将信息回显给浏览器。

PHP

$host = getenv("REMOTE_ADDR");
$username = "tannerhallman1";
$password = "";
$database = "Charlotte";
$dbport = 3306;

// Create connection
$db = mysqli_connect($host, $username, $password, $database, $dbport);

// Check connection
if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
} 
echo "Connected successfully my dude(".$db->host_info.")";
?>
<?php
    $sql = "SELECT * FROM lenders";
    $records = mysql_query($sql);

    echo "$records"

?>

<html>

<head>
<title>Local Lenders</title>
</head>

<body>
<table width = "600" border = "1" cellpadding="1" cellspacing = "1">
<tr>

<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Company</th>
<tr>

<?php

while ($lender = mysql_fetch_assoc($records)) {
echo "<tr>";

echo "<td>".$lender['ID']."</td";
echo "<td>".$lender['First']."</td>";
echo "<td>".$lender['Last']."</td>";
echo "<td>".$lender['Company']."</td>";

echo "</tr>";
} //end while

?>

</table>
</body>
</html>

服务器控制台

==> /home/ubuntu/lib/apache2/log/error.log <==
[Mon Mar 23 13:59:17.146489 2015] [:error] [pid 26008] [client  10.240.52.53:54254] PHP Warning:  mysqli_connect(): (HY000/2003): Can't connect      to MySQL server on '10.240.52.53' (111) in /home/ubuntu/workspace/lenders.php on line 13
[Mon Mar 23 13:59:17.146571 2015] [:error] [pid 26008] [client 10.240.52.53:54254] PHP Notice:  Trying to get property of non-object in /home/ubuntu/workspace/lenders.php on line 16
[Mon Mar 23 13:59:17.146580 2015] [:error] [pid 26008] [client 10.240.52.53:54254] PHP Notice:  Trying to get property of non-object in /home/ubuntu/workspace/lenders.php on line 19
[Mon Mar 23 13:59:17.146680 2015] [:error] [pid 26008] [client 10.240.52.53:54254] PHP Warning:  mysql_query(): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2) in /home/ubuntu/workspace/lenders.php on line 24
[Mon Mar 23 13:59:17.146693 2015] [:error] [pid 26008] [client 10.240.52.53:54254] PHP Warning:  mysql_query(): A link to the server could not be established in /home/ubuntu/workspace/lenders.php on line 24
[Mon Mar 23 13:59:17.146708 2015] [:error] [pid 26008] [client 10.240.52.53:54254] PHP Warning:  mysql_fetch_assoc() expects parameter 1 to be     resource, boolean given in /home/ubuntu/workspace/lenders.php on line 48

C9终端输出

mysql> select * FROM lenders;
+----+--------+---------+--------------+-----------+-------------------------------------------------------+
| ID | First  | Last    | Company      | Phone     | Bio                                                   |
+----+--------+---------+--------------+-----------+-------------------------------------------------------+
|  1 | Tanner | Hallman | UNCW         | 123456789 | I am a cool guy that would be good for your finances. |
|  2 | Wes    | Hallman | Mortgage Pro | 987654321 | I'm a good, cool person.                              |
+----+--------+---------+--------------+-----------+-------------------------------------------------------+
2 rows in set (0.00 sec)

浏览器输出 我从 table 开始,headers 但它没有检索数据。 我已经确认c9确实可以使用c9终端连接到数据库,所以我迷路了。

连接成功我的兄弟()

编号 |名字 |姓 |公司

根据:http://php.net/manual/en/mysqli.quickstart.statements.php

$records = mysql_query($sql); 应该替换为 $records = $db->query($sql); 甚至 $records = mysqli_query($db, $sql); 因为您没有指定哪个数据库应该用于 运行 您的查询

那么while ($lender = mysql_fetch_assoc($records)) {应该换成 while ($lender = $records->fetch_assoc()) {while ($lender = mysqli_fetch_assoc($records)) {

希望这对您有所帮助。