on Basics Tutorials

Making a shoutbox with PHP, MySql and jQuery – Revisited

5 comments
megaphone
Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon

Recommendation to all my readers who have trouble with their hosting not installing PDO: change your hosting.

This is just an update on a previous tutorial which can be found here.

In that particular tutorial I am using PDO class to manipulate the MySql database. Everything else stays the same, except the code in shout.php file.

For all readers that can not use PDO, change the code in shout.php to this:

/*** mysql hostname ***/
$hostname = 'localhost';

/*** mysql username ***/
$username = 'root';

/*** mysql password ***/
$password = '';

$dbname = 'demo';

mysql_connect($hostname, $username, $password);
mysql_select_db($dbname);

if($_POST['name']) {
    $name       = mysql_real_escape_string($_POST['name']);
    $message    = mysql_real_escape_string($_POST['message']);
    
    $sql = "INSERT INTO shoutbox (date_time, name, message)
            VALUES (NOW(), '".$name."', '".$message."')";

    /*** run the sql statement ***/
    if (mysql_query($sql)) {
        populate_shoutbox();
    }
} 


if($_POST['refresh']) {
    populate_shoutbox();
}


function populate_shoutbox() {
    $sql = "select * from shoutbox order by date_time desc limit 10";
    $rez = mysql_query($sql);
    echo '<ul>';
    while ($row = mysql_fetch_array($rez, MYSQL_ASSOC)) {
        echo '<li>';
        echo '<span class="date">'.date("d.m.Y H:i", strtotime($row['date_time'])).'</span>';
        echo '<span class="name">'.$row['name'].'</span>';
        echo '<span class="message">'.$row['message'].'</span>';
        echo '</li>';
    }
    echo '</ul>';
}

Now your shoutbox is working like a charm using mysql functions.


Tweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on RedditShare on StumbleUpon