PHP Function to search values in database and replace in a loop

2499 views php

I am building a web scraper in PHP and I am not so experimented with all this stuff. What I am trying to achieve is as following:

  • Split an array of values into strings using foreach
  • Search any value in a predefined MYSQL table. If value is identical with one of the defined ones, it should be replaced. Otherwise it should remain the same
  • Put the new values back into an array

Below is my snippet. Basic structure of database is "ID, Marime, Inlocuire". "Marime" is the column to search on, and "Inlocuire" is the column to replace value with.

foreach ($marimi as $marime) {
        $sizes[]=trim(strtok($marime->innertext, '-'));
        $newArray = array_filter($sizes, 'myFilter');

        foreach ($newArray as $marimeFixa) {
            $marimeDefinita = $conn->query("SELECT * FROM oc_1_tabelmarimi WHERE Marime = '$marimeFixa'");
            if($marimeDefinita->num_rows == 0) {
                $marimeFixa = $marimeFixa;
            } else {
                $marimeFixa = $marimeDefinita['Inlocuire'];
            $arrayMarimi[] = $marimeFixa;

However this doesn't seem to work. Any help is greatly appreciated. Thanks!

answered question

What seems to be not working ? Please add details about the issues you are facing.

Array ( [0] => Array ( ) [1] => Select Size ) Fatal error: Uncaught Error: Cannot use object of type mysqli_result as array in /home/ob2011/public_html/stockscan.php:59

Do var_dump of $marimeDefinita and check its structure. It seems to be an object, while you are using it as an array.

1 Answer



$marimeDefinita = $marimeDefinita->fetch_assoc();

before if($marimeDefinita->num_rows == 0) {


$marimeFixa = $marimeDefinita->Inlocuire;

posted this

Have an answer?


Please login first before posting an answer.