0

我正在用用户照片创建插件然后图像用 BLOB 保存到数据库中

当我将图像获取到数据库时,它无法正常工作。

我的代码 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>

如果您看到这一行<img src="<?php echo plugins_url( 'photo.php', __FILE__ ); ?>?pid=<?php echo $myadminphoto->apid; ?>&img=adminphoto" /> ,我会通过另一个页面 photo.php 调用图像

那么 photo.php 的文件就是这个

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;

    }

你去,没有图像显示也没有错误显示。

但是当我做这样的事情时,我得到照片的网址然后右键单击打开到新标签,错误说 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

我的代码有问题吗?

请告诉我谢谢...

4

1 回答 1

1

您需要在您的 php 文件中实例化 wordpress。

我认为在不破坏逻辑的情况下在 Wordpress 中执行此操作的最简单方法是在插件中定义一个这样的函数,以便像 ajax 调用一样使用它:

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

然后你可以这样称呼它:

<img src="<?php echo admin_url('admin-ajax.php'); ?>?action=MY_FUNCTION&pid=<?php echo $myadminphoto->apid; ?>&img=adminphoto" />
于 2013-01-31T15:40:13.847 回答