• 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 6 years, 9 months ago by jscoolen.

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • 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

    Thread Starter jscoolen

    (@jscoolen)

    thank you, let’s see if this will work

    Thread Starter jscoolen

    (@jscoolen)

    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>	

    $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

    (@jscoolen)

    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

    (@jscoolen)

    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 6 years, 9 months ago by jscoolen.
    • This reply was modified 6 years, 9 months ago by jscoolen.
    • This reply was modified 6 years, 9 months ago by jscoolen.
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘add php with database connection’ is closed to new replies.