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
Replace your code with the following and check
You have not closed the PHP, closing curly braces were not there…
Try echoing it out:
Missing something there, no?
Sometimes it’s the simple things.
Of note is PHP is perfectly fine with just having a useless string defined:
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. Insteadrequire "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:
And do it like this:
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.