Forums

cant use wpdb functions n my tables (8 posts)

  1. pesahsone
    Member
    Posted 6 months ago #

    i have created new database table with this code:

    <?php
    /*
    Plugin Name: salery
    Plugin URI: http://www.orangecreative.net
    Description: Plugin for displaying products from an OSCommerce shopping cart database
    Author: tsachi
    Version: 1.0
    Author URI: http://www.orangecreative.net
    */
    ?>
    <?php
    global $jal_db_version;
    $jal_db_version = "1.0";

    function jal_install() {
    global $wpdb;
    global $jal_db_version;

    $table_name = $wpdb->prefix . "salery";

    $sql = "CREATE TABLE " . $table_name . " (
    user_login varchar(20) NOT NULL default '',
    dayOfWork date NOT NULL default '0000-00-00',
    beginingOfWork time NOT NULL default '00:00:00',
    endOfWork time NOT NULL default '00:00:00',
    beginingOfBreak time NOT NULL default '00:00:00',
    endOfBreak time NOT NULL default '00:00:00',
    beginingOfBreakTwo time NOT NULL default '00:00:00',
    endOfBreakTwo time NOT NULL default '00:00:00',
    money float NOT NULL default '0'
    );";
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
    add_option("jal_db_version", $jal_db_version);
    }
    function jal_install_data() {
    global $wpdb;
    $name = "tsachi";
    $rows_affected = $wpdb->insert( $table_name, array( 'user_login' => $name) );
    }
    register_activation_hook(__FILE__,'jal_install');
    register_activation_hook(__FILE__,'jal_install_data');
    ?>

    and i cant use wpdb function like adding rows
    or SELECT

    thanks

  2. vtxyzzy
    Member
    Posted 6 months ago #

    I believe that in the function jal_install_data(), the $table_name variable is undefined.

  3. tbradley22
    Member
    Posted 6 months ago #

    yes vtxyzzy is correct. here is the code that works

    function jal_install_data() {
    global $wpdb;
    $welcome_name = "Mr. WordPress";
    $welcome_text = "Congratulations, you just completed the installation!";

    $table_name = $wpdb->prefix . "erw_core";
    $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'name' => $welcome_name, 'text' => $welcome_text ) );
    }

  4. pesahsone
    Member
    Posted 6 months ago #

    thanks ,i changed it. but still the wpdb functions dosent works.

    $mylink = $wpdb->get_row("SELECT * FROM $wpdb->salery WHERE user_salery = 'tsachi'");
    echo $mylink->user_salery;

    when i am using this function on other table its working. its dosent working only on this table(that i have created)

  5. vtxyzzy
    Member
    Posted 6 months ago #

    wpdb does not have your table registered. Try this:

    $my_table = $wpdb->prefix . 'salery';
    $mylink = $wpdb->get_row("SELECT * FROM $my_table WHERE user_salery = 'tsachi'");
  6. pesahsone
    Member
    Posted 6 months ago #

    wow THANKS!
    BUT ihv alredy did that (you can see my code):

    $my_table = $wpdb->prefix . 'salery';

    when i made the table.
    why i need to do it again?
    i dont need to do it with wp tables..

  7. vtxyzzy
    Member
    Posted 6 months ago #

    WP stores all of the built-in table names in $wpdb. Your table name is not stored there.

  8. pesahsone
    Member
    Posted 6 months ago #

    ok, thanks a lot!

Reply

You must log in to post.

About this Topic

Tags