1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

A database with testimonials

Discussion in 'PHP & Perl' started by lanbo, Nov 3, 2009.

  1. lanbo

    lanbo Jr. VIP Jr. VIP Premium Member

    Joined:
    Aug 23, 2009
    Messages:
    3,437
    Likes Received:
    595
    Home Page:
    How can I make something that will take a random testimonial from a database and post it on the site?
     
  2. PostWhore

    PostWhore Registered Member

    Joined:
    Jul 22, 2008
    Messages:
    51
    Likes Received:
    2
    Here you go, but can't test it.

    PHP:
    <?php

    $dbhost 
    'localhost';
    $dbuser 'username'
    $dbpass 'password';
    $dbname 'database_name';
    $url 'http://example.com/post.php';

    $conn mysql_connect($dbhost$dbuser$dbpass) or die ('Error connecting to mysql');
    mysql_select_db($dbname);

    $query "SELECT * FROM $dbname";
    $results mysql_query($query);

    $resultsCount mysql_numrows($results);

    if(
    $resultsCount 0){
        
    $randomNumber rand(0$resultsCount-1);
        
    $randomTestimonial mysql_result($results,$randomNumber,'testimonial');
        
        
        
    $ch curl_init();
        
    curl_setopt($chCURLOPT_URL$url);
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
    curl_setopt($chCURLOPT_POSTtrue);
        
        
    $data = array(
            
    'testimonial' => $randomTestimonial,
            
    'something_else' => 'some_other_post_field'
        
    );

        
    curl_setopt($chCURLOPT_POSTFIELDS$data);
        
    $output curl_exec($ch);
        
    curl_close($ch);
        
        echo 
    $output;

        
        
    }else{
        echo 
    "DID NOT FIND ANYTHING";
    }



    ?>