• I tried to use this script but it doesn’t appear to be working. Could someone provide a simple working example of how to connect to a database, retrieve data and print it out?

    The script prints out Database connection working but it doesn’t print out the data and after the connection is closed doesn’t print out that the database is closed

    <?php

    $local = ‘xxxxxxx’;
    $user = ‘xxxxxxx’;
    $pass = ‘xxxxxxx’;
    $data = ‘xxxxxxx’;

    $testConnection = mysqli_connect($local,$user,$pass, $data);

    if (!$testConnection) {
    die(‘Error: ‘ . mysqli_connect_errno() . PHP_EOL);
    }
    echo ‘Database connection working!’;

    global $wpdb;
    $result = $wpdb->get_results (“SELECT * FROM $wpdb->_J1D_options” );

    foreach ( $result as $row ){
    echo $row->option_id .'<br/>’;
    echo $row->option_name .'<br/>’;
    }

    mysqli_close($testConnection);

    if (!$testConnection) {
    echo “database closed”:
    }

    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Global $wpdb makes its own connection. Your $testConnection connection is not used.

    _J1D_options is not a valid table name property for the $wpdb object. Maybe you want "SELECT * FROM {$wpdb->prefix}_J1D_options"?

    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";
    	}
    	
    
    	?>
    Thread Starter rssail

    (@rssail)

    _J1D_ is in fact a proper wp database name. Also, the problem was solved using mysqli_query

    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

    Thread Starter rssail

    (@rssail)

    Thanks, I tried using $wpdb but the documentation is really poor. Do you have an example of how to connect to a database and query data into an array? I’m using the following and it seems to work.

    require( ‘wp-load.php’ );
    $local =’xxx’;
    $user =’xxx’;
    $pass =’xxx’;
    $data =’xxx’;

    $testConnection = mysqli_connect($local,$user,$pass, $data);

    if (!$testConnection) {
    die(‘Error: ‘ . mysqli_connect_errno() . PHP_EOL);
    }
    echo ‘Database connection working!’;

    $result = mysqli_query($testConnection, “SELECT * FROM _J1D_options”);

    $results = [];
    while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
    {
    $results[] = $row;
    }

    //$results has all that you need
    print_r($results);

    mysqli_free_result($result);

    $testClosed = mysqli_close($testConnection);

    if ($testClosed) {
    echo “closed”;
    }

    ?>

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

    @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.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Unable to connect and print data from database’ is closed to new replies.