skip to Main Content

I have a weird problem. I read all related topic but there was no any solution for me. There are bunch of php codes on my project and all work perfectly but that one :

<?php
    require("connection.php");
    $query = "SELECT * FROM mrf WHERE completed=0 AND archive IS NULL ORDER BY projectname";
    $get = $db->prepare($query);
    $get->execute();
    if ($get->rowCount()!=0){
    foreach ($get as $res) {
      '<li class="list-group-item"><span class="font-weight-bold">Product(s) : </span>';
      $pieces = explode(",", $res['products']);
      $pieces_count = count($pieces);
      for ($i=0; $i <= $pieces_count -1 ; $i++) { 
        $query2 = "SELECT * FROM suppliers WHERE id = :id";
        $get2 = $db->prepare($query2);
        $get2->bindValue(":id",$pieces[$i]);
        $get2->execute();
        if ($get2->rowCount()!=0){
          foreach ($get2 as $res2) {
            echo $res2['name'].'<b>/</b>';
          }
       }
    }       
    echo '</li>';

It works localhost perfectly but on server the result is empty.

  • I have checked the error(s)

     error_reporting(E_ALL);
     ini_set('display_errors', 1);
    

But there is no any error. Also If there had been an error, I would have seen it on localhost, because error_reporting works on localhost as well as on live server.

  • I checked typos, no any typo
  • Checked cpanel everything related to PHP. All is OK. By the way php ver is 5.6

  • Any idea??

———————————–RESULT—————————-

-I have checked all codes again and didn’t find any error.
-I contacted my hosting company. They said my server has reached RLimitMEM (don’t know what it is) So they increased my RlimitMEM value.
– I amnot satisfied with hosting company’s answer but the code is working now.
– I appreciate for all answers and advices.

2

Answers


  1. Replace your code with the following and check

    <?php
    require("connection.php");
    $query = "SELECT * FROM mrf WHERE completed=0 AND archive IS NULL ORDER BY projectname";
    $get = $db->prepare($query);
    $get->execute();
    if ($get->rowCount()!=0){
    foreach ($get as $res) { ?>
      '<li class="list-group-item"><span class="font-weight-bold">Product(s) : 
    </span>';
      <?php 
      $pieces = explode(",", $res['products']);
      $pieces_count = count($pieces);
      for ($i=0; $i <= $pieces_count -1 ; $i++) { 
        $query2 = "SELECT * FROM suppliers WHERE id = :id";
        $get2 = $db->prepare($query2);
        $get2->bindValue(":id",$pieces[$i]);
        $get2->execute();
        if ($get2->rowCount()!=0){
          foreach ($get2 as $res2) {
            echo $res2['name'].'<b>/</b>';
          }
        }
      }
      echo '</li>';
     }
    }
    

    You have not closed the PHP, closing curly braces were not there…

    Login or Signup to reply.
  2. Try echoing it out:

    foreach ($get as $res) {
      '<li class="list-group-item"><span class="font-weight-bold">Product(s) : </span>';
    

    Missing something there, no?

    foreach ($get as $res) {
      echo '<li class="list-group-item"><span class="font-weight-bold">Product(s) : </span>';
    

    Sometimes it’s the simple things.

    Of note is PHP is perfectly fine with just having a useless string defined:

    'foo';
    

    Sandbox

    It won’t complain about that. For other languages this may be an issue, but PHP is very fault tolerant (sometimes to much so).

    PS require("connection.php"); is not a function, while it will work with the () it’s considered a bad practice. Instead require "connection.php"; works fine. This helps to differentiate it from a function when you glance at the code.

    You should also move this around a bit:

    foreach ($get as $res) {   
       //...
      for ($i=0; $i <= $pieces_count -1 ; $i++) { 
        $query2 = "SELECT * FROM suppliers WHERE id = :id";
        $get2 = $db->prepare($query2);
        $get2->bindValue(":id",$pieces[$i]);
        $get2->execute();
    

    And do it like this:

    $query2 = "SELECT * FROM suppliers WHERE id = :id";
    $get2 = $db->prepare($query2);
    
    foreach ($get as $res) {   
       //...
      for ($i=0; $i <= $pieces_count -1 ; $i++) { 
         $get2->execute(['id'=>$pieces[$i]]); //instead of binding, you can just do it in execute too
    

    And do the Prepare outside of the loop, this will improve performance in most cases. Every time you do a Prepare the DB is used to compile the query instructions, then when you execute it only the data is sent. This is how they defeat SQl Injection, because the SQL and the DATA are handled separately.

    So by preparing it within the Loop you are asking the DB for unnecessary work. The format of the SQL does not change per/loop only the data does. Therefor, there is only the need to execute the query for each iteration.

    Lastly:

    Your missing two closing }} in your code for the question. Make sure to fix those syntax errors. I suspect they are just a omission in the question as you stated the code works locally.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search