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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应