我正在尝试创建一个基本的“搜索所有匹配的行的表列,并向我显示行”类型搜索。我最终得到的是一个永远重复的查询,结果网页永远不会停止加载。循环的经典示例无法结束。我不知道为什么。我试图让每个小步骤保持自己的功能就像一个好孩子,并重用功能。
一个人应该能够搜索问题或 john 以返回一行 plus 或 minus 看起来像两行,而 test 将返回三行。
请帮忙!
function databaseconnection($sql){
$usernm="XXXfooXXX";
$passwd="XXXfooXXX";
$host="XXXfooXXX";
$database="contact_info";
mysql_connect($host,$usernm,$passwd);
mysql_select_db($database);
$result = mysql_query ($sql) or die (mysql_error ());
return $result;
}
function searchtable(){
echo 'searchtable() <br />';
if ($_POST['search'] != "" ){
$search = preg_replace('/[^ \wa-zA-Z0-9_.@()\-+~,?]+/', '', $_POST['search']);
}
if ($search){
$sql = "SELECT * FROM names_numbers WHERE name LIKE \"%{$search}%\"".
" OR phone_address LIKE \"%{$search}%\"".
" OR notes LIKE \"%{$search}%\"";
echo 'attempting sql action<br />';
echo "$sql <br />";
while ($row = mysql_fetch_assoc(databaseconnection($sql))){
print_r($row);
}
}
}
function draw_search_form(){
echo '<form action="./numbers.php" method="post">'."\n";
echo 'notes:<br>'."\n";
echo '<input type="text" name="search"><br>'."\n";
echo '<input type="submit" value="Submit">'."\n";
echo '</form>'."\n";
}
答案 0 :(得分:1)
我相信你的while
循环,你每次都在执行查询,这会重置你的结果迭代器。当你切换到mysqli或PDO时,你可以重构一次运行查询,然后迭代结果。
$results = databaseconnection($sql);
while($row = mysqli_fetch_assoc($results) {
...
}
答案 1 :(得分:0)
使用当前的while循环,您将打开一个新的数据库连接,运行查询并反复获取第一行。
通过将查询移出数据库,然后迭代结果
,可以轻松解决此问题以下是带有更改的代码
function databaseconnection($sql){
$usernm="XXXfooXXX";
$passwd="XXXfooXXX";
$host="XXXfooXXX";
$database="contact_info";
mysql_connect($host,$usernm,$passwd);
mysql_select_db($database);
$result = mysql_query ($sql) or die (mysql_error ());
return $result;
}
function searchtable(){
echo 'searchtable() <br />';
if ($_POST['search'] != "" ){
$search = preg_replace('/[^ \wa-zA-Z0-9_.@()\-+~,?]+/', '', $_POST['search']);
}
if ($search){
$sql = "SELECT * FROM names_numbers WHERE name LIKE \"%{$search}%\"".
" OR phone_address LIKE \"%{$search}%\"".
" OR notes LIKE \"%{$search}%\"";
echo 'attempting sql action<br />';
echo "$sql <br />";
// Run the query once
$result = databaseconnection($sql);
// Now iterate over the results
while ($row = mysql_fetch_assoc($result)){
print_r($row);
}
}
}
function draw_search_form(){
echo '<form action="./numbers.php" method="post">'."\n";
echo 'notes:<br>'."\n";
echo '<input type="text" name="search"><br>'."\n";
echo '<input type="submit" value="Submit">'."\n";
echo '</form>'."\n";
}
答案 2 :(得分:0)
发布作为答案,以便更容易格式化已更正的功能:
function searchtable(){
echo 'searchtable() <br />';
if ($_POST['search'] != "" ){
$search = preg_replace('/[^ \wa-zA-Z0-9_.@()\-+~,?]+/', '', $_POST['search']);
}
if ($search){
$sql = "SELECT * FROM names_numbers WHERE name LIKE \"%{$search}%\"".
" OR phone_address LIKE \"%{$search}%\"".
" OR notes LIKE \"%{$search}%\"";
echo 'attempting sql action<br />';
echo "$sql <br />";
$results = databaseconnection($sql);
while($row = mysql_fetch_assoc($results)) {
print_variable($row);
}
//while ($row = mysql_fetch_assoc(databaseconnection($sql))){
// print_variable($row);
//}
}
}
答案 3 :(得分:0)
伙计,你正在提供一个阵列,而这将始终保持真实。请改用foreach。
$row = mysql_fetch_assoc(databaseconnection($sql))
foreach ($row as $r){
print_r ($r)//extract associative elements
}