I want to call a PHP function :
$rights = $user->recupererDroitCreateur($_SESSION[CODE_USER]);
Code of recupererDroitCreateur is :
function recupererDroitCreateur($user_id) {
$ret = array();
$sSQL = "SELECT cm.class_menu_code
FROM menu m
LEFT JOIN classe_menu cm
ON m.class_menu_code = cm.class_menu_code
WHERE m.menu_deleted = 0 AND m.menu_visible = 1 AND cm.class_menu_parent IS NULL AND cm.class_menu_deleted = 0
ORDER BY cm.class_menu_lib, m.menu_titre";
$this->db->query($sSQL);
while ( $this->db->next_record() ) {
$code = $this->db->f('class_menu_code');
$strMenus = $code.";";
$this->recupererMenus($code, $code, $user_id, $strMenus);
}
return (explode(";", $strMenus));
}
Code of recupererMenus is :
function recupererMenus($classRoot, $classParentDirect, $user_id, &$menus)
{
$sSQL1 = "SELECT class_menu_code FROM classe_menu WHERE class_menu_parent = '$classParentDirect' AND class_menu_deleted = 0";
$this->db->query($sSQL1);
while ( $this->db->next_record() ) {
$this->recupererMenus($classRoot, $this->db->f('class_menu_code'), $user_id, $menus);
}
$sSQL = "SELECT m.menu_code
FROM menu m
LEFT JOIN classe_menu cm
ON m.class_menu_code = cm.class_menu_code
WHERE m.menu_deleted = 0 AND m.menu_visible = 1 AND cm.class_menu_parent = '$classParentDirect' AND cm.class_menu_deleted = 0
ORDER BY cm.class_menu_lib, m.menu_titre";
$this->db->query($sSQL);
while ( $this->db->next_record() )
{
$menus .= $this->db->f('menu_code').";";
}
}
In runtime there is the error next_record called with no query pending : the error output tells the lines which cause the error and it says the line corresponding to this statement : while ( $this->db->next_record() ) { , there is also line pointing to the statement $this->recupererMenus($code, $code, $user_id, $strMenus);
So what is wrong in my codes ?
In recupererMenus you recursively call recupererMenus. Since there's no query identifier passed to next_record, using multiple querys will create a big mess.. When you return from the recursive call, you are going to try to fetch the next record with next_record for a previous query, but your DB framework doesn't know about this..
I don't know the framework you use, but you either have to pass the identifier (if it's possible at all), or first fetch all the results, and then do the recursive call.
Related
I am trying to pull dynamic content from a database based on the wildcard subdomain.
Please see the code below.
<?php
$data = $_GET['get'];
$data = addslashes(str_replace('-',' ',$data));
$town_result = $database->getData("select * from nationwide where town = '$data' ");
if( is_object( $town_result ) )
{
$area = $town_result->fetch_assoc();
}
else
{
$region_result = $database->getData("select * from nationwide where region = '$data' ");
if( !is_object( $region_result ) ) {
header('Location: http://example.com');
}
$area = $region_result->fetch_assoc();
$area['town'] = $area['region'];
}
?>
I have tried changing = '$data' "); to LIKE '%" . $data . "%'"); but this only pulls the first row in my database.
You can review how to use fetch_assoc to get associative records.
https://www.php.net/manual/en/mysqli-result.fetch-assoc.php
You can print $area, then you can verify either array count is 1 or more than 1.
Or best way, you can print sql query and run directly in your db.
I am wondering if you can make the Table name of the propel query dynamic(sort of like a variable variable)? An example would be like \"DynamicVar"Query::create(). I have it working in ifs like in my example below, but could get rid of quite a few lines if made more dynamically. The tables are all setup the same, so they can be treated like the same object they just have different names.
Currently have something like this happening:
//$dynamic is a result of grabbing it from a different table
//that corresponds to values passed by AJAX
$dyanmic = "Customer"
$query = null;
If( $dynamic == "Customer" ) $query = \CustomerQuery()::create();
If( $dynamic == something2 ) $query = \Table2Query()::create();
If( $dynamic == something3 ) $query = \Table3Query()::create();
If( $query != null ) {
$query->filterBy("something");
$query->find();
}
I played around with the code some, and the code below will work for dynamically changing the table as long as each table can be treated like the same object. You can define your $table and use it in this fashion for a function that returns the object that you want
function Get_Table($table,$id){
$query = null;
$queryClass = '\\'. ucfirst($table) . 'Query';
if ( class_exists( $queryClass ) ) {
$$dynamics = $queryClass::create()
->filterById($id)
->find();
if( $dynamics->getFirst() == null ) { return false; }
/** #var \Base\.ucfirst($table) $dynamic*/
$dynamic= $dynamics->getFirst();
return $dynamic;
}
//On Failure
else {
return false;
}
}
I'm getting a error 500 when i try to select something from the database:
I've test it without the select function and just return a random string and it worked. But when i try to get a value from the database i'll get an error 500.
This is the function:
public function seasons_rules($CheckIn)
{
$Request = $this->db->get_results(
$this->db->prepare(
"SELECT A.rule_id
FROM $this->booking_rules_seasons_table AS A
INNER JOIN $this->seasons_dates_table AS B
ON B.season_id = A.seasons_id
INNER JOIN $this->booking_rules_table AS C
ON A.rule_id = C.id
WHERE ('%s' BETWEEN B.start_date AND B.end_date) OR C.all_seasons = 1
",$CheckIn), ARRAY_A);
$RulesIDs = '';
if ( ( $Request == NULL ) || ( count( $Request ) == 0 ) ) {
return false;
} else {
foreach ($Request as $response) {
$RulesIDs .= $response['rule_id'].',';
}
return $RulesIDs;
}
}
When i run the query directly into database. I'll get a result back so there isn't any errors in the query.
You're passing the variable inside the string in the wrong way.
If you want to pass a variable of an object inside a string, you use this syntax:
echo "This is my string: {$obj->string}";
You're passing the variable directly without the braces.
I'm new and I feel lost. Is this code doing a infinite loop?
Should I remove line 2-4? Is line 19 "$_row['employee_manager_id']," even important?
Any advice on how I should do this? Thank you.
function get_employees_by_hierarchy( $_employee_id = 0,$_depth = 0,$_org_array = array() ) {
if ( $this->org_depth < $_depth ) {
$this->org_depth = $_depth;
}
$_depth++;
$_query = "SELECT * FROM employees WHERE ";
if ( !$_employee_id ) {
$_query .= "employee_manager_id IS NULL OR employee_manager_id = 0";
}
else {
$_query .= "employee_manager_id = " . $this->dbh->quoteSmart( $_employee_id );
}
$_result = $this->query( $_query );
while ( $_row = $_result->fetchRow() ) {
$_row['depth'] = $_depth;
array_push( $_org_array, $_row );
$_org_array = $this->get_employees_by_hierarchy(
$_row['employee_manager_id'],
$_depth,
$_org_array
);
}
return $_org_array;
}
From the looks of it:
It does do an infinite loop. There isn't a terminating condition on the recursion. you need a condition that causes this:
$_org_array = $this->get_employees_by_hierarchy(
$_row['employee_manager_id'],
$_depth,
$_org_array
);
to not run, or at least to assign it to something that isn't get_employees_by_hierarchy.
after you fix that there it will be an infinite loop if an employee is their own manager at some point.
Impossible to say, is 'depth' used anywhere else? This function returns the $_org_array that has the 'depth' field but we can't see if it's used. It certainly isn't breaking anything in this code.
Without seeing sample data I'd say, yes it's important. The code recurses using the self referential employees table using the manager's id as the next level of the hierarchy.
put a check in before you assign the $_org_array that checks if the manager's id is null, if that happens do something like this: $_org_array = $this.
I'm trying to pass an array to a model which has a query. I'm not sure how to correctly pass the array or if i have to manipulate the array somehow.
I have this array:
Array
(
[0] => 1
[1] => 2
)
I have a controller with this line:
$ratings = $this->login_model->get_ratings($mechanicIds); // get the mechanic ratings
I have this model:
function get_ratings($mechanicId)
{
$sql = "select m.mechanic_id,
m.mechanic_name,
m.city,
m.state,
count(mr.rating_id) as num_ratings,
round(avg(mr.rating_id),2) avg_rating
from mechanic m, mechanic_rating mr, rating r
where m.mechanic_id in (?)
and m.mechanic_id = mr.mechanic_id
and mr.rating_id = r.rating_id";
$query = $this->db->query($sql, $mechanicId);
if($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
return false;
}
}
It actually returns results, but the problem is it only returns the results 1 row when it should be returning 2 since there are 2 results in my array. Anyone know what i'm doing wrong?
I found this question which helped.
Below is the code I used.
Controller that contains this:
$mIds_size = count($mIds);
$i = 1;
foreach($mIds as $row)
{
if($i == $mIds_size)
{
$mechanicIds .= $row;
}
else
{
$mechanicIds .= $row.', ';
}
$i++;
}
$ratings = $this->login_model->get_ratings($mechanicIds); // get the mechanic ratings
Model which contains this:
function get_ratings($mechanicId)
{
$this->db->escape($mechanicId);
$sql = "select m.mechanic_id,
m.mechanic_name,
m.city,
m.state,
count(mr.rating_id) as num_ratings,
round(avg(mr.rating_id),2) avg_rating
from mechanic m, mechanic_rating mr, rating r
where m.mechanic_id in ($mechanicId)
and m.mechanic_id = mr.mechanic_id
and mr.rating_id = r.rating_id
group by mechanic_id";
$query = $this->db->query($sql, $mechanicId);
if($query->num_rows() > 0)
{
return $query->result_array();
}
else
{
return false;
}
}
Change this line:
$query = $this->db->query($sql, $mechanicId);
to this:
$query = $this->db->query($sql, array(implode(', ',$mechanicId)));
as per the manual
To pass an array into a raw query (not built with helper methods) for an IN condition, use the ? placeholder without wrapping in parentheses.
When binding the array to the placeholder, you must declare your array inside of an array. In other words, the "parameters" (2nd) argument of query() method expects an array which relates to each ? in the SQL string. Because the first ? is bound to an array, the $mechanicIds array must be declared as the first element of the "parameters" argument. I have tested this advice to work successfully in a CodeIgniter3 instance that I have access to.
$sql = "SELECT m.mechanic_id,
m.mechanic_name,
m.city,
m.state,
COUNT(mr.rating_id) AS num_ratings,
ROUND(AVG(mr.rating_id), 2) AS avg_rating
FROM mechanic AS m,
mechanic_rating AS mr,
rating AS r
WHERE m.mechanic_id IN ? /* <--- here */
AND m.mechanic_id = mr.mechanic_id
AND mr.rating_id = r.rating_id";
$query = $this->db->query($sql, [$mechanicIds]);
The DB_driver.php core file contains this portion of code in compile_binds() which escapes each value in the array and wraps the comma-joined string in parentheses before returning the sql string.
...
do
{
$c--;
$escaped_value = $this->escape($binds[$c]);
if (is_array($escaped_value))
{
$escaped_value = '('.implode(',', $escaped_value).')';
}
$sql = substr_replace($sql, $escaped_value, $matches[0][$c][1], $ml);
}
while ($c !== 0);
...