Support » Fixing WordPress » Database guru wanted! How to get data from wp_postmeta column meta_key?

Viewing 15 replies - 1 through 15 (of 19 total)
  • You could use something like this:

    <?php
    
        $conn = mysql_connect("localhost", $userName, $passWord);
        mysql_select_db($DB);
        $sql = mysql_query("SELECT * FROM tableName");
    
    ?>

    Then your data would be stored in the $sql variable. This code is a template and would need to be adjusted to suit your specific needs.

    Thanks Josh,

    I tried your template, but no lucky so far.

    How did you modify it?

    Post your code… but don’t include any sensitive info like username or password. I’m interested in what you changed this to:

    $sql = mysql_query("SELECT * FROM tableName");

    Thanks for your help.

    “data7” is my meta_key that I want to access and get the data from it. I tried with below.

    $meta_key = 'data7';
    $my_data = $wpdb->get_var( $wpdb->prepare(
    	"
    		SELECT (meta_key)
    		FROM $wpdb->postmeta
    		WHERE meta_key = 'data7'
    	",
    	$meta_key
    ) );
    echo "<p>My data is {$my_data}</p>";

    How about this?

    $meta_key = 'data7';
    $my_data = $wpdb->get_var( $wpdb->prepare(
    	"
    		SELECT (meta_key)
    		FROM $wpdb->wp_postmeta
    		WHERE meta_key = 'data7'
    	",
    	$meta_key
    ) );
    echo "<p>My data is {$my_data}</p>";

    Okay, no error now. But the echo is not printing anything. It seems to be empty.

    Take the brackets out of your bottom line:

    $meta_key = 'data7';
    $my_data = $wpdb->get_var( $wpdb->prepare(
    	"
    		SELECT (meta_key)
    		FROM $wpdb->wp_postmeta
    		WHERE meta_key = 'data7'
    	",
    	$meta_key
    ) );
    echo "<p>My data is $my_data</p>";

    I’m put this code on my page.php

    <?php
    $meta_key = 'data7';
    $my_data = $wpdb->get_var( $wpdb->prepare(
    	"	SELECT (meta_key)
    		FROM $wpdb->wp_postmeta
    		WHERE meta_key = 'data7'
    	",
    	$meta_key
    ) );
    echo <p>My data is {$my_data}</p>;
    ?>

    If I take the brackets out, I get HTTP Error 500.

    What is it exactly you are trying to accomplish” Do you just want to pull one record with “data7” or all records with “data7”?

    Fair enough your question.

    I’m using Job Manager plugin, which store all the candidate info on wp_postmeta on meta_keys called data2, date3, date4, data5, data6 and data7. What I want to do, is to be able to extract those info from the database and show them like a list of candidates on any regular page.

    I’m using Job Manager plugin, which store all the candidate info on wp_postmeta on meta_keys called data2, date3, date4, data5, data6 and data7.

    Is it date1, date2, date3 or data1, data2, data3?

    Sorry, my typo. It’s dataN

    Okay, try this:

    <?php
    // set the meta_key to the appropriate custom field meta key
    $meta_key = 'data7';
    $mydata = $wpdb->get_var( $wpdb->prepare(
    	"
    		SELECT sum(meta_value)
    		FROM $wpdb->wp_postmeta
    		WHERE meta_key = %s
    	",
    	$meta_key
    ) );
    echo "<p>My data is {$mydata}</p>";
    ?>

    I tried above. No error, but the variable still empty.

    Are you sure you have a table entry with “data7” in the wp-postmeta column??

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Database guru wanted! How to get data from wp_postmeta column meta_key?’ is closed to new replies.