Trying to do a db query... could use a little help...


I'm trying to figure out the proper query to do on the wp database to check if a form has been submitted by the logged in user, and then presenting different content depending on whether or not he/she has.

For testing, I'm using a plugin that allows me to add php to posts, so what I've got is this:

// check if logged in user has submitted the form
// output different text depending on whether user has submitted a form
global $wpdb, $user_ID;

$queryForms = $wpdb->prepare("SELECT id FROM ".RG_LEAD." WHERE created_by=%d AND form_id=2");
	$numberSubmissions = $wpdb->mysql_num_rows($queryForms);
	if($numberSubmissions = 0){
echo "no reports";
} else {
echo "reports";

But I'm getting no output at all. Where have I gone wrong?


  • scottmcculloch
    • The Incredible Code Injector

    I got partway to what I want - well, actually, I got what I was initially after and now want more! :wink:
    Here's what worked (I turned it into a plugin)

    add_shortcode( 'trimergence-ki-completed','display_ki_completed_text');
    	function display_ki_completed_text(){
    		global $wpdb, $user_ID;
    		$results = $wpdb->prepare("SELECT id FROM ".wp_rg_lead." WHERE FORM_ID=2 AND CREATED_BY=%d",$user_ID);
    		$theForm = $wpdb->get_results($results);
    		$submissionCount = $wpdb->num_rows;
    		if($submissionCount == 0){
    			$ki_completed_text = '<p>You have not yet completed the Kernel Insights.</p>';
    			} else {
    			$ki_completed_text = '<a class="uk-button" href="/self_assessments/kernel-insights-report/">View/edit your Kernel Insights (KI) Report</a><br /><p>The Kernel Insights is a little different than the other tools. Instead of creating a new version, you can edit an your existing report from the report page.</p>';
    		return $ki_completed_text;

    Now I'd like to add output that shows the value of 'id' -- but I'm not sure how. When I do this:
    I get this:

    Array ( [0] => stdClass Object ( [id] => 13 ) )

    I want to figure out how to just get that '13' (or whatever the id might happen to be)... but I'm not sure how to get it, everything I've tried has produced errors like:

    Cannot use object of type stdClass as array

    Please help. :slight_smile:

  • Milan
    • WordPress Wizard

    Hello @scottmcculloch,

    I hope you are having a good day and thanks for asking us. :slight_smile:

    First of all congrats that you solve all of your questions on your own.

    Dear member here for your future reference I want to inform you that you can always access public properties of php class with '->' special character. This characters used to represent accessing statement which you can use with initialized object to access its publicly exposed properties.

    For more information please go throughout this and let us know if you need further help with something else. :slight_smile:

    I hope this helps you.

    Milan Savaliya.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.