Jump to content

Recommended Posts

i have a problem in connecting to the database. i created the database using phpMyAdmin and using Macromedia Dreamweaver as the platform for my codes. when i view my interface in the browser which request information from my database, a dialog box appears asking whether to open, save or cancel the php file that is related to the database and do not return the result which is suppose to come from the database. the database contains login information that contains two field which are the username and password

these are the codes which i used:

1. sample html file to retrieve info from the database
Code:
[code]<html>
<head>
<title>Sample</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body style = "background-color: #F0E86C">
   <h2 style = "font-family: arial color: blue" >
   Querying a mysql dbase</h2>
    
   <form method = "post" action = "try.php">
   <p>Select a field to display:
    
   <!--add a select box containing options-->
    
   <select name = "select">
      <option selected = "selected">* </option>
      <option> username</option>
      <option>password</option>
   </select>
   </p>
   <input type = "submit" value = "Send Query"
      style = "background-color: blue;
      color:yellow; font-weight: bold" />
    
   </form>    
</body>
</html> [/code]

2. the connection codes using php which i named as connect.php

PHP:
[code]<?php
<!-- DATABASE CONNECTION INFO---->
$hostname="localhost";  
$mysql_login="root";  
$mysql_password="";  
$database="myfyp";
&dbtype="MySQL";  

// connect to the database server  
if (!($db = mysql_connect($hostname, $mysql_login , $mysql_password))){  
  die("Can't connect to database server.");      
}else{  
  // select a database  
    if (!(mysql_select_db("$database",$db))){  
      die("Can't connect to database.");  
    }  
}  
?>  [/code]


3. codes to retrieve result. i used the include method to call for the connection to the database.

PHP:
[code]<html>
<head>
<title>try</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body style = "font-family: arial, sans-serif"  
    style = " background-color : #FOE68C " >
    
<?php
include("includes/connect.php");  
    
    extract($_POST);
    //check if user has left username or password field blank
    if( !$username || !$password ) {
        fieldsBlank();
        die();
    }
<?php  

extract ($_POST);
//$var = @$_POST['select'];

//build SELECT query
$query = "SELECT" . $select . " FROM logininfo ";

//connect to MySQL
if (!( $database = mysql_connect( "localhost", "root", "")))
    die ("Could not connect to database");

//open myfyp database
if (!mysql_select_db( "logininfo", $database))
    die ("Could not open myfyp database");
    
//query myfyp database
if (! ( $result = mysql_query ($query, $database))) {
    print ("Could not execute query! <br/> ");
    die( mysql_error() );
}
?>

<h3 style = "color : blue">  
Search Result</h3>

<table border = "1" cellpadding = "3" cellspacing = "2"
    style = "background-color: #ADD8E6" >
    
<?php
    
    // fetch each record in result set
    for ( $counter = 0;
        $row = mysql_fetch_row ( $result);
        $counter++ ) {
        
        //build table to display results
        print( "<tr>");
        
        foreach ( $row as $key => $value )
            print ("<td> $value </td>" );
            
            print ("</tr>");
        }
        
        mysql_close ( $database );
    ?>
    
    </table>
    
    <br /> Your search: <strong>
    <?php print ("$counter") ?> results. <br/> <br/></strong>
    
    

</body>
</html>   [/code]

i hope somebody can help me with the problem. thank you
Link to comment
https://forums.phpfreaks.com/topic/4814-problem-in-connecting-to-database/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.