【问题标题】:Database field retrieval using Php on Cloud9在 Cloud9 上使用 PHP 检索数据库字段
【发布时间】:2023-03-06 19:05:01
【问题描述】:

在在线 IDE 云中显示来自我的数据库的信息时遇到了一些问题 9。我遵循了一些关于连接到数据库和检索信息的指南,但它似乎没有进入实际上将信息回显到的 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 |名字 |姓氏 |公司

【问题讨论】:

  • 我什至按照cloud9 post 上的指示从终端手动检索了 IP 和 C9_USER 并手动输入它们,但仍然无济于事。
  • 我认为您的主机名 ($host) 可能是问题所在。尝试将getenv("REMOTE_ADDR") 替换为getenv('IP'),然后重试。
  • 我刚刚尝试了这个编辑,但没有奏效。当我从 c9 终端检索 IP 时,它的 0.0.0.0.
  • 没错,0.0.0.0 应该是您运行 MySQL 的地方。我不确定为什么它不起作用
  • 你的 MySQL 是如何启动的?

标签: php html cloud9-ide


【解决方案1】:

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

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

那么while ($lender = mysql_fetch_assoc($records)) {应该被替换为 while ($lender = $records-&gt;fetch_assoc()) {while ($lender = mysqli_fetch_assoc($records)) {

希望这会有所帮助。

【讨论】:

  • 我昨晚才弄明白的。我需要与my_sqli 命令保持一致,并将$db 变量添加到语句$records = mysqli_query($db, $sql); 中,这一切都成功了!感谢您的帮助。
猜你喜欢
  • 2012-01-25
  • 2017-09-20
  • 2013-06-21
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
相关资源
最近更新 更多