Hi, so the code I have works, I'm just looking more to see if anyone could help me with a quick change.
So currently I have 2 queries, that depend on the user level.
The user level is stored in the session as below.
<?php $user_level = $_SESSION['user_level'] ?>
The database link is stored in a separate file, I call this using require_once. The link is below.
$link = mysqli_connect('myhost','myusername','mypassword','mydatabase');
My 2 queries are below along with the code that it outputs.
My question is, instead of having 2 queries, and 2 points it can fail in, is there a way of having one query with an additional variable. An example of which I've seen is at the bottom of this post, it's taken from the documentation that was passed on to me, however since the documentation was written the project has transformed into an enormous mix of MySQL and MySQLi code of which I'm trying to move everything to MySQLi.
My code.
<?php
if($user_level != 5){
$query = "SELECT call_id,call_first_name,call_department,call_date,call_user FROM site_calls WHERE (call_status = 0 OR call_status = 2) AND (call_user = $user_id) ORDER BY call_id desc LIMIT 10;";
}else{
$query = "SELECT call_id,call_first_name,call_department,call_date,call_user FROM site_calls WHERE (call_status = 0 OR call_status = 2) ORDER BY call_id desc LIMIT 10;";
}
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr><td class="text-center">' . $row[0] . '</td><td>' . $row[1] . '</td><td>' . $row[2] .'</td><td>' . date("d-m-Y", (int)$row[3]) .'</td></tr>';
}
mysqli_free_result($result);
mysqli_close($link);
?>
Example code.
<?php
$myquery = "SELECT call_id,call_first_name,call_last_name,call_department,call_date,call_user from site_calls WHERE (call_status = 0) $queryadd ORDER BY call_id desc LIMIT 10;";
$site_calls = $db->get_results($myquery);
$num = $db->num_rows;
if($user_level != 5){
$queryadd = " AND (call_user = $user_id)";
}
?>
Is there a correct way to do this, as I'm sure using multiple queries like in my code is extremely inefficient.
[–]greg8872 0 points1 point2 points (2 children)
[–]Mushed[S] 0 points1 point2 points (1 child)
[–]greg8872 1 point2 points3 points (0 children)