MySql Connection

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tqmd1
Forum Newbie
Posts: 7
Joined: Sat Nov 09, 2013 1:07 am

MySql Connection

Post 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
vamsisnikky
Forum Newbie
Posts: 1
Joined: Wed Nov 13, 2013 12:05 am

Re: MySql Connection

Post 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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: MySql Connection

Post 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
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply