[wp-hackers] Creating / Inserting into Table named after WP Username

Otto otto at ottodestruct.com
Mon Aug 26 18:11:35 UTC 2013


I guess I'm not really understanding the question or the underlying
problem here then. If you have a table called, say, wp_userstuff
(using the table prefix), and it looks like this:

(
  `user_id` bigint(20) NOT NULL,
  `otherstuff` text NOT NULL
)

Then you can select from it like so:

$current_user = wp_get_current_user();
if ( $current_user->exists() ) {
  $results = $wpdb->get_results( $wpdb->prepare("SELECT * FROM
{$wpdb->prefix}userstuff WHERE user_id = %d", $current_user->ID) );
}

To insert into the table, you could do something similar with
$wpdb->query(), but the $wpdb->insert() function is probably better.

$wpdb->insert( $wpdb->prefix.'userstuff', array( 'user_id' =>
$current_user->ID, 'otherstuff' => 'whatever' ) );

But realistically, usermeta is going to be a better choice if this is
something you need to pull out for every user lookup, since usermeta
is pulled automatically. The only reason to make a separate table for
it is if you have special needs. Your mention of storing "history"
would be a valid need, because you could stick a datetime column on it
to store that information as well.

-Otto



On Mon, Aug 26, 2013 at 12:53 PM, Gregory Lancaster
<greglancaster71 at gmail.com> wrote:
> I considered that, but I have no idea how to match the user id owning a
> row- I am trying to figure that out now.  Its actually why I posted here,
> looking for a way to grab the active user ID or login_name and match it to
> a table or row.  I tried INSERT INTO $table and that wont work, nor will
> any variation with quotes or curly brackets.
>
>
> On Mon, Aug 26, 2013 at 9:53 AM, Otto <otto at ottodestruct.com> wrote:
>
>> On Mon, Aug 26, 2013 at 11:46 AM, Gregory Lancaster
>> <greglancaster71 at gmail.com> wrote:
>> > so i still have the same issue- how to steup the sql to create the table
>> so
>> > it matches the current active user.
>>
>> You shouldn't have one table per user. There's no case where that makes
>> sense.
>>
>> Instead, take your existing table structure, and add a user_id column
>> onto it to connect the various rows with a specific user.
>>
>> -Otto
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list