无法从数据库中的数据比较字符串数组 [英] unable to compare data from database to array of strings

查看:113
本文介绍了无法从数据库中的数据比较字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,我需要插入的话,就像我希望,然后将这些话将通过数据库进行检查,如果是在数据库present,它应该返回多少字,其中present在数据库。
请告诉我什么是错这个code,它没有返回类似的条目数数据库

 < HTML和GT;
< HEAD>< SCRIPT LANGUAGE =JavaScript的类型=文/ JavaScript的>
// pre-定义的文本:
VAR newtext =这是新文本;
//落菜单:
VAR newtext = myform.mymenu.options [myform.mymenu.selectedIndex] .value的;
//提示:
VAR newtext =提示(输入新的文字在这里:','');
功能addtext(){
    VAR newtext = document.myform.inputtext.value;
    document.myform.outputtext.value + = newtext +'和; NBSP;';
}< / SCRIPT>
< /头>
<身体GT;
<表格名称=MyForm的行动=方法=后>
<表格边框=0CELLSPACING =0的cellpadding =5>< TR>
< TD>< textarea的名字=inputText的>< / textarea的>< / TD>
<输入类型=电台NAME =安置值=追加检查>添加到现有文字< BR>
< TD>< P><输入类型=电台NAME =安置值=替换>替换现有文字< BR>
<输入类型=按钮值=添加新文本的onClick =addtext();>< / P>
< / TD>
< TD>< textarea的名字=的outputText>< / textarea的>< / TD>
< / TR>< /表>
<输入类型=提交/>
< /表及GT;
< PHP
$字符串= $ _ POST ['的outputText'];
$阵列=阵列();
$阵列=爆炸(;,$字符串);
@ $ DB =新的mysqli('localhost'的,'根','','文字');
如果(mysqli_connect_errno())
{
    回声错误:无法连接到数据库;
}
其他
呼应已连接;
$ DB-GT&; select_db('字');
$计数= 0;
的foreach($数组作为$ S)
{
    $查询=从集合中选择*,其中单词LIKE'%$的%。'
    $结果= $ DB-GT&;查询($查询);
    如果($结果)
    $计数+ = $ DB-GT&; NUM_ROWS;
}
回声$计数;
$ DB-GT&;关闭();
?>
< /身体GT;
< / HTML>


解决方案

  $计数= 0;
的foreach($数组作为$ S)
{
    $查询=从收集num_matched SELECT COUNT(*),其中字LIKE'%$的%。'
    $结果= $ DB-GT&;查询($查询)或死亡($ DB-GT&;错误);
    $行= $ result-> FETCH_ASSOC();
    $计数+ = $行['num_matched'];
}
回声$计数;

您也应该切换到参数化查询,而不是直接使用的输入。

  $语句= $ DB-GT&; prepare(SELECT COUNT(*)
                      从集合
                      WHERE字LIKE CONCAT)('%','%'?);
$ stmt-> bind_param(S,$ S);
$计数= 0;
的foreach($数组作为$ S){
    $结果= $ stmt->执行();
    $ stmt-> bind_result($ num_matched);
    $ stmt->取();
    $计数+ = $ num_matched;
}
回声$计数;

i have a program where i need to insert words as much as i wish and then those words will be checked through database, if it is present in database , it should return how many words where present in database. please tell me what is wrong with this code, it is not returning the number of similar entries to database

<html>
<head>

<script language="javascript" type="text/javascript">
// Pre-defined text:
var newtext = "This is the new text";
// Drop-Menu:
var newtext = myform.mymenu.options[myform.mymenu.selectedIndex].value;
// Prompt:
var newtext = prompt('Enter New Text Here:', '');
function addtext() {
    var newtext = document.myform.inputtext.value;
    document.myform.outputtext.value += newtext+'&nbsp;';
}

</script>
</head>
<body>
<form name="myform" action="" method="post">
<table border="0" cellspacing="0" cellpadding="5"><tr>
<td><textarea name="inputtext"></textarea></td>
<input type="radio" name="placement" value="append" checked> Add to Existing Text<br>
<td><p><input type="radio" name="placement" value="replace"> Replace Existing Text<br>
<input type="button" value="Add New Text" onClick="addtext();"></p>
</td>
<td><textarea name="outputtext"></textarea></td>
</tr></table>
<input type="submit"/>
</form>
<?php
$string=$_POST['outputtext'];
$array=array();
$array=explode(';',$string);
@ $db=new mysqli('localhost','root','','words');
if(mysqli_connect_errno())
{
    echo 'Error:Could not connect to the database';
}
else
echo 'connected';
$db->select_db('words');
$count = 0;
foreach($array as $s)
{    
    $query="select * from collection where word LIKE '%".$s."%'";
    $result=$db->query($query);
    if($result)
    $count += $db->num_rows;    
}
echo $count;
$db->close();
?>
</body>
</html>

解决方案

$count = 0;
foreach($array as $s)
{    
    $query="select count(*) as num_matched from collection where word LIKE '%".$s."%'";
    $result=$db->query($query) or die($db->error);
    $row = $result->fetch_assoc();
    $count += $row['num_matched'];
}
echo $count;

You should also switch to parametrized queries instead of using the input directly.

$stmt = $db->prepare("select count(*)
                      FROM collection
                      WHERE word LIKE CONCAT('%', ?, '%')");
$stmt->bind_param("s", $s);
$count = 0;
foreach ($array as $s) {
    $result = $stmt->execute();
    $stmt->bind_result($num_matched);
    $stmt->fetch();
    $count += $num_matched;
}
echo $count;

这篇关于无法从数据库中的数据比较字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆