在Cloud9上使用Php进行数据库字段检索
在联机IDE cloud 9中显示数据库中的信息时遇到了一些问题。我遵循了一些关于连接数据库和检索信息的指南,但它似乎没有进入while循环,而while循环实际上是将信息回送到浏览器 PHP在Cloud9上使用Php进行数据库字段检索,php,html,cloud9-ide,Php,Html,Cloud9 Ide,在联机IDE cloud 9中显示数据库中的信息时遇到了一些问题。我遵循了一些关于连接数据库和检索信息的指南,但它似乎没有进入while循环,而while循环实际上是将信息回送到浏览器 PHP $host = getenv("REMOTE_ADDR"); $username = "tannerhallman1"; $password = ""; $database = "Charlotte"; $dbport = 3306; // Create connection $db = mysqli_
$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>
从浏览器输出
$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>
我得到一个表的开头,带有标题,但它不检索数据。
我已经确认c9实际上可以使用c9终端连接到数据库,所以我迷路了
成功连接我的哥们()
ID |名|姓|公司依据:
$records=mysql\u查询($sql)
应替换为$records=$db->query($sql)
甚至$records=mysqli\u查询($db,$sql)代码>因为您没有指定应该使用哪个数据库来运行查询
然后,while($lender=mysql\u fetch\u assoc($records)){
应替换为
while($lender=$records->fetch_assoc()){
或while($lender=mysqli_fetch_assoc($records)){
希望这能有所帮助。我甚至按照上面的说明,从终端手动检索IP和C9_用户并手动输入,但仍然无效。我认为您的主机名($host
)可能是问题所在。请尝试将getenv(“远程地址”)
替换为getenv(“IP”)
再试一次。我刚刚尝试了这个编辑,但没有成功。当我从c9终端检索到IP时,它是0.0.0.0。没错,0.0.0
应该是你的MySQL正在运行的地方。我不确定为什么它不起作用。你是如何启动MySQL的?我昨晚才知道。我需要与保持一致>my_sqli
命令,并将$db
变量添加到语句$records=mysqli_query($db,$sql);
中,所有操作都成功了!感谢您的帮助。
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)