Forum Replies Created

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter nancy56

    (@nancy56)

    Thanks alot

    Thread Starter nancy56

    (@nancy56)

    Thanks alot

    Thread Starter nancy56

    (@nancy56)

    Thanks

    @rssail are you sure you tested the code i posted above. Recall that all wordpress table must start with wp_ so if your table name is nancy. it should be created as wp_nancy. I have included a a table creation and data insertion for testing purpose. The two example below works perfect as I have tested it myself okay.

    Table creation and Insertion for testing purpose

    
    create table wp_J1D_options(id int primary key auto_increment,option_id int(11),option_name varchar(30));
    
    insert into wp_J1D_options(option_id,option_name) values (100,'nancybaby1');
    insert into wp_J1D_options(option_id,option_name) values (200,'nancybaby2');

    Method 1

    global $wpdb;
    $tablename="J1D_options";
    $tablename_all = $wpdb->prefix.$tablename;
    
    	$list = $wpdb->get_results("SELECT * FROM $tablename_all");
    	if(count($list) > 0){
    		$count = 1;
    		foreach($list as $row){
    		    $op_id = $row->option_id;
    		    $op_name = $row->option_name;
    		  
    		    echo "
    		    	<div>".$count."</div>
    		    	<div>".$op_id."</div>
    		    	<div>".$op_name."</div>		   
    		    ";
    		    $count++;
    		}
    	}else{
    		echo "No record found";
    	}
    

    Method 2 as you requested

    
    global $wpdb;
    $tablename="J1D_options";
    $tablename_all = $wpdb->prefix.$tablename;
    $result = $wpdb->get_results("SELECT * FROM $tablename_all");
    	
    		$results = [];
    		foreach($result as $row){
    		    $results[] = $row;
    		
    		}
    
    	print_r($results);
    
    	if($results){
    echo "data found";
    }else{
    
    echo "no data found. could not connect to database";
    }
    

    I guess your are trying to create a plugin.

    Make sure you are running this code from wordpress and the table above must be created within your wordpress database. it works 100%. From your comment, you said that the documentation is poor: who told you that. You will need to cool down and read it so that you can understand it. WordPress Database documentation is the easiest seen on the internet. please enjoy and give me a shout as it will work for uuuuu…… Remember that you will also need to ready wordpress documentation on how to escape html characters from data returns from database call to ensure that Html Injection and XSS Attack is not possible

    • This reply was modified 5 years, 5 months ago by nancy56.
    • This reply was modified 5 years, 5 months ago by nancy56.

    please be-careful of what you are doing and not combining msqli connection with wordpress global connection. WordPress has its own venerable way of handling database connections and sanitization which ensures that sql injection attack is eliminated… using mysqli, you have to do security coding on your own. Thanks

    Like @bcworkz already stated above. Global $wpdb makes its own database connections. so please Try something like this code below and get back to me

    
    <?php 
    
    global $wpdb;
    $tablename="J1D_options";
    $tablename_all = $wpdb->prefix.$tablename;
    
    	$list = $wpdb->get_results("SELECT * FROM $tablename_all");
    	if(count($list) > 0){
    		$count = 1;
    		foreach($list as $row){
    		    $op_id = $row->option_id;
    		    $op_name = $row->option_name;
    		  
    		    echo "
    		    	<div>".$count."</div>
    		    	<div>".$op_id."</div>
    		    	<div>".$op_name."</div>		   
    		    ";
    		    $count++;
    		}
    	}else{
    		echo "No record found";
    	}
    	
    
    	?>
Viewing 6 replies - 1 through 6 (of 6 total)