• I need some help with wpdb again. I have created a new database for members and am trying to extract certain values from that database. I can successfully access the database and retrieve some values, but the following code does not seem to work. The first echo for the number of rows works correctly as does the 2 echos for the user email.
    The line that is extracting the member_id from the new table does not return any value. The SQL query string was copied directly from phpmyadmin where it correctly returns the value. I did edit the email address for this posting.
    The fourth echo extracting the user_email from the _users database with a similar command works correctly. What have I missed?

    Thanks for any suggestions.

    Also is it possible to enter the SQL query directly without using wpdb?

    ` global $wpdb;

    $table_name = $wpdb->prefix . “club_members”;
    $members = $wpdb->get_var( “SELECT COUNT(*) FROM $table_name” );
    echo “<p>The number of rows in the member table is {$members}</p>”;

    /* Determine the logged in member from email address*/
    global $current_user;
    get_currentuserinfo();
    echo “<p>’User email: $current_user->user_email'</p>”;

    $member_email = $current_user->user_email;
    echo “<p>The member email is: {$member_email}</p>”;

    /* Find member_id (row number) in club_members database from email address*/
    $tmp = $wpdb->get_var( “SELECT member_id FROM $table_name WHERE member_email=\’xxxxx@gmail.com\ ” );
    echo “<p>Member ID is {$tmp}</p>”;

    $email = $wpdb->get_var(“SELECT user_email FROM wp_users WHERE user_login = ‘guest’ “);

    // Echo the user’s email address
    echo “<p>Guest email is {$email}</p>”;

Viewing 9 replies - 1 through 9 (of 9 total)
  • I’m not sure how to get that code to display properly.

    Let me try this:

    	global $wpdb;
    	
    	$table_name = $wpdb->prefix . "club_members";
    	$members = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
    	echo "<p>The number of rows in the member table is {$members}</p>";
    	
    /* Determine the logged in member from email address*/
    	global $current_user;
        get_currentuserinfo();
    	echo "<p>'User email: $current_user->user_email'</p>";
    
    	$member_email = $current_user->user_email;
    	echo "<p>The member email is:  {$member_email}</p>";
    
    /* Find member_id (row number) in club_members database from email address*/
    	$tmp = $wpdb->get_var( "SELECT member_id FROM $table_name WHERE member_email=\'xxxxx@gmail.com\ " );
    	echo "<p>Member ID is {$tmp}</p>";
    	
    	$email = $wpdb->get_var("SELECT user_email FROM wp_users WHERE user_login = 'guest' ");
    
       // Echo the user's email address
       echo "<p>Guest email is {$email}</p>";
    	
    

    Or this:

    global $wpdb;

    $table_name = $wpdb->prefix . "club_members";
    $members = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
    echo "<p>The number of rows in the member table is {$members}</p>";

    /* Determine the logged in member from email address*/
    global $current_user;
    get_currentuserinfo();
    echo "<p>'User email: $current_user->user_email'</p>";

    $member_email = $current_user->user_email;
    echo "<p>The member email is: {$member_email}</p>";

    /* Find member_id (row number) in club_members database from email address*/
    $tmp = $wpdb->get_var( "SELECT member_id FROM $table_name WHERE member_email=\'xxxxx@gmail.com\ " );
    echo "<p>Member ID is {$tmp}</p>";

    $email = $wpdb->get_var("SELECT user_email FROM wp_users WHERE user_login = 'guest' ");

    // Echo the user's email address
    echo "<p>Guest email is {$email}</p>";

    I believe I found my error. I now have it working.

    Can anyone explain how to enter code properly in a forum post? I tried clicking on the code button in the text box, and entering < pre > or < code > before the post, none of them seemed to display the pasted code properly. < pre > seemed to work the best (without spaces)

    Thanks

    • This reply was modified 1 week, 1 day ago by  gshell.
    • This reply was modified 1 week, 1 day ago by  gshell.
    • This reply was modified 1 week, 1 day ago by  gshell.
    Moderator Steve Stern

    (@sterndata)

    Support Volunteer

    Use backticks or the code button (which does the same thing). Sometimes the forum gets picky if the opening backtick has leading blanks.

    I’ll try the code button without spaces

    <tr>
    		<th>First Name</th>
    		<th>Last Name</th> 
    		<th>Member Email</th> 
    		<th>Home Phone</th> 
    		<th>Cell Phone</th> 
    	  </tr>
    • This reply was modified 1 week, 1 day ago by  gshell.

    certainly not as clear as what others post. I’ll try some more
    ` global $wpdb;

    $table_name = $wpdb->prefix . “club_members”;
    $members = $wpdb->get_var( “SELECT COUNT(*) FROM $table_name” );
    echo “<p>The number of rows in the member table is {$members}</p>”;

    /*$field = $wpdb->get_var( “SELECT * FROM $table_name”, 1, 0); */
    /*return “<p>Field 1,0 in the member table is {$field}</p>”; */

    $first_row = 0;
    $last_row = $members – 1;
    /* echo “<p>Last row number in the member table is {$last_row}</p>”; */

    Moderator Steve Stern

    (@sterndata)

    Support Volunteer

    You missed the closing backtick. Select the stuff you want to code, then click the code button. The editor here is not particularly sophisticated about such things.

    OK, I think I got it.

    		<th>Home Phone</th> 
    		<th>Cell Phone</th> 
    	  </tr>
    <?php for ($cur_row = 0; $cur_row <= $last_row; $cur_row++) { ?>
    			  <tr>
    				<td><?php echo $wpdb->get_var( "SELECT * FROM $table_name ORDER BY $table_name.<code>last_name</code> ASC ", 1, $cur_row); ?></td>
    				<td><?php echo $wpdb->get_var( "SELECT * FROM $table_name ORDER BY $table_name.<code>last_name</code> ASC ", 2, $cur_row); ?></td>
    				<td><?php echo make_clickable($wpdb->get_var( "SELECT * FROM $table_name ORDER BY $table_name.<code>last_name</code> ASC ", 3, $cur_row)); ?></td>
    				<td><?php echo $wpdb->get_var( "SELECT * FROM $table_name ORDER BY $table_name.<code>last_name</code> ASC ", 4, $cur_row); ?></td>
    Moderator Steve Stern

    (@sterndata)

    Support Volunteer

    By George! I think he’s got it!

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.