douyong1285 2017-04-26 14:00
浏览 85

相同的代码不同地从数据库加载数据和使用本地数据

(Yes, the title should have been more understandable but honestly I didn't know how to set it.)

What I am trying to do is creating a online dictionary of Latin. When you search a word the code (I am using some index so don't worry about selectivity) will search that name row by row inside a string (the string becomes an array using explode).

I've created a table called 'test' just to practice with this code but it works in the same way of the one of the dictionary: here the table

Before trying to load data from database I've checked if it worked locally. It works locally. Here the code:

$word_searched = 'puellam';

$string_declination = ('puella,puellae,puellae,puellam,puella,puella');
$array_declination = explode(",", $string_declination);

$check = 0;

foreach ($array_declinazion as $item) {
    if ( (strcmp($item, $word_searched)) === 0 ){
        $check++;
    }
}   

if ( $check > 0) {
    echo 'yes';
}

Then I've tryed to load the data from the database (the table 'test') using the same code. Here the code:

$query =  "SELECT * FROM test";

$result = mysqli_query($conn, $query) or die('Error running query!');

$check = 0;
$string = 'puellam';

while($row = $result->fetch_assoc()) { 
    $array_declinazione = explode(",", $row['declinazione']);
    foreach ($array_declinazione as $item) {
        if ( (strcmp($item, $string)) === 0 ){
            $check++;
        }
    }
}


if ( $check > 0) {
    echo 'yes';
}

The first code works perfectly but the second one doesn't even though it's the same way. I just don't understand why.

  • 写回答

2条回答 默认 最新

  • dongwu8064 2017-04-26 14:21
    关注

    I think what you need for the second one is this:

    $string = 'puellam';
    // If there is a word that matches $string it will return the row
    $query =  "SELECT * FROM test WHERE declinazione LIKE '%$string%'"; // demonstration purposes only. THIS IS VERY UNSAFE!!!! DO NOT USE IN PRODUCTION!!!!
    
    $result = mysqli_query($conn, $query;
    
    if ( $result ) {
        $check = mysqli_num_rows($result);
    
        if ( $check > 0) {
            echo 'yes';
        }
    } else {
        die('Error running query! ' . mysqli_error($conn); // Throw errors if any - It's best to log it instead of "echoing" it
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器