Using PHP / MySQL

This is a sort of long explanation, but trying to provide all the information the first time.
Hopefully someone can tell me what I’m missing or doing wrong, without too much criticism.

I am trying to create a FUNCTION that I can use to retrieve a list of the most recent friends that a member has referred to their website that have subsequently registered as a client(member)

I’m doing it in a function because this information will be displayed in a block on their back office dashboard, along with other data blocks on the same page with other lists in them, and I want to provide them a link/icon to click on to Refresh the listing of the information in that block only, without needing to reload the entire page...
Hopefully I can do that with a simple call to the function... guess I'll find out.

Hope that makes sense.

I intend to call the function with – function call: recent_friends($mem_id, $fmax, $ord_by, $dir)
Where the $mem_id is the logged in member, $fmax is the maximum number of entries to display, $ord_by is naturally the column to order by, and $dir is either Ascending or Decending

DB Table called ‘members’
(list of relevant fields)
mem_id / user / email / status / fname / lname / mem_status / join_date / ref_id

Function call in script:
$friends = recent_friends($_SESSION['mem_id'], 10, 'join_date', 'D');

// ***********************************************************************
//  retrieve list of friends referred by member to client website
//  call by setting variable = recent_friends(mem_id, max, ord_by, dir)

  function recent_friends($mem_id, $fmax, $ord_by, $dir)
  {
    if ($dir == 'A'){ $list_dir = 'ASC'; }else{ $list_dir = 'DESC'; }

    $sql_data = "
    SELECT mem_id, user, fname, lname, mem_status, join_date
    FROM members
    WHERE ref_id = '".$mem_id."'
    ORDER BY ".$ord_by." ".$list_dir;

    if ($fmax != 'A'){    // if limit defined - append to query
      $sql_data .= " LIMIT ".$fmax; 
    }

  $result = mysqli_query($connection,$sql_data);

  $friends = array();

    while($row = mysql_fetch_assoc($result)){
      $friends[] = $row;
    }

    return $friends;
  }//end function
When the function is called I get a blank screen

I don’t know if the function call is malformed or if the function itself has an issue causing it.

Also, tried it with the database connection call inside the function and without it... No change.

include "tm_connect.php"; // connects to database

And the page that should display this information in a table code section looks like this

                        <tbody>
<?php
     foreach ($friends as $row)
        {
?>
                            <tr>


                                <td>
                                    <span class="font-w600"><?php echo $row['user'];?></span>
                                </td>
                                <td class="d-none d-sm-table-cell text-center">
                                    <?php echo $row['fname']." ".$row['lname'];?>
                                </td>
                                <td class="font-w600">
                                    <?php echo $row['mem_status'];?>
                                </td>
                                <td class="d-none d-sm-table-cell text-center">
                                    <?php echo $row['join_date'];?>
                                </td>
 <?php
        }
?>
                           </tr>
                        </tbody>

When the function is called I get a blank screen

This happens when there's a PHP fatal error. Usually, it's a syntax error, but it can also be when a function call can't be made, parse error, or any number of different things. At the top of the PHP script, you can put the following to display on screen what the error actually is (just do this in developer mode and not in production so your end users won't see it):

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Also, and this is just a recommendation, you might wish to use PHP short tags. You may need to enable this on your server if it's not enabled by default, but they make it much easier to write PHP templates. Instead of your existing PHP template, when using PHP short tags, you can write it as the following instead, which makes it much clearer to understand:

<tbody>
    <?php foreach ($friends as $row): ?>
            <tr>
                <td>
                    <span class="font-w600"><?= $row['user'] ?></span>
                </td>
                <td class="d-none d-sm-table-cell text-center">
                    <?= $row['fname'] ?> <?= $row['lname'] ?>
                </td>
                <td class="font-w600">
                    <?= $row['mem_status'] ?>
                </td>
                <td class="d-none d-sm-table-cell text-center">
                    <?= $row['join_date'] ?>
                </td>
           </tr>
     <?php endforeach; ?>
</tbody>

Thanks Dani for your response...

found the first one...
Fatal error: Uncaught Error: Call to undefined function mysql_fetch_assoc()

should have been mysqli not mysql... still not used to that.

Then the next 2 were a notice and a warning that lead me to putting the connection call inside the function
include "tm_connect.php"; // connects to database

And Voila... it worked!!

Thank you again for your response, and I'll look into that other suggestion that you made. Not sure I fully understood it when I read it, but will study it to see what I need to do with it.

Douglas

PHP short tags are just a slightly different abbreviated php syntax that makes it easier to read and understand what’s going on when PHP is mixed into HTML templates.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.