Jump to content

Linux mysql sort to mysqli sort not working


stpsoft

Recommended Posts

This was a working sort before we upgraded our web server....

 $result = (isset($sort)) ? mysqli_query($db, "Select * from ". $company . " where member = $member_numbers[$i] order by location, $sort, name")
: mysqli_query($db, "Select * from " . $company . " where member = $member_numbers[$i] order by location, name");
 

Data is display, just not sorted....

Thanks

Link to comment
Share on other sites

Script works under php 5.....upgrading to php 7

$result = (isset($sort)) ? mysqli_query........(as shown above)

isset($sort) does not appear to set the "sort" variable

Edited by stpsoft
add additional info
Link to comment
Share on other sites

Just my 0.02 worth ...

1. It is always a bad idea to place values provided by the user ($_GET, $_POST, $_COOKIE) directly into your SQL. A better way is this, which avoids doing that and also checks that only valid column names can be used

$sort = $_GET['sort'] ?? '';
switch ($sort) {
    case 'recd_date':
            $orderby = 'location, recd_date, name';
            break;
    case 'sent_date':
            $orderby = 'location, sent_date, name';
            break;
    case 'birth_date':
            $orderby = 'location, birth_date, name';
            break;
    default:
            $orderby = 'location, name';
            break;
}

2. Use prepared statements to place values into your query

$result = $db->prepare("SELECT *
                       FROM $company
                       WHERE member = ?
                       ORDER BY $orderby
                       ");
$result->bind_param('i', $member_number[$i]);
$result->execute();

3. The use of that [$i] index - are you running the query in a loop for different member_numbers? Don't.

4. You have a variable table name ($company) which suggests you have several tables, each for a different company. Better to have a single table and add an indexed "company" column.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.