Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I know eval is dangerous and evil but I need to use this for this situation. Basically I have two different databases that has the same table structure but with different data so I used a for each

loop to access both databases and a while loop to show all the databases results and I used eval so I can make the variables unique from each other. Everything seems to work well,

till I load the page and there are no errors but the page goes into a endless loop and it just keeps generating the first rows value which never progress showing the next rows value so how can I structure this better so I won't end up in a endless loop?

This is my code

<?php
    
    $search_user_input='a';
    
    $array_of_user_xs_database_name = array('agency_clients','agency_models');
    $array_of_user_xs = array('client','model');
    
    $iterator = new MultipleIterator();
    $iterator->attachIterator(new ArrayIterator($array_of_user_xs_database_name));
    $iterator->attachIterator(new ArrayIterator($array_of_user_xs));
    
    foreach($iterator as $index => $value){ 
    
    $user_xs_database_name= $value[0];
    $user_x= $value[1];
    $User_x= ucfirst($user_x);
    
    eval('
    $db_servername_getSearched'.$User_x.'="localhost";
    $db_username_getSearched'.$User_x.'="jd";
    $db_password_getSearched'.$User_x.'="1234";
    $db_name_getSearched'.$User_x.'= $user_xs_database_name;
    
    $db_connect_getSearched'.$User_x.'= new mysqli ($db_servername_getSearched'.$User_x.',$db_username_getSearched'.$User_x.',$db_password_getSearched'.$User_x.',
    $db_name_getSearched'.$User_x.');
    
    $db_query_getSearched'.$User_x.'= "SELECT uid, photo, CONCAT(first_name, \' \', last_name) AS name, signature_name, phone_number, email 
    FROM '.$user_x.'s WHERE CONCAT_WS(\' \','.$user_x.'s.first_name,'.$user_x.'s.last_name) LIKE \'%".$search_user_input."%\' 
    OR signature_name LIKE \'%".$search_user_input."%\' OR phone_number LIKE \'%".$search_user_input."%\' OR email LIKE \'%".$search_user_input."%\'";
    
    $db_result_getSearched'.$User_x.'= $db_connect_getSearched'.$User_x.'->query($db_query_getSearched'.$User_x.');
    ');
    
    $mysqli_result= eval('return $db_result_getSearched'.$User_x.';');
    
    if($mysqli_result-> num_rows >= 1){
    	
    $mysqli_row= eval('return $db_row_getSearched'.$User_x.'= $db_result_getSearched'.$User_x.'->fetch_assoc();');
    
    while($mysqli_row){
    
    ?>
    
    <h1>
    <?php 
    
    $photos= eval('return $db_row_getSearched'.$User_x.'["photo"];');
    echo $photos;
    ?>
    
    </h1>
    
    <?php
    }
    }
    }
    
    ?>


What I have tried:

Went on other forums ask questions no good answers and I even did a Google search.
Posted
Updated 4-Jun-19 3:35am
Comments
Mohibur Rashid 4-Jun-19 2:41am    
This is an absolute nightmare. My nightmare is better than the eval statement you wrote. Just try to imagine 10,000 lines of an average web application.

And your while loop does not have an exit strategy.
Member 13605567 4-Jun-19 6:18am    
Thanks for your reply Mohibur Rashid. I don't think you understand why I want to use eval for this. I'm using eval so I can have unique variables because later down the road i'm going to want to call a certain group of variable for example this $db_row_getSearched'.$User_x.' really means this $db_row_getSearchedClient or this $db_row_getSearchedModel because what if later down the road I want to call $db_row_getSearchedClient['Name'];
CHill60 4-Jun-19 8:45am    
Mohibur will not see your response - use the "Reply" link next to a comment to notify the member you are responding.
However, his point is valid - you do not have an exit strategy in the loop
Mohibur Rashid 4-Jun-19 9:11am    
https://stackoverflow.com/questions/4631246/risks-of-using-php-eval
read this thread on why not to use eval. Your problem can be fixed using alternative idea. ponder the ideas.
ZurdoDev 4-Jun-19 9:28am    
Or you can explain it to them.

1 solution

You have an endless loop because you've coded one:
PHP
while($mysqli_row){
    ?>    
    <h1>
    <?php 
    
    $photos= eval('return $db_row_getSearched'.$User_x.'["photo"];');
    echo $photos;
    ?>
        </h1>
        <?php
    }
Your loop continues based on the value of $mysqli_row, but nowhere in the loop does that value get changed; so once entered, you never leave it. Without spending a lot of time (and asking you a lot more questions about your databases and what you're trying to do), I've a suspicion that if you change while to if you'll get the result you want - though as other respondents have said, I would strongly recommend finding an alternative approach!
 
Share this answer
 
Comments
Member 13605567 4-Jun-19 15:36pm    
Thanks for responding @DerekTP123 I'm not sure if you saw this so this is what I say earlier I don't think you guys understand why I want to use eval for this. I'm using eval so I can have unique variables because later down the road i'm going to want to call a certain group of variable for example this $db_row_getSearched'.$User_x.' really means this $db_row_getSearchedClient or this $db_row_getSearchedModel because what if later down the road I want to call $db_row_getSearchedClient['Name'];
DerekT-P 5-Jun-19 4:49am    
Well hopefully your problem is resolved anyway - please mark it as such if the endless loop is resolved. Yes I'd read what you put about Eval but there are more ways than one to achieve anything. Eval can be useful but you've yet to convince us it's the best option in this particular case!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900