douer9399 2015-03-23 14:11
浏览 38
已采纳

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

have been having some trouble displaying information from my database in the online IDE cloud 9. I have followed some guides about connecting to databases and retrieving information, but it doesn't seem to enter the while loop that actually echo's the information to the browser.

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>

Server console

==> /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 Terminal output

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)

Output from Browser I get the start of a table, with the headers but it doesn't retrieve the data. I have confirmed that c9 can actually connect to the database using the c9 terminal, so I am lost.

Connected successfully my dude()

ID | First Name | Last Name | Company

  • 写回答

1条回答 默认 最新

  • dongmo1708 2015-03-24 07:30
    关注

    According to: http://php.net/manual/en/mysqli.quickstart.statements.php

    $records = mysql_query($sql); should be replaced with $records = $db->query($sql); or even $records = mysqli_query($db, $sql); since you aren't specifying which db should be used to run your query

    Then while ($lender = mysql_fetch_assoc($records)) { should be replaced with while ($lender = $records->fetch_assoc()) { or while ($lender = mysqli_fetch_assoc($records)) {

    Hope this helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分