问题
您好,我正在创建一个搜索功能,允许用户搜索特定的电子号码,看看它是否来自动物,基本上看它是否是素食主义者。我已经成功地在我的网站上使用PHP连接到数据库。
守则
在页面顶部:
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=***** port=*****
dbname=**** user=**** password=*****")
or die('Could not connect: ' . pg_last_error());
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
// Performing SQL query
$query = 'SELECT vegan FROM enumbers WHERE code = searchq';
}
?>
搜索栏:
<div id="tablebox">
<!-- Search bar -->
<p>Is It Vegan?</p>
<form name="form1" method="post" action="searchEnumbers.php">
<input name="search" type="text" size="30" maxlength="5" />
<input name="submit" type="submit" value="Search" />
</form>
</div>
我现在如何展示素食主义者&#39;搜索结果?我不确定如何打印结果。
更新
enumbers表中的列名是:&#39; code&#39;,&#39; name&#39;,&#39; type&#39;和&#39; vegan&#39;。< / p>
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=db.dcs.aber.ac.uk port=5432
dbname=cs399030_16_17 user=sec17 password=Liverpool2112")
or die('Could not connect: ' . pg_last_error());
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
// Performing SQL query
$pg_query = 'SELECT vegan FROM enumbers WHERE code = '.$searchq;
$result = pg_query($query);
foreach($result as $r){ //If you have multiple records or $result
echo "<p> Your ".$r->params." or ".$r['params']." here </p>"; //for instance
}
}
?>
答案 0 :(得分:0)
你应该有类似的东西:
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=**** port=****
dbname=**** user=**** password=****")
or die('Could not connect: ' . pg_last_error());
//collect
if(isset($_POST['search'])) {
$searchq = $_POST['search'];
// Performing SQL query
$query = 'SELECT vegan FROM enumbers WHERE code = '.$searchq;
// make the query with: pg_query
// $result = pg_query($query);
foreach($result as $r){ //If you have multiple records or $result
echo "<p> Your ".$r->params." or ".$r['params']." here </p>"; //for instance
}
//you could use: var_dump(pg_fetch_all($result));
}
?>
<!-- If you want to show always the form. If not, put inside the else -->
<div id="tablebox">
<!-- Search bar -->
<p>Is It Vegan?</p>
<form name="form1" method="post" action="searchEnumbers.php">
<input name="search" type="text" size="30" maxlength="5" />
<input name="submit" type="submit" value="Search" />
</form>
</div>
请注意,这是not the best
方法(您可以使用AJAX,以及不同页面以便现在刷新页面)或其他方式。但这可以按照你想要的方式工作。
查看PHP的官方文档:http://php.net/manual/kr/function.pg-connect.php
希望它有所帮助!