Retrieving value from wordpress database in javascript

So I created a table in my database called inspirationWall with id and Visit_Count INT.
and added the row with id=1 and Visit_Count = 2.

Im trying to retrieve the value in Visit_Count in wordpress and display the value in an alert just for testing.

Read More

Ive been following the word press codex and used this example

<?php $user_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->users;" ) ); echo "<p>User count is {$user_count}</p>";?>

So by looking at that i made my code

var $inspirationWall_Count = 0;

$inspirationWall_Count = $wpdb->get_var( $wpdb->prepare( "SELECT Visit_Count FROM $wpdb-    >inspirationWall WHERE id=1;" ) );

alert($inspirationWall_Count);

It does not seem to retrieve the value and save it into $inspirationWall_Count.

What am I doing wrong?

Thanks

Related posts

Leave a Reply

1 comment

  1. You can’t make an alert with php you have to do it with JS,

    also you don’t need to define again the var $inspirationWall_Count = 0; as its not working with php,

    So here what you have to do,

    $inspirationWall_Count = 0;
    $inspirationWall_Count = $wpdb->get_var( $wpdb->prepare( "SELECT Visit_Count FROM $wpdb-    >inspirationWall WHERE id=1;" ) );
    echo '<script>alert('.$inspirationWall_Count.')</script>';