Page 1 of 1

MySql Connection

Posted: Sat Nov 09, 2013 1:15 am
by tqmd1
Dear Sir,

HTML file has following codes

Code: Select all

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Connection Checking</title>
    </head>
    <body>
    <center>
    <h1>Connection Checker</h1>
    <fieldset style=width:300px;background-color:#E0FFFF>
    <legend sytle=font-size:12;>Click Connect</legend>
    <form name="form1" action="connect.php" method="post">
    <table>
    <tr><td></td><td align="center"><input type="submit" name="button1" value="Connect">
    </td></tr>
    </table>
    </form>
    </fieldset>
    </center>
    </body>
    </html>
and Connect.php has following codes

Code: Select all

<?php
    // Connection variables
    $host="localhost";
    $username="root";
    $password="";
    $db_name="asia"; 
    
    // Connect to database
    $con=mysqli_connect("$host", "$username", "$password");
    
    // Connect result
    if(!$con){
    die('Error Connecting to Database: ' . mysqli_error());
    }
    else
    {
    echo "Connected" . mysqli_error($con);
    }
    //close the connection
    //mysql_close($con);
    ?>
Both files have no problems and works fine.

But I need following modifications:

When connection is established then this line trigs from connect.php

echo "Connected" . mysqli_error($con);

While standing on html page, I want to show a messagebox like this

http://i43.tinypic.com/23vc5k5.jpg

Please help

Re: MySql Connection

Posted: Wed Nov 13, 2013 12:21 am
by vamsisnikky
U just add this code to connect.php

Code: Select all

if(!$con){
    die('Error Connecting to Database: ' . mysqli_error());
    }
    else
    {
     echo '<script> alert("Connected !!!")</script>'. mysqli_error($con);
    }
Then it will gives u Output like that

Re: MySql Connection

Posted: Tue Nov 19, 2013 3:53 pm
by social_experiment
just a note about using mysqli_error() willy-nilly :idea: it can reveal sensitive information about the connection error; good to use when developing, bad to use when your code is on a live server