Butternut
Butternut

Reputation: 843

wordpress wpdb display image error

I was creating plugin with users photos Then the image was save to the database with BLOB

When im going to get the image to the database it was not workin.

My codes user-management.php

<table cellpadding="5" cellspacing="0">
    <thead>
    <tr>
        <th>Username</th>
        <th>Email</th>
        <th>First Name</th>
        <th>Middle Name</th>
        <th>Last Name</th>
        <th>Status</th>
        <th>Photo</th>
        <th>Date</th>
        <th>Option</th>
     </tr>
    </thead>
    <tbody>
<?php
global $wpdb;
$myadmin = $wpdb->get_results( "SELECT * FROM jon_admin" );
$color1 = "#F8F8F8"; 
$color2 = "#FFFFFF"; 
$row_count = 0;
foreach ($myadmin as $admin) {
    $myadminphoto = $wpdb->get_row( "SELECT * FROM jon_admin_photo WHERE user_login = '".$admin->user_login."' " );
$row_color = ($row_count % 2) ? $color1 : $color2;
    ?>
        <tr>
            <td style="background-color:<? echo $row_color; ?>"><?php echo $admin->user_login; ?></td>
            <td style="background-color:<? echo $row_color; ?>"><?php echo $admin->email; ?></td>
            <td style="background-color:<? echo $row_color; ?>"><?php echo $admin->fname; ?></td>
            <td style="background-color:<? echo $row_color; ?>"><?php echo $admin->mname; ?></td>
            <td style="background-color:<? echo $row_color; ?>"><?php echo $admin->lname; ?></td>
            <td style="background-color:<? echo $row_color; ?>"><?php echo $admin->user_status; ?></td>
            <td style="background-color:<? echo $row_color; ?>">
            <img src="<?php echo plugins_url( 'photo.php', __FILE__ ); ?>?pid=<?php echo $myadminphoto->apid; ?>&img=adminphoto" />                </td>
            <td style="background-color:<? echo $row_color; ?>"><?php echo $admin->date; ?></td>
            <td style="background-color:<? echo $row_color; ?>">edit view delete</td>
        </tr>
<?php
$row_count++;
}       
?>
    </tbody>
</table>

if you see this line <img src="<?php echo plugins_url( 'photo.php', __FILE__ ); ?>?pid=<?php echo $myadminphoto->apid; ?>&img=adminphoto" /> im calling the image via another page which is photo.php

then the file of photo.php was this

if ( $_GET['img'] == 'adminphoto' ) {
    global $wpdb;
        ## VIEW ADMIN PHOTO
        $id = $_GET['pid'];
        $get_photo =  $wpdb->get_row( "SELECT * FROM jon_admin_photo WHERE apid = '".$id."'" );

            $content = $get_photo->image;
            $filetype = $get_photo->imgcode;
            header('Content-type: $filetype');
            echo $content;

    }

There you go, there is no image display also there is no error display.

But when i do something like this, I get the url of the photo then right click open to new tab it the error said Fatal error: Call to a member function get_row() on a non-object in C:\xampp\htdocs\web\advanced-nanny\wp-content\plugins\jon-user-management\photo.php on line 37

Is there something wrong with my codes?

Please tell me thank you...

Upvotes: 0

Views: 291

Answers (1)

Philippe Boissonneault
Philippe Boissonneault

Reputation: 3949

You'll need to instanciate wordpress in your php file.

I think the easiest way to do this in Wordpress without breaking your logic is to define a function in your plugin like this to use it like an ajax call:

function MY_FUNCTION() {
    if ( $_GET['img'] == 'adminphoto' ) {
        global $wpdb;
        ## VIEW ADMIN PHOTO
        $id = $_GET['pid'];
        $get_photo =  $wpdb->get_row( "SELECT * FROM jon_admin_photo WHERE apid = '".$id."'" );

            $content = $get_photo->image;
            $filetype = $get_photo->imgcode;
            header('Content-type: $filetype');
            echo $content;

    }
    exit();
}
add_action('wp_ajax_MY_FUNCTION', 'MY_FUNCTION');
add_action('wp_ajax_nopriv_MY_FUNCTION', 'MY_FUNCTION');//for users that are not logged in

Then you can call it this way :

<img src="<?php echo admin_url('admin-ajax.php'); ?>?action=MY_FUNCTION&pid=<?php echo $myadminphoto->apid; ?>&img=adminphoto" />

Upvotes: 1

Related Questions