phpmysqlarrayswordpressuser-data

From user table data doesn't retrive in hostinger


I used the following script, retrieve the user ID from user table on WordPress. This script is working local server, all other server. But this is not working in the Hostinger server.

Why this is not working in Hostinger?

Only this is print_r($query); but $userid=$query[0][ID]; ID not found

<?php
    if (sanitize_text_field($_GET['ven_search_title']) && sanitize_text_field ($_GET['ven_search_title']))
    {
        $ven_search_title = sanitize_text_field($_GET['ven_search_title']);
    }

    global $wpdb;
    $table = $wpdb->prefix . 'users'; 
    $query = $wpdb->get_results("SELECT * FROM $table WHERE user_nicename LIKE '%".$ven_search_title."%'", ARRAY_A);

    
    if($query){
 
    echo "<pre>";
    print_r ($query);
    echo "</pre>";  
 
    $userid=$query[0][ID];
    
    echo "<pre>";
    print_r ($query[0][ID]);
    echo "</pre>";  
?>     
<?php $user=get_userdata($userid)?>
    <a href="<?php echo bloginfo('url').'/store/'.$user->user_nicename; ?>">
    <div class="store-wrappers"> 
        <div class="seller-avatar"><?php echo get_avatar($userid) ?></div>
        <div class="storebody">
            <div class="search_title"><span>Store Name:</span> <?php echo $user->user_nicename; ?> </div> 
        </div>   
    <div style="clear:both;"></div>  

Solution

  • There are some mistakes in your code. For example bloginfo('url') is already echoed by itself. Your code can be simplified a bit and secured.

    Try the following:

    if ( isset($_GET['ven_search_title']) && ! empty($_GET['ven_search_title']) ) {
        global $wpdb;
    
        $user_id = $wpdb->get_var( $wpdb->prepare("
            SELECT ID FROM {$wpdb->prefix}users WHERE user_nicename LIKE '%s' 
        ", '%'.sanitize_text_field($_GET['ven_search_title']).'%' ));
    
        if( $user_id > 0 ){
            $user     = new WP_User($user_id);
            $nicename = $user->user_nicename;
            ?>
            <a href="<?php echo site_url('/store/'.$nicename); ?>">
            <div class="store-wrappers"> 
                <div class="seller-avatar"><?php echo get_avatar($user_id) ?></div>
                <div class="storebody">
                    <div class="search_title"><span><?php _e('Store Name'); ?>:</span> <?php echo $nicename; ?></div> 
                </div>   
                <div style="clear:both;"></div>
            <div><?php 
        } 
    }
    

    Tested, lightweight, more secure and work.