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


Database field retrieval using Php on Cloud9

在在线IDE云9中显示数据库中的信息时遇到了一些问题。我遵循了一些关于连接数据库和检索信息的指南,但它似乎没有进入while循环,而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)

浏览器输出我得到了一个表的开头,有标题,但它不检索数据。我已经确认c9实际上可以使用c9终端连接到数据库,所以我迷路了。

成功连接我的哑弹()

ID|名字|姓氏|公司

根据: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)) {

希望这能有所帮助。