Title: add php with database connection
Last modified: March 1, 2018

---

# add php with database connection

 *  [jscoolen](https://wordpress.org/support/users/jscoolen/)
 * (@jscoolen)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/add-php-with-database-connection/)
 * I have made a php script with a database connection and I want to add this to
   a page.
    But for some reason the script stops after: <div class=”gal”>
 * Does anyone know what I am doing wrong?
 * This is the script I made
 *     ```
       $connection2 = new wpdb( "jcsl123456", "123456", "table", "localhost" );
   
       ?>
   
       <div class="klanten_gallery">
       	<div class="container">
       		<div class="klanten_gallery_inn">
               	<div class="klanten_gallery_header">
               	<!--h2>Onze klanten</h2-->
                   <h2><?php the_title(); ?></h2>
                   	<div class="border_photo_klanten"></div>
                   </div>
       			<div class="klanten_gallery_text">
                       <?php
                           if ( have_posts() ) : while ( have_posts() ) : the_post(); 
                               the_content(); 
                           endwhile; endif;
                       ?>
                   </div>
       			<div class="gal">
                       <?php
       $main ='';
       $qry ='
       	SELECT 
       		kl_logos.id,
       		kl_logos.klnr,
       		kl_logos.image,
       		kl_overzicht.kl_overzicht_naam
       	FROM 
       		kl_logos
       	INNER JOIN
       		kl_overzicht
       	ON
       		kl_logos.klnr = kl_overzicht.kl_overzicht_klnr
       	ORDER BY 
       		RAND()
       	LIMIT
       		18';
       if(!$result = $connection2->query($qry)) {
           echo 'Fout in query: '.$connection2->error;
       }else while($list = $result->fetch_assoc()){					
       $main .='			
       			<a class="fancy6" href="#" title="'.$list['kl_overzicht_naam'].'">
       				<div class="klanten_image">
       				<img src="'.$urlfoto.$list['image'].'" alt="'.$list['kl_overzicht_naam'].'"/>
   
       				</div>
       			</a>';
       		}
       		echo $main;
                   ?>			
       			 </div>
   
       		</div>
       	</div>
       </div>	
       ```
   
    -  This topic was modified 8 years, 3 months ago by [jscoolen](https://wordpress.org/support/users/jscoolen/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fadd-php-with-database-connection%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [wpaart](https://wordpress.org/support/users/wpaart/)
 * (@wpaart)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/add-php-with-database-connection/#post-10032192)
 * When connecting to the database of your WordPress site it is advisable to use
   the $wpdb functionality, it helps you with security and makes your code easier
   to debug.
 * The developer handbook provides a good introduction: [Talking To The Database](https://codex.wordpress.org/Class_Reference/wpdb)
 *  Thread Starter [jscoolen](https://wordpress.org/support/users/jscoolen/)
 * (@jscoolen)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/add-php-with-database-connection/#post-10033101)
 * thank you, let’s see if this will work
 *  Thread Starter [jscoolen](https://wordpress.org/support/users/jscoolen/)
 * (@jscoolen)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/add-php-with-database-connection/#post-10038011)
 * SO i have changed the connection, but still the scripts stops after <div class
   =”gal”>
 *     ```
       $mydb = new wpdb('user','123456','database','localhost');
   
       $url = 'https://www.********/clean/';
        $urlfoto = $url.'images/logos/';
   
       	?>
       <div class="klanten_gallery">
       	<div class="container">
       		<div class="klanten_gallery_inn">
               	<div class="klanten_gallery_header">
               	<!--h2>Onze klanten</h2-->
                   <h2><?php the_title(); ?></h2>
                   	<div class="border_photo_klanten"></div>
                   </div>
       			<div class="klanten_gallery_text">
                       <?php
                           if ( have_posts() ) : while ( have_posts() ) : the_post(); 
                               the_content(); 
                           endwhile; endif;
                       ?>
                   </div>
       			<div class="gal">
                   <?php	
       $qry ='
       	SELECT 
       		kl_logos.id,
       		kl_logos.klnr,
       		kl_logos.image,
       		kl_overzicht.kl_overzicht_naam
       	FROM 
       		kl_logos
       	INNER JOIN
       		kl_overzicht
       	ON
       		kl_logos.klnr = kl_overzicht.kl_overzicht_klnr
       	ORDER BY 
       		RAND()
       	LIMIT
       		18';
       if(!$result = $mydb ->query($qry)) {
       	echo ' Error in query: '. $mydb->error; 
       }else while($list = $result->fetch_assoc()){
       	echo'					
   
       			<a class="fancy6" href="#" title="'.$list['kl_overzicht_naam'].'">
       				<div class="klanten_image">
       				<img src="'.$urlfoto.$list['image'].'" alt="'.$list['kl_overzicht_naam'].'"/>
   
       				</div>
       			</a>';
       			}		
       ?>		
       			 </div>
   
       		</div>
       	</div>
       </div>	
       ```
   
 *  [mxsmoku](https://wordpress.org/support/users/mxsmoku/)
 * (@mxsmoku)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/add-php-with-database-connection/#post-10038283)
 * `$result->fetch_assoc()` probably generate fatal error because $mydb->query($
   qry) returns only int or false.
 * Try this:
 *     ```
       $results = $mydb->get_results($qry, 'ARRAY_A');
   
       foerach ($results as $list) {
       ...
       }
       ```
   
 * For security reasons you should use **prepare** function to generate safe $qry(
   $mydb->prepare).
 *  Thread Starter [jscoolen](https://wordpress.org/support/users/jscoolen/)
 * (@jscoolen)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/add-php-with-database-connection/#post-10038340)
 * I just tried that, but now my whole page is white.
 *     ```
       <div class="klanten_gallery">
       	<div class="container">
       		<div class="klanten_gallery_inn">
               	<div class="klanten_gallery_header">
               	<!--h2>Onze klanten</h2-->
                   <h2><?php the_title(); ?></h2>
                   	<div class="border_photo_klanten"></div>
                   </div>
       			<div class="klanten_gallery_text">
                       <?php
                           if ( have_posts() ) : while ( have_posts() ) : the_post(); 
                               the_content(); 
                           endwhile; endif;
                       ?>
                   </div>
       			<div class="gal">
                   <?php	
       $qry ='
       	SELECT 
       		kl_logos.id,
       		kl_logos.klnr,
       		kl_logos.image,
       		kl_overzicht.kl_overzicht_naam
       	FROM 
       		kl_logos
       	INNER JOIN
       		kl_overzicht
       	ON
       		kl_logos.klnr = kl_overzicht.kl_overzicht_klnr
       	ORDER BY 
       		RAND()
       	LIMIT
       		18';
       if(!$results = $mydb ->prepare($qry)) {
       	echo ' Error in query: '. $mydb->error; 
       }else while(
   
       $results = $mydb->get_results($qry, 'ARRAY_A');
   
       foerach ($results as $list) {
       	echo'					
   
       			<a class="fancy6" href="#" title="'.$list['kl_overzicht_naam'].'">
       				<div class="klanten_image">
       				<img src="'.$urlfoto.$list['image'].'" alt="'.$list['kl_overzicht_naam'].'"/>
   
       				</div>
       			</a>';
       			}		
       ?>		
       			 </div>
   
       		</div>
       	</div>
       </div>	
       ```
   
 *  Thread Starter [jscoolen](https://wordpress.org/support/users/jscoolen/)
 * (@jscoolen)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/add-php-with-database-connection/#post-10038343)
 * never mind I think you mean foreach.
 * edit 1: but still the page is all white
 * edit 2: now it works
 *     ```
                   <?php	
       $qry ='
       	SELECT 
       		kl_logos.id,
       		kl_logos.klnr,
       		kl_logos.image,
       		kl_overzicht.kl_overzicht_naam
       	FROM 
       		kl_logos
       	INNER JOIN
       		kl_overzicht
       	ON
       		kl_logos.klnr = kl_overzicht.kl_overzicht_klnr
       	ORDER BY 
       		RAND()
       	LIMIT
       		18';
       if(!$results = $mydb ->prepare($qry)) {
       	echo ' Error in query: '. $mydb->error; 
       }else{
   
       $results = $mydb->get_results($qry, 'ARRAY_A');
   
       foreach ($results as $list) {
       	echo'					
       			<a class="fancy6" href="#" title="'.$list['kl_overzicht_naam'].'">
       				<div class="klanten_image">
       				<img src="'.$urlfoto.$list['image'].'" alt="'.$list['kl_overzicht_naam'].'"/>
   
       				</div>
       			</a>';
       			}	
       }			
       ?>	
       ```
   
 * edit 3: Thank you very much!!!
    -  This reply was modified 8 years, 2 months ago by [jscoolen](https://wordpress.org/support/users/jscoolen/).
    -  This reply was modified 8 years, 2 months ago by [jscoolen](https://wordpress.org/support/users/jscoolen/).
    -  This reply was modified 8 years, 2 months ago by [jscoolen](https://wordpress.org/support/users/jscoolen/).

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘add php with database connection’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 3 participants
 * Last reply from: [jscoolen](https://wordpress.org/support/users/jscoolen/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/add-php-with-database-connection/#post-10038343)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
