问题
您好,我正在创建一个搜索功能,允许用户搜索特定的电子号码,看看它是否来自动物。首先,我想将所有结果转储到网页上,看看它是否100%正确。我已成功使用PHP在我的网站上连接数据库,但pg_query没有显示任何结果,是否需要更多代码?
守则
<?php
$conn = pg_connect("***** port=*****
dbname=***** user=***** password=*****");
$res = pg_query($conn, "SELECT * FROM enumbers");
$connection = pg_connect ("host=***** port=*****
dbname=***** user=***** password=*****");
if($connection) {
echo 'connected';
} else {
echo 'there has been an error connecting';
}
?>
答案 0 :(得分:0)
感谢@LaurenzAlbe指导我answer,我想我会在这里展示:
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=***** port=*****
dbname=***** user=***** password=*****")
or die('Could not connect: ' . pg_last_error());
// Performing SQL query
$query = 'SELECT * FROM enumbers';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
// Printing results in HTML
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
?>
正如您在上面的代码中所看到的,我完全忘记了在HTML中实际打印结果,这就是没有出现的原因。