Jump to content


Photo

problem in connecting to database


  • Please log in to reply
1 reply to this topic

#1 thania

thania
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 March 2006 - 08:35 AM

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:
<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>

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

PHP:
<?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.");
}
}
?>



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

PHP:
<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>



i hope somebody can help me with the problem. thank you

#2 moberemk

moberemk
  • Members
  • PipPipPip
  • Advanced Member
  • 695 posts

Posted 13 March 2006 - 12:23 PM

Create a dreamweaver connection to connect to the database. Also, don't post the same thread twice.
Disclaimer: No matter how harsh I may be, I'm always IMO only.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users