0

I have a code in PHP like the next:

    //$menutype is defined in this point, and no problem with it
    $sql = "SELECT struct FROM menutype WHERE id=$menutype;"; 
    $result = mysql_query($sql);
    list($struct) = mysql_fetch_row($result);
    $menu = explode("\n", $struct); //Explode to make an array with each line
    foreach ($menu as $index => $value)
    {
        //$barid is defined in the top of the document and no issue with it
        creatabla($barid, $value);
    }

function creatabla($barid, $tipo)
{
    $tipo = trim($tipo, ' '); //trim to delete unwanted spaces
    $sql = "SELECT name FROM products WHERE tipo LIKE '%$tipo%' AND restaurante='$barid';"; 
    $result = mysql_query($sql);
    while(list($name) = mysql_fetch_row($result)) 
    {
        echo "$name";
    }
}   

Similar 'struct' row struct:

Line1
Line2
Line3
Line4AndLastLine

No car return after Last Line.

Well, the code usually works fine, but If I modify the 'Struct' row, some lines won't be read fine, so I usually need to edit the struct row in the advaced editor of the phpmyadmin.

What can I do to solve this issue? Can I try other kind of filter in the sql statement? What can I do to improve the trim or the explode function to solve this issue?

Thanks you to all in advance.

6
  • 2
    First of all, let me start with the mandatory warning not to use mysql_* functions. They are deprecated. You should be using mysqli_* or PDO functions instead. Your code above is ripe for SQL injection. Commented Mar 15, 2015 at 17:50
  • When you modify the Struct row you say "some lines won't be read fine" - what does that mean? Do 2 lines come out looking as if they are on the same line? Or are some lines ignored altogether? It might be helpful to do a print_r($menu); right after it is created. Commented Mar 15, 2015 at 17:55
  • It's appearing not diferences between a good line and a bad line :S Commented Mar 15, 2015 at 18:27
  • I'm sorry, I don't understand. Could you show an example of what happens (actual program output) when you have a bad line in the Struct row? Commented Mar 15, 2015 at 18:41
  • Hi, no differences between a good line and a bad line: I.e: I have a line with the next content: "Test1", the output says: "Test1", but the execution won't return anything with the sql query where tipo is Like %Test1%. If I edit Test1 line with the advanced editor of phpmyadmin, the output of each print_r are the same, but now, the code run successfully. I think it could be the end of the line, that the simply editor can add some weird end of line. :S Commented Mar 16, 2015 at 16:17

1 Answer 1

1

I think I'm starting to understand the issue and I'm guessing you're right - I'm guessing there is a \r character on the end.

Right now you are calling this:

trim($tipo, ' ');

That deletes ONLY spaces. If you change it to simply:

trim($tipo);

Then it will delete a lot more types of whitespace, including (but not limited to) the \r character.

See HERE for more information on the trim() function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.