While循环不能用于访问SQL记录


while loop not working for acessing sql records

我将一个字符串传递给这个文件-txtname-(字符串由空格分隔),并将每个单词分开,然后将其传递给函数subtoken(),该函数应该从数据库中获取相应的单词,具有两个属性-rootwords和example,但subtoken()函数只执行一次并退出。

$counter=0; 
$string = $_REQUEST['txtname'];
$token = strtok($string, ' ');
while ($token != false)
{echo $counter;
  subtoken($token,$counter);
 $counter++;
 $token = strtok(' ');  
}

function subtoken($fname,$counter)
{
$row ="";
$result="";
$result = mysql_query('SELECT * FROM hindi WHERE rootwords LIKE  ''%:'.$fname.':%''' );
while($row = mysql_fetch_array($result))
{
$temp=$row['rootwords'];
$token2 = strtok($temp, ':');
echo $token2 ;
while($token2!=false)
{
    echo $token2."<br/>" ;
    $token2=strtok(':');
}
}
}
mysql_close($con);

strtok的双重使用将阻止"main"循环正确处理来自原始$string的所有令牌。您不能同时使用多个"打开"的strtok

原来的嫌疑犯是你的查询,它只是没有选择任何东西。尝试打印SQL语句,然后直接执行该语句(例如通过phpmyadmin)

// insert this line:
echo(
    'SELECT * FROM hindi '.
        'WHERE rootwords LIKE  ''%:'.$fname.':%''');
// just before the actual query execution
$result = mysql_query(
    'SELECT * FROM hindi '.
        'WHERE rootwords LIKE  ''%:'.$fname.':%''' );

根据我的经验,在调试时尽可能早地回显数据是很容易发现错误的最佳工具之一。

参见:http://nl2.php.net/mysql_fetch_arrayMysql_fetch_array需要第二个参数才能正常工作。

将mysql_fetch_array替换为mysql_fetch_row。或者mysql_fetch_assoc,如果您想按名称引用列。

可能你有一些错误/警告?error_reporting设置为E_ALL和display_errors为"On"?

似乎strtok()只有一个指针。因此,当您在函数中结束它时,您需要重新初始化$token = strtok($string, ' '); ?但我不确定。我认为如果你使用爆炸()函数会更好。