problem to connect to database
Posted: Mon Mar 13, 2006 12:11 am
feyd | Please use
2. the connection codes using php which i named as connect.php
3. codes to retrieve result. i used the include method to call for the connection to the database.
i hope somebody can help me with the problem. thank you
feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
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 databaseCode: Select all
<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
Code: Select all
<?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.
Code: Select all
<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
feyd | Please use
Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]