在php中获取具有相同用户ID的所有行值?

时间:2015-09-16 10:14:59

标签: php mysql wordpress

我正在使用wordpress网站,我只是坚持一个SQL查询。

我需要从数据库表中获取包含相同用户ID的值。

这是我的表:

enter image description here

当我在mysql中运行select查询时,它获取的结果是正确的:

enter image description here

但我不知道如何在我的php模板中获得此结果。

这是我在php中使用的查询:

$current_user = wp_get_current_user(); 
    $id=$current_user->ID;
    $skill1 =mysql_query("SELECT Skills FROM wp_candidate_skills WHERE user_id='{$id}' "); 

我无法使用get->结果,因为它显示的错误是get->结果必须包含对象。像这样的错误是。

这是我在Mysql_query之后使用的:

$arr = mysql_fetch_array($skill1);
  print_r($arr);

输出为:enter image description here

Wordpress代码:

public static function init_fields() {
        if ( self::$fields )
            return;
$current_user = wp_get_current_user(); 
    $user = $current_user->user_login;
    $useremail =  $current_user->user_email;
    $id=$current_user->ID;
$skill1 =mysql_query("SELECT ID,user_id,Skills FROM wp_candidate_skills WHERE user_id=$id"); 
$skill_note = mysql_fetch_array($skill1);
$result = mysql_query("SELECT * FROM wp_candidate_details WHERE user_id='{$id}' LIMIT 1"); 
$expe_note = mysql_fetch_assoc($result);
$job_title = $expe_note['job-cat1'];
$user_loc = $expe_note['location_one_id'];
$row_num = mysql_query("SELECT COUNT(Skills) FROM wp_candidate_skills WHERE user_id='{$id}' " );
$row_cnt = mysql_fetch_array($row_num);
$arr = mysql_fetch_array($skill1);
print_r($arr);
mysql_free_result($skill1);
    $skill_area = $skill_note['Skills'];
        self::$fields = apply_filters( 'submit_resume_form_fields', array(
            'resume_fields' => array(
                 'candidate_name' => array(
                    'label'       => __( 'Your name', 'wp-job-manager-resumes' ),
                    'type'        => 'text',
                    'required'    => true,
                    'placeholder' => __( 'Your full name', 'wp-job-manager-resumes' ),
                    'priority'    => 1,
                    'value'       => $user
                ),
                'candidate_email' => array(
                    'label'       => __( 'Your email', 'wp-job-manager-resumes' ),
                    'type'        => 'text',
                    'required'    => true,
                    'placeholder' => __( 'you@yourdomain.com', 'wp-job-manager-resumes' ),
                    'priority'    => 2,
                    'value'       => $useremail
                ),
                'candidate_title' => array(
                    'label'       => __( 'Professional title', 'wp-job-manager-resumes' ),
                    'type'        => 'text',
                    'required'    => true,
                    'placeholder' => __( 'e.g. "Web Developer"', 'wp-job-manager-resumes' ),
                    'priority'    => 3,
                    'value'       => $job_title
                ),
                'candidate_location' => array(
                    'label'       => __( 'Location', 'wp-job-manager-resumes' ),
                    'type'        => 'text',
                    'required'    => true,
                    'placeholder' => __( 'e.g. "London, UK", "New York", "Houston, TX"', 'wp-job-manager-resumes' ),
                    'priority'    => 4,
                    'value'       => $user_loc
                ),

            'resume_skills' => array(
                'label'       => __( 'Skills', 'wp-job-manager-resumes' ),
                'type'        => 'text',
                'required'    => false,
                'placeholder' => __( 'Comma separate a list of relevant skills', 'wp-job-manager-resumes' ),
                'priority'    => 9,
                'value'      =>  $skill_area

            ),
            'links' => array(
                'label'       => __( 'URL(s)', 'wp-job-manager-resumes' ),
                'type'        => 'links',
                'required'    => false,
                'placeholder' => '',
                'description' => __( 'Optionally provide links to any of your websites or social network profiles.', 'wp-job-manager-resumes' ),
                'priority'    => 10,
                'fields'      => array(
                    'name' => array(
                        'label'       => __( 'Name', 'wp-job-manager-resumes' ),
                        'type'        => 'text',
                        'required'    => true,
                        'placeholder' => '',
                        'priority'    => 1
                    ),
                    'url' => array(
                        'label'       => __( 'URL', 'wp-job-manager-resumes' ),
                        'type'        => 'text',
                        'required'    => true,
                        'placeholder' => 'http://',
                        'priority'    => 2
                    )
                )
            ),
            'candidate_education' => array(
                'label'       => __( 'Education', 'wp-job-manager-resumes' ),
                'type'        => 'education',
                'required'    => false,
                'placeholder' => '',
                'priority'    => 11,
                'fields'      => array(
                    'location' => array(
                        'label'       => __( 'School name', 'wp-job-manager-resumes' ),
                        'type'        => 'text',
                        'required'    => true,
                        'placeholder' => ''
                    ),
                    'qualification' => array(
                        'label'       => __( 'Qualification(s)', 'wp-job-manager-resumes' ),
                        'type'        => 'text',
                        'required'    => true,
                        'placeholder' => ''
                    ),
                    'date' => array(
                        'label'       => __( 'Start/end date', 'wp-job-manager-resumes' ),
                        'type'        => 'text',
                        'required'    => true,
                        'placeholder' => ''
                    ),
                    'notes' => array(
                        'label'       => __( 'Notes', 'wp-job-manager-resumes' ),
                        'type'        => 'textarea',
                        'required'    => false,
                        'placeholder' => ''
                    )
                )
            ),
            'candidate_experience' => array(
                'label'       => __( 'Experience', 'wp-job-manager-resumes' ),
                'type'        => 'experience',
                'required'    => false,
                'placeholder' => '',
                'priority'    => 12,
                'fields'      => array(
                    'employer' => array(
                        'label'       => __( 'Employer', 'wp-job-manager-resumes' ),
                        'type'        => 'text',
                        'required'    => true,
                        'placeholder' => ''
                    ),
                    'job_title' => array(
                        'label'       => __( 'Job Title', 'wp-job-manager-resumes' ),
                        'type'        => 'text',
                        'required'    => true,
                        'placeholder' => ''
                    ),
                    'date' => array(
                        'label'       => __( 'Start/end date', 'wp-job-manager-resumes' ),
                        'type'        => 'text',
                        'required'    => true,
                        'placeholder' => ''
                    ),
                    'notes' => array(
                        'label'       => __( 'Notes', 'wp-job-manager-resumes' ),
                        'type'        => 'textarea',
                        'required'    => false,
                        'placeholder' => ''
                    )
                )
            ), 
            'resume_file' => array(
                'label'       => __( 'Resume file', 'wp-job-manager-resumes' ),
                'type'        => 'file',
                'required'    => false,
                'description' => sprintf( __( 'Optionally upload your resume for employers to view. Max. file size: %s.', 'wp-job-manager-resumes' ), size_format( wp_max_upload_size() ) ),
                'priority'    => 13,
                'placeholder' => ''
            ),
        )
    ) );
    if ( ! get_option( 'resume_manager_enable_resume_upload' ) )
        unset( self::$fields['resume_fields']['resume_file'] );

    if ( ! get_option( 'resume_manager_enable_categories' ) || wp_count_terms( 'resume_category' ) == 0 )
        unset( self::$fields['resume_fields']['resume_category'] );

    if ( ! get_option( 'resume_manager_enable_skills' ) )
        unset( self::$fields['resume_fields']['resume_skills'] );
}

帮帮我们!感谢

4 个答案:

答案 0 :(得分:1)

试试这段代码,`$ skill1 = $ wpdb-> get_results(“SELECT ID,user_id,Skills FROM wp_candidate_skills WHERE user_id ='$ id'”);

        $count = count($skill1);
        $array = array();
        foreach ($skill1 as $key => $value) {
            $sk = $value->Skills;
            $array[] = $sk;
        }

        $fixId =array();
        $fixId[0] ='';
        for($i=0;$i<=$count-1;$i++)
        {
            $temp = $array[$i];
            $fixId[0] = $fixId[0].$temp.',';
        }
        $data = $fixId[0] ;
        $words = array_filter(preg_split('/\s*,\s*/', $data));
        $temp = implode(', ', $words);`

它将数据作为一个数组获取,并在两个值之间插入逗号。

答案 1 :(得分:0)

使用get_results,如下所示:

<?php

    global $wpdb;   //important  

    $result = $wpdb->get_results("SELECT Skills FROM wp_candidate_skills WHERE user_id='$id' ");  

    foreach($result as $row)
    {    
        echo $row->Skills."<br>";    
    }

Display mysql table data in a wp page

答案 2 :(得分:0)

试试这种方式

$current_user = wp_get_current_user(); 

$id=$current_user->ID;

$skill1 =mysql_query("SELECT Skills FROM wp_candidate_skills WHERE user_id='".$id."' ");

$arr = mysql_fetch_array($skill1);

print_r($arr);

答案 3 :(得分:0)

使用以下代码:

public static function init_fields() {
        if ( self::$fields )
            return;

    global $wpdb;
    $current_user = wp_get_current_user(); 
    $user = $current_user->user_login;
    $useremail =  $current_user->user_email;
    $id=$current_user->ID;

    $result = $wpdb->get_results("SELECT ID,user_id,Skills FROM wp_candidate_skills WHERE user_id=$id");

    print_r($result);
    die();
}