Display data from SQL Database in WordPress












1















I am new in WordPress so i need to display some data from database to a WordPress php page.



I add this code inside my functions.php (inside my installed theme)



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
global $wpdb;

//Count completed courses for logged user.
$ws_completed_courses = $wpdb->get_results("SELECT COUNT(item_id) FROM
$wpdb->user_items WHERE status = 'completed' AND used_id =
$ws_current_user");
}


And after that inside my dashboard.php i have added the next code...



<p>The user has been finished <?php echo $ws_completed_courses ; ?> courses</p>


But i only see on website The user has been finished courses



Where is my mistake and how to solve the problem?



Thanks!



UPDATE 21-Nov-18



function.php



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
var_dump($ws_current_user);
global $wpdb;

$ws_completed_courses = $wpdb->get_results( "SELECT COUNT(item_id) FROM
$wpdb->user_items WHERE status = 'completed' AND user_id =
$ws_current_user" );

foreach ($ws_completed_courses as $course) {
$completedCourseCount = $course->user_item_id;
}
return $completedCourseCount;
}


dashboard.php



<p>The user has been finished <?php echo ws_count_completed_courses(); ?> courses</p>









share|improve this question

























  • Does "get_results" return an array of fields? I think you need the first field in the results. I think you are treating it like a string.

    – Cory Trese
    Nov 20 '18 at 21:02











  • "get_results" should return string because i use COUNT, and it should return only one number...

    – Martin M
    Nov 20 '18 at 21:13
















1















I am new in WordPress so i need to display some data from database to a WordPress php page.



I add this code inside my functions.php (inside my installed theme)



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
global $wpdb;

//Count completed courses for logged user.
$ws_completed_courses = $wpdb->get_results("SELECT COUNT(item_id) FROM
$wpdb->user_items WHERE status = 'completed' AND used_id =
$ws_current_user");
}


And after that inside my dashboard.php i have added the next code...



<p>The user has been finished <?php echo $ws_completed_courses ; ?> courses</p>


But i only see on website The user has been finished courses



Where is my mistake and how to solve the problem?



Thanks!



UPDATE 21-Nov-18



function.php



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
var_dump($ws_current_user);
global $wpdb;

$ws_completed_courses = $wpdb->get_results( "SELECT COUNT(item_id) FROM
$wpdb->user_items WHERE status = 'completed' AND user_id =
$ws_current_user" );

foreach ($ws_completed_courses as $course) {
$completedCourseCount = $course->user_item_id;
}
return $completedCourseCount;
}


dashboard.php



<p>The user has been finished <?php echo ws_count_completed_courses(); ?> courses</p>









share|improve this question

























  • Does "get_results" return an array of fields? I think you need the first field in the results. I think you are treating it like a string.

    – Cory Trese
    Nov 20 '18 at 21:02











  • "get_results" should return string because i use COUNT, and it should return only one number...

    – Martin M
    Nov 20 '18 at 21:13














1












1








1








I am new in WordPress so i need to display some data from database to a WordPress php page.



I add this code inside my functions.php (inside my installed theme)



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
global $wpdb;

//Count completed courses for logged user.
$ws_completed_courses = $wpdb->get_results("SELECT COUNT(item_id) FROM
$wpdb->user_items WHERE status = 'completed' AND used_id =
$ws_current_user");
}


And after that inside my dashboard.php i have added the next code...



<p>The user has been finished <?php echo $ws_completed_courses ; ?> courses</p>


But i only see on website The user has been finished courses



Where is my mistake and how to solve the problem?



Thanks!



UPDATE 21-Nov-18



function.php



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
var_dump($ws_current_user);
global $wpdb;

$ws_completed_courses = $wpdb->get_results( "SELECT COUNT(item_id) FROM
$wpdb->user_items WHERE status = 'completed' AND user_id =
$ws_current_user" );

foreach ($ws_completed_courses as $course) {
$completedCourseCount = $course->user_item_id;
}
return $completedCourseCount;
}


dashboard.php



<p>The user has been finished <?php echo ws_count_completed_courses(); ?> courses</p>









share|improve this question
















I am new in WordPress so i need to display some data from database to a WordPress php page.



I add this code inside my functions.php (inside my installed theme)



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
global $wpdb;

//Count completed courses for logged user.
$ws_completed_courses = $wpdb->get_results("SELECT COUNT(item_id) FROM
$wpdb->user_items WHERE status = 'completed' AND used_id =
$ws_current_user");
}


And after that inside my dashboard.php i have added the next code...



<p>The user has been finished <?php echo $ws_completed_courses ; ?> courses</p>


But i only see on website The user has been finished courses



Where is my mistake and how to solve the problem?



Thanks!



UPDATE 21-Nov-18



function.php



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
var_dump($ws_current_user);
global $wpdb;

$ws_completed_courses = $wpdb->get_results( "SELECT COUNT(item_id) FROM
$wpdb->user_items WHERE status = 'completed' AND user_id =
$ws_current_user" );

foreach ($ws_completed_courses as $course) {
$completedCourseCount = $course->user_item_id;
}
return $completedCourseCount;
}


dashboard.php



<p>The user has been finished <?php echo ws_count_completed_courses(); ?> courses</p>






php mysql wordpress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 14:49







Martin M

















asked Nov 20 '18 at 20:47









Martin MMartin M

114




114













  • Does "get_results" return an array of fields? I think you need the first field in the results. I think you are treating it like a string.

    – Cory Trese
    Nov 20 '18 at 21:02











  • "get_results" should return string because i use COUNT, and it should return only one number...

    – Martin M
    Nov 20 '18 at 21:13



















  • Does "get_results" return an array of fields? I think you need the first field in the results. I think you are treating it like a string.

    – Cory Trese
    Nov 20 '18 at 21:02











  • "get_results" should return string because i use COUNT, and it should return only one number...

    – Martin M
    Nov 20 '18 at 21:13

















Does "get_results" return an array of fields? I think you need the first field in the results. I think you are treating it like a string.

– Cory Trese
Nov 20 '18 at 21:02





Does "get_results" return an array of fields? I think you need the first field in the results. I think you are treating it like a string.

– Cory Trese
Nov 20 '18 at 21:02













"get_results" should return string because i use COUNT, and it should return only one number...

– Martin M
Nov 20 '18 at 21:13





"get_results" should return string because i use COUNT, and it should return only one number...

– Martin M
Nov 20 '18 at 21:13












2 Answers
2






active

oldest

votes


















0














There are $wpdb tutorials you could google; but, here's a conceptual overview that might help get you started:




  1. From a SQL prompt, make sure your query works. Then figure out how to adapt the SQL to use program variables in your PHP code.



  2. Take a look at the structure you are getting back from your query:



    var_dump($ws_current_user);




  3. There are lots of way to extract the data from the returned structure, but the most common approach is something like this:



    foreach ($ws_completed_courses as $course) {
    $completedCourseCount = $course->colum_you_want;
    }




  4. Return the value you extracted as the last line in your function:



    return $completedCourseCount;




  5. Call the function like this:



    echo ws_count_completed_courses();








share|improve this answer


























  • Hi ChuckC, i try with your solution but i receive int(3), i am not sure where is the problem... i also try to test the SQL statement using another statement to count registered users... but the results is the same... My code is updated right now, could you please check the updated section and give me another instructions?

    – Martin M
    Nov 21 '18 at 14:45



















0














$wpdb->get_results returns an array or an object. So try one of these:



echo $ws_completed_courses['COUNT(item_id)'];
echo $ws_completed_courses->COUNT(item_id);


EDIT: Ok, try this then:



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
global $wpdb;

$ws_completed_courses = $wpdb->get_results( "SELECT COUNT(item_id) as user_item_id FROM $wpdb->user_items WHERE status = 'completed' AND user_id = $ws_current_user" );

foreach ($ws_completed_courses as $course) {
$completedCourseCount = $course->user_item_id;
}
return $completedCourseCount;
}





share|improve this answer


























  • They didn't work also.. could you please check the edit on my code? Thanks

    – Martin M
    Nov 21 '18 at 22:23











  • Ok see above, made a small tweak to the SQL query.

    – Richard Zack
    Nov 22 '18 at 4:02











  • Did it work changing to "SELECT COUNT(item_id) as user_item_id"

    – Richard Zack
    Nov 24 '18 at 15:15











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401268%2fdisplay-data-from-sql-database-in-wordpress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














There are $wpdb tutorials you could google; but, here's a conceptual overview that might help get you started:




  1. From a SQL prompt, make sure your query works. Then figure out how to adapt the SQL to use program variables in your PHP code.



  2. Take a look at the structure you are getting back from your query:



    var_dump($ws_current_user);




  3. There are lots of way to extract the data from the returned structure, but the most common approach is something like this:



    foreach ($ws_completed_courses as $course) {
    $completedCourseCount = $course->colum_you_want;
    }




  4. Return the value you extracted as the last line in your function:



    return $completedCourseCount;




  5. Call the function like this:



    echo ws_count_completed_courses();








share|improve this answer


























  • Hi ChuckC, i try with your solution but i receive int(3), i am not sure where is the problem... i also try to test the SQL statement using another statement to count registered users... but the results is the same... My code is updated right now, could you please check the updated section and give me another instructions?

    – Martin M
    Nov 21 '18 at 14:45
















0














There are $wpdb tutorials you could google; but, here's a conceptual overview that might help get you started:




  1. From a SQL prompt, make sure your query works. Then figure out how to adapt the SQL to use program variables in your PHP code.



  2. Take a look at the structure you are getting back from your query:



    var_dump($ws_current_user);




  3. There are lots of way to extract the data from the returned structure, but the most common approach is something like this:



    foreach ($ws_completed_courses as $course) {
    $completedCourseCount = $course->colum_you_want;
    }




  4. Return the value you extracted as the last line in your function:



    return $completedCourseCount;




  5. Call the function like this:



    echo ws_count_completed_courses();








share|improve this answer


























  • Hi ChuckC, i try with your solution but i receive int(3), i am not sure where is the problem... i also try to test the SQL statement using another statement to count registered users... but the results is the same... My code is updated right now, could you please check the updated section and give me another instructions?

    – Martin M
    Nov 21 '18 at 14:45














0












0








0







There are $wpdb tutorials you could google; but, here's a conceptual overview that might help get you started:




  1. From a SQL prompt, make sure your query works. Then figure out how to adapt the SQL to use program variables in your PHP code.



  2. Take a look at the structure you are getting back from your query:



    var_dump($ws_current_user);




  3. There are lots of way to extract the data from the returned structure, but the most common approach is something like this:



    foreach ($ws_completed_courses as $course) {
    $completedCourseCount = $course->colum_you_want;
    }




  4. Return the value you extracted as the last line in your function:



    return $completedCourseCount;




  5. Call the function like this:



    echo ws_count_completed_courses();








share|improve this answer















There are $wpdb tutorials you could google; but, here's a conceptual overview that might help get you started:




  1. From a SQL prompt, make sure your query works. Then figure out how to adapt the SQL to use program variables in your PHP code.



  2. Take a look at the structure you are getting back from your query:



    var_dump($ws_current_user);




  3. There are lots of way to extract the data from the returned structure, but the most common approach is something like this:



    foreach ($ws_completed_courses as $course) {
    $completedCourseCount = $course->colum_you_want;
    }




  4. Return the value you extracted as the last line in your function:



    return $completedCourseCount;




  5. Call the function like this:



    echo ws_count_completed_courses();









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 20 '18 at 22:27

























answered Nov 20 '18 at 22:16









ChuckCChuckC

12




12













  • Hi ChuckC, i try with your solution but i receive int(3), i am not sure where is the problem... i also try to test the SQL statement using another statement to count registered users... but the results is the same... My code is updated right now, could you please check the updated section and give me another instructions?

    – Martin M
    Nov 21 '18 at 14:45



















  • Hi ChuckC, i try with your solution but i receive int(3), i am not sure where is the problem... i also try to test the SQL statement using another statement to count registered users... but the results is the same... My code is updated right now, could you please check the updated section and give me another instructions?

    – Martin M
    Nov 21 '18 at 14:45

















Hi ChuckC, i try with your solution but i receive int(3), i am not sure where is the problem... i also try to test the SQL statement using another statement to count registered users... but the results is the same... My code is updated right now, could you please check the updated section and give me another instructions?

– Martin M
Nov 21 '18 at 14:45





Hi ChuckC, i try with your solution but i receive int(3), i am not sure where is the problem... i also try to test the SQL statement using another statement to count registered users... but the results is the same... My code is updated right now, could you please check the updated section and give me another instructions?

– Martin M
Nov 21 '18 at 14:45













0














$wpdb->get_results returns an array or an object. So try one of these:



echo $ws_completed_courses['COUNT(item_id)'];
echo $ws_completed_courses->COUNT(item_id);


EDIT: Ok, try this then:



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
global $wpdb;

$ws_completed_courses = $wpdb->get_results( "SELECT COUNT(item_id) as user_item_id FROM $wpdb->user_items WHERE status = 'completed' AND user_id = $ws_current_user" );

foreach ($ws_completed_courses as $course) {
$completedCourseCount = $course->user_item_id;
}
return $completedCourseCount;
}





share|improve this answer


























  • They didn't work also.. could you please check the edit on my code? Thanks

    – Martin M
    Nov 21 '18 at 22:23











  • Ok see above, made a small tweak to the SQL query.

    – Richard Zack
    Nov 22 '18 at 4:02











  • Did it work changing to "SELECT COUNT(item_id) as user_item_id"

    – Richard Zack
    Nov 24 '18 at 15:15
















0














$wpdb->get_results returns an array or an object. So try one of these:



echo $ws_completed_courses['COUNT(item_id)'];
echo $ws_completed_courses->COUNT(item_id);


EDIT: Ok, try this then:



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
global $wpdb;

$ws_completed_courses = $wpdb->get_results( "SELECT COUNT(item_id) as user_item_id FROM $wpdb->user_items WHERE status = 'completed' AND user_id = $ws_current_user" );

foreach ($ws_completed_courses as $course) {
$completedCourseCount = $course->user_item_id;
}
return $completedCourseCount;
}





share|improve this answer


























  • They didn't work also.. could you please check the edit on my code? Thanks

    – Martin M
    Nov 21 '18 at 22:23











  • Ok see above, made a small tweak to the SQL query.

    – Richard Zack
    Nov 22 '18 at 4:02











  • Did it work changing to "SELECT COUNT(item_id) as user_item_id"

    – Richard Zack
    Nov 24 '18 at 15:15














0












0








0







$wpdb->get_results returns an array or an object. So try one of these:



echo $ws_completed_courses['COUNT(item_id)'];
echo $ws_completed_courses->COUNT(item_id);


EDIT: Ok, try this then:



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
global $wpdb;

$ws_completed_courses = $wpdb->get_results( "SELECT COUNT(item_id) as user_item_id FROM $wpdb->user_items WHERE status = 'completed' AND user_id = $ws_current_user" );

foreach ($ws_completed_courses as $course) {
$completedCourseCount = $course->user_item_id;
}
return $completedCourseCount;
}





share|improve this answer















$wpdb->get_results returns an array or an object. So try one of these:



echo $ws_completed_courses['COUNT(item_id)'];
echo $ws_completed_courses->COUNT(item_id);


EDIT: Ok, try this then:



function ws_count_completed_courses() {
$ws_current_user = get_current_user_id();
global $wpdb;

$ws_completed_courses = $wpdb->get_results( "SELECT COUNT(item_id) as user_item_id FROM $wpdb->user_items WHERE status = 'completed' AND user_id = $ws_current_user" );

foreach ($ws_completed_courses as $course) {
$completedCourseCount = $course->user_item_id;
}
return $completedCourseCount;
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 3:20

























answered Nov 20 '18 at 22:22









Richard ZackRichard Zack

1668




1668













  • They didn't work also.. could you please check the edit on my code? Thanks

    – Martin M
    Nov 21 '18 at 22:23











  • Ok see above, made a small tweak to the SQL query.

    – Richard Zack
    Nov 22 '18 at 4:02











  • Did it work changing to "SELECT COUNT(item_id) as user_item_id"

    – Richard Zack
    Nov 24 '18 at 15:15



















  • They didn't work also.. could you please check the edit on my code? Thanks

    – Martin M
    Nov 21 '18 at 22:23











  • Ok see above, made a small tweak to the SQL query.

    – Richard Zack
    Nov 22 '18 at 4:02











  • Did it work changing to "SELECT COUNT(item_id) as user_item_id"

    – Richard Zack
    Nov 24 '18 at 15:15

















They didn't work also.. could you please check the edit on my code? Thanks

– Martin M
Nov 21 '18 at 22:23





They didn't work also.. could you please check the edit on my code? Thanks

– Martin M
Nov 21 '18 at 22:23













Ok see above, made a small tweak to the SQL query.

– Richard Zack
Nov 22 '18 at 4:02





Ok see above, made a small tweak to the SQL query.

– Richard Zack
Nov 22 '18 at 4:02













Did it work changing to "SELECT COUNT(item_id) as user_item_id"

– Richard Zack
Nov 24 '18 at 15:15





Did it work changing to "SELECT COUNT(item_id) as user_item_id"

– Richard Zack
Nov 24 '18 at 15:15


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53401268%2fdisplay-data-from-sql-database-in-wordpress%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents