Jump to content

PHP is not connecting to mysql database?


usman07

Recommended Posts

For some reason Im not able to connect to mysql database, when i fill in the form and select search, it just basically refreshes the page but does not come up with no error messages or any results from my database. any help is appreciated.

 

HTML Code

 


<table id="tb1">
<tr>
<td><p class="LOC">Location:</p></td>
<td><div id="LC">
<form action="insert.php" method="post">
<select multiple="multiple" size="5" style="width: 150px;" >
<option>Armley</option>
<option>Chapel Allerton</option>
<option>Harehills</option>
<option>Headingley</option>
<option>Hyde Park</option>
<option>Moortown</option>
<option>Roundhay</option>
</select>
</form>
</div>
</td>

	<td><p class="PT">Property type:</p></td>
<td><div id="PS">
	<form action="insert.php" method="post">
	<select name="property type" style="width: 170px;">
	<option value="none" selected="selected">Any</option>
	<option value="Houses">Houses</option>
	<option value="Flats / Apartments">Flats / Apartments</option>
	</select>
		</form>
	</div>
</td><td>
<div id="ptype">
<form action="insert.php" method="post"> 
<input type="radio" class="styled" name="ptype" value="forsale"/> For Sale 

<p class="increase">
<input type="radio" class="styled" name="ptype" value="forrent"/> To Rent
</p>
<p class="increase">
<input type="radio" class="styled" name="ptype" value="any"/> Any
</p>
</form>
</div>
</td>
</tr>

	</table>

	<div id="table2">
	<table id="NBtable">
	<tr>
	<td><p class="NBS">Number of bedrooms:</p></td>
	<td><div id="NB">
	<form action="insert.php" method="post">
	<select name="number of bedrooms">
	<option value="none" selected="selected">No Min</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	</select> to

	<select name="number of bedrooms">
	<option value="none" selected="selected">No Max</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	</select>
</form>		
	</div>
</td>

	<td><p class="PR">Price range:</p></td>
	<td><div id="PR">
	<form action="insert.php" method="post">
	<select name="price range">
	<option value="none" selected="selected">No Min</option>
	<option value="50,000">50,000</option>
	<option value="60,000">60,000</option>
	<option value="70,000">70,000</option>
	<option value="80,000">80,000</option>
	<option value="90,000">90,000</option>
	<option value="100,000">100,000</option>
	<option value="110,000">110,000</option>
	<option value="120,000">120,000</option>
	<option value="130,000">130,000</option>
	<option value="140,000">140,000</option>
	<option value="150,000">150,000</option>
	<option value="160,000">160,000</option>
	<option value="170,000">170,000</option>
	<option value="180,000">180,000</option>
	<option value="190,000">190,000</option>
	<option value="200,000">200,000</option>
	<option value="210,000">210,000</option>
	<option value="220,000">220,000</option>
	<option value="230,000">230,000</option>
	<option value="240,000">240,000</option>
	<option value="250,000">250,000</option>
	<option value="260,000">260,000</option>
	<option value="270,000">270,000</option>
	<option value="280,000">280,000</option>
	<option value="290,000">290,000</option>
	<option value="300,000">300,000</option>
	<option value="310,000">310,000</option>
	<option value="320,000">320,000</option>
	<option value="330,000">330,000</option>
	<option value="340,000">340,000</option>
	<option value="350,000">350,000</option>
	</select> to

	<select name="price range">
	<option value="none" selected="selected">No Max</option>
	<option value="50,000">50,000</option>
	<option value="60,000">60,000</option>
	<option value="70,000">70,000</option>
	<option value="80,000">80,000</option>
	<option value="90,000">90,000</option>
	<option value="100,000">100,000</option>
	<option value="110,000">110,000</option>
	<option value="120,000">120,000</option>
	<option value="130,000">130,000</option>
	<option value="140,000">140,000</option>
	<option value="150,000">150,000</option>
	<option value="160,000">160,000</option>
	<option value="170,000">170,000</option>
	<option value="180,000">180,000</option>
	<option value="190,000">190,000</option>
	<option value="200,000">200,000</option>
	<option value="210,000">210,000</option>
	<option value="220,000">220,000</option>
	<option value="230,000">230,000</option>
	<option value="240,000">240,000</option>
	<option value="250,000">250,000</option>
	<option value="260,000">260,000</option>
	<option value="270,000">270,000</option>
	<option value="280,000">280,000</option>
	<option value="290,000">290,000</option>
	<option value="300,000">300,000</option>
	<option value="310,000">310,000</option>
	<option value="320,000">320,000</option>
	<option value="330,000">330,000</option>
	<option value="340,000">340,000</option>
	<option value="350,000">350,000</option>

	</select>
</form>		
	</div>
</td>


</tr>

	</table>	

 

PHP Code


<?php
$server = "";      // Enter your MYSQL server name/address between quotes
$username = "";    // Your MYSQL username between quotes
$password = "";    // Your MYSQL password between quotes
$database = "";    // Your MYSQL database between quotes

$con = mysql_connect($server, $username, $password);       // Connect to the database
if(!$con) { die('Could not connect: ' . mysql_error()); }  // If connection failed, stop and display error

mysql_select_db($database, $con);  // Select database to use

$result = mysql_query("SELECT * FROM tablename");  // Query database

while($row = mysql_fetch_array($result)) {     // Loop through results
  echo "<b>" . $row['id'] . "</b><br>\n";      // Where 'id' is the column/field title in the database
  echo $row['location'] . "<br>\n";            // Where 'location' is the column/field title in the database
  echo $row['property_type'] . "<br>\n";       // as above
  echo $row['number_of_bedrooms'] . "<br>\n";  // ..
  echo $row['purchase_type'] . "<br>\n";       // ..
  echo $row['price_range'] . "<br>\n";         // ..
}

mysql_close($con);  // Close the connection to the database after results, not before.
?>

 

To connect to my database I an entering mysql database details just at the first top lines server,username,password and database. is thats correct?

 

Thank You for your help in adavance.

Link to comment
Share on other sites

It looks like it's connecting to the database or you would see your error messages. You are however assuming that your query succeeded without checking it. Try:

 

if ($result = mysql_query("SELECT * FROM tablename")) {  // Query database
    if (mysql_num_rows($result)) {
        while($row = mysql_fetch_array($result)) {     // Loop through results
            echo "<b>" . $row['id'] . "</b><br>\n";      // Where 'id' is the column/field title in the database
            echo $row['location'] . "<br>\n";            // Where 'location' is the column/field title in the database
            echo $row['property_type'] . "<br>\n";       // as above
            echo $row['number_of_bedrooms'] . "<br>\n";  // ..
            echo $row['purchase_type'] . "<br>\n";       // ..
            echo $row['price_range'] . "<br>\n";         // ..
        }
    } else {
        // no records found by query.
        echo "No records found";
    }
} else {
    // query failed, handle error
    trigger_error(mysql_error());
}

 

 

Link to comment
Share on other sites

PHP Code

 


<?php
$server = "";      // Enter your MYSQL server name/address between quotes
$username = "";    // Your MYSQL username between quotes
$password = "";    // Your MYSQL password between quotes
$database = "";    // Your MYSQL database between quotes

$con = mysql_connect($server, $username, $password);       // Connect to the database
if(!$con) { die('Could not connect: ' . mysql_error()); }  // If connection failed, stop and display error

mysql_select_db($database, $con);  // Select database to use

if ($result = mysql_query("SELECT * FROM tablename")) {  // Query database
    if (mysql_num_rows($result)) {
        while($row = mysql_fetch_array($result)) {     // Loop through results
            echo "<b>" . $row['id'] . "</b><br>\n";      // Where 'id' is the column/field title in the database
            echo $row['location'] . "<br>\n";            // Where 'location' is the column/field title in the database
            echo $row['property_type'] . "<br>\n";       // as above
            echo $row['number_of_bedrooms'] . "<br>\n";  // ..
            echo $row['purchase_type'] . "<br>\n";       // ..
            echo $row['price_range'] . "<br>\n";         // ..
        }
    } else {
        // no records found by query.
        echo "No records found";
    }
} else {
    // query failed, handle error
    trigger_error(mysql_error());
}

mysql_close($con);  // Close the connection to the database after results, not before.
?>

 

$server = "";      // Enter your MYSQL server name/address between quotes
$username = "";    // Your MYSQL username between quotes
$password = "";    // Your MYSQL password between quotes
$database = "";    // Your MYSQL database between quotes

Above this is where I have included my own details, but taken them out here due to privacy. This is the only part of the code where I have taken out my details the rest of code is how it is. Is this right?

 

Link to comment
Share on other sites

Ok I have done that above, but i'm totally puzzled I don't understand why it still does not show me anything still? been stuck on this for 3 days. it doesnt show no error messages or anything?

It shows absolutely nothing? No errors or anything? The only explanations I can see for the above code not to show anything would be 1) You are not really calling that page or 2) The database has nothing but empty records. The following modifications will tell you if either is the problem. I changed the logic slightly since I like to process error conditions first.

 

<?php

//Debug statement to verify this page is being loaded
echo "THE PAGE IS BEING EXECUTED!<br>";

$server = "";      // Enter your MYSQL server name/address between quotes
$username = "";    // Your MYSQL username between quotes
$password = "";    // Your MYSQL password between quotes
$database = "";    // Your MYSQL database between quotes

$con = mysql_connect($server, $username, $password);       // Connect to the database
if(!$con) { die('Could not connect: ' . mysql_error()); }  // If connection failed, stop and display error
mysql_select_db($database, $con);  // Select database to use
// Query database
$result = mysql_query("SELECT * FROM tablename");

if (!$result)
{
    echo "Error running query:<br>";
    trigger_error(mysql_error());
}
elseif(!mysql_num_rows($result))
{
    // no records found by query.
    echo "No records found";
}
else
{
    $i = 0;
    while($row = mysql_fetch_array($result)) {     // Loop through results
        $i++;
        echo "Displaying record $i<br>\n";
        echo "<b>" . $row['id'] . "</b><br>\n";      // Where 'id' is the column/field title in the database
        echo $row['location'] . "<br>\n";            // Where 'location' is the column/field title in the database
        echo $row['property_type'] . "<br>\n";       // as above
        echo $row['number_of_bedrooms'] . "<br>\n";  // ..
        echo $row['purchase_type'] . "<br>\n";       // ..
        echo $row['price_range'] . "<br>\n";         // ..
    }
}

mysql_close($con);  // Close the connection to the database after results, not before.
?>

 

Link to comment
Share on other sites

Note: trigger_error generates a NOTICE if no error level is specified. If error_reporting is not on, or is excluding NOTICE's, that page could produce absolutely nothing.

 

1) Do you have error reporting turned on?

 

error_reporting(E_ALL);
ini_set("display_errors", 1);

 

2) Is your table really called "tablename"?

 

Edit

You probably should check the result of the select db statement as well:

if (! mysql_select_db($database, $con)) {
  trigger_error(mysql_error(), E_USER_ERROR);
}

Link to comment
Share on other sites

No my tablename is called 'properties', Iv just changed that and still it shows nothing. Do you reckon its my HTML code because it break up the forms all the time?

 

HTML FORM


<table id="tb1">
<tr>
<td><p class="LOC">Location:</p></td>
<td><div id="LC">
<form action="insert.php" method="post">
<select multiple="multiple" size="5" style="width: 150px;" >
<option>Armley</option>
<option>Chapel Allerton</option>
<option>Harehills</option>
<option>Headingley</option>
<option>Hyde Park</option>
<option>Moortown</option>
<option>Roundhay</option>
</select>
</form>
</div>
</td>

	<td><p class="PT">Property type:</p></td>
<td><div id="PS">
	<form action="insert.php" method="post">
	<select name="property type" style="width: 170px;">
	<option value="none" selected="selected">Any</option>
	<option value="Houses">Houses</option>
	<option value="Flats / Apartments">Flats / Apartments</option>
	</select>
		</form>
	</div>
</td><td>
<div id="ptype">
<form action="insert.php" method="post"> 
<input type="radio" class="styled" name="ptype" value="forsale"/> For Sale 

<p class="increase">
<input type="radio" class="styled" name="ptype" value="forrent"/> To Rent
</p>
<p class="increase">
<input type="radio" class="styled" name="ptype" value="any"/> Any
</p>
</form>
</div>
</td>
</tr>

	</table>

	<div id="table2">
	<table id="NBtable">
	<tr>
	<td><p class="NBS">Number of bedrooms:</p></td>
	<td><div id="NB">
	<form action="insert.php" method="post">
	<select name="number of bedrooms">
	<option value="none" selected="selected">No Min</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	</select> to

	<select name="number of bedrooms">
	<option value="none" selected="selected">No Max</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	</select>
</form>		
	</div>
</td>

	<td><p class="PR">Price range:</p></td>
	<td><div id="PR">
	<form action="insert.php" method="post">
	<select name="price range">
	<option value="none" selected="selected">No Min</option>
	<option value="50,000">50,000</option>
	<option value="60,000">60,000</option>
	<option value="70,000">70,000</option>
	<option value="80,000">80,000</option>
	<option value="90,000">90,000</option>
	<option value="100,000">100,000</option>
	<option value="110,000">110,000</option>
	<option value="120,000">120,000</option>
	<option value="130,000">130,000</option>
	<option value="140,000">140,000</option>
	<option value="150,000">150,000</option>
	<option value="160,000">160,000</option>
	<option value="170,000">170,000</option>
	<option value="180,000">180,000</option>
	<option value="190,000">190,000</option>
	<option value="200,000">200,000</option>
	<option value="210,000">210,000</option>
	<option value="220,000">220,000</option>
	<option value="230,000">230,000</option>
	<option value="240,000">240,000</option>
	<option value="250,000">250,000</option>
	<option value="260,000">260,000</option>
	<option value="270,000">270,000</option>
	<option value="280,000">280,000</option>
	<option value="290,000">290,000</option>
	<option value="300,000">300,000</option>
	<option value="310,000">310,000</option>
	<option value="320,000">320,000</option>
	<option value="330,000">330,000</option>
	<option value="340,000">340,000</option>
	<option value="350,000">350,000</option>
	</select> to

	<select name="price range">
	<option value="none" selected="selected">No Max</option>
	<option value="50,000">50,000</option>
	<option value="60,000">60,000</option>
	<option value="70,000">70,000</option>
	<option value="80,000">80,000</option>
	<option value="90,000">90,000</option>
	<option value="100,000">100,000</option>
	<option value="110,000">110,000</option>
	<option value="120,000">120,000</option>
	<option value="130,000">130,000</option>
	<option value="140,000">140,000</option>
	<option value="150,000">150,000</option>
	<option value="160,000">160,000</option>
	<option value="170,000">170,000</option>
	<option value="180,000">180,000</option>
	<option value="190,000">190,000</option>
	<option value="200,000">200,000</option>
	<option value="210,000">210,000</option>
	<option value="220,000">220,000</option>
	<option value="230,000">230,000</option>
	<option value="240,000">240,000</option>
	<option value="250,000">250,000</option>
	<option value="260,000">260,000</option>
	<option value="270,000">270,000</option>
	<option value="280,000">280,000</option>
	<option value="290,000">290,000</option>
	<option value="300,000">300,000</option>
	<option value="310,000">310,000</option>
	<option value="320,000">320,000</option>
	<option value="330,000">330,000</option>
	<option value="340,000">340,000</option>
	<option value="350,000">350,000</option>

	</select>
</form>		
	</div>
</td>


</tr>

	</table>	

	</div>
	 <form id="submit" action="">
      <input type="submit" value="submit" />
      </form>

Link to comment
Share on other sites

Edit your php.ini file. Find the following directives, and change them to read as follows, then restart Apache.

 

display_errors = On

error_reporting = -1

 

While developing, this is the how those directives should be set, so all errors are reported. Enabling error reporting at runtime by adding the code to the script itself will not report fatal parse errors because the script is killed before any output is generated.

Link to comment
Share on other sites

They don't seem to allow per-user php.ini files, so for now just go with using the code in the script itself. Are you getting anything at all with the code, or just a white screen? If it's just a white screen, is there anything if you View ---> Source?

Link to comment
Share on other sites

Im getting absolutley nothing, its driving me crazy I dont understand why, iv used php before doing a login, but not come across this problem before. I basically click the search button of the form and it just refreshes the page and the form still appears. here the link to the website:

the form is on the homepage below the flash: http://mumtazproperties.hostei.com/

Link to comment
Share on other sites

OK, this is an HTML problem. The whole form needs to be in one set of <form></tags>. Right now, you have form tags scattered all over the place, and the submit button is in a set of form tags all by itself. That will not work that way. Why did you try to break up those tags anyhow?

Link to comment
Share on other sites

ahhh right. i broke up the tags cuz i had trouble with placing them on the page, so i tried all sorts until the positioning was where i wanted it i guess. So the whole form has to be in 1 tag? e.g.:

 

<form>

 

All html code for form

 

</form>

Link to comment
Share on other sites

I have taken all the form tags and only used 1, but i get a error in my html document validator: which says 'end tag for element form which is not opened' but it is opened? help will be appreciated.

 



<table id="tb1">
<tr>
<td><p class="LOC">Location:</p></td>
<td><div id="LC">
<form action="insert.php" method="post">
<select multiple="multiple" size="5" style="width: 150px;" >
<option>Armley</option>
<option>Chapel Allerton</option>
<option>Harehills</option>
<option>Headingley</option>
<option>Hyde Park</option>
<option>Moortown</option>
<option>Roundhay</option>
</select>

</div>
</td>

	<td><p class="PT">Property type:</p></td>
<td><div id="PS">

	<select name="property type" style="width: 170px;">
	<option value="none" selected="selected">Any</option>
	<option value="Houses">Houses</option>
	<option value="Flats / Apartments">Flats / Apartments</option>
	</select>
</div>
</td><td>
<div id="ptype">

<input type="radio" class="styled" name="ptype" value="forsale"/> For Sale 

<p class="increase">
<input type="radio" class="styled" name="ptype" value="forrent"/> To Rent
</p>
<p class="increase">
<input type="radio" class="styled" name="ptype" value="any"/> Any
</p>

</div>
</td>
</tr>

	</table>

	<div id="table2">
	<table id="NBtable">
	<tr>
	<td><p class="NBS">Number of bedrooms:</p></td>
	<td><div id="NB">

	<select name="number of bedrooms">
	<option value="none" selected="selected">No Min</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	</select> to

	<select name="number of bedrooms">
	<option value="none" selected="selected">No Max</option>
	<option value="1">1</option>
	<option value="2">2</option>
	<option value="3">3</option>
	<option value="4">4</option>
	<option value="5">5</option>
	</select>	
	</div>
</td>

	<td><p class="PR">Price range:</p></td>
	<td><div id="PR">

	<select name="price range">
	<option value="none" selected="selected">No Min</option>
	<option value="50,000">50,000</option>
	<option value="60,000">60,000</option>
	<option value="70,000">70,000</option>
	<option value="80,000">80,000</option>
	<option value="90,000">90,000</option>
	<option value="100,000">100,000</option>
	<option value="110,000">110,000</option>
	<option value="120,000">120,000</option>
	<option value="130,000">130,000</option>
	<option value="140,000">140,000</option>
	<option value="150,000">150,000</option>
	<option value="160,000">160,000</option>
	<option value="170,000">170,000</option>
	<option value="180,000">180,000</option>
	<option value="190,000">190,000</option>
	<option value="200,000">200,000</option>
	<option value="210,000">210,000</option>
	<option value="220,000">220,000</option>
	<option value="230,000">230,000</option>
	<option value="240,000">240,000</option>
	<option value="250,000">250,000</option>
	<option value="260,000">260,000</option>
	<option value="270,000">270,000</option>
	<option value="280,000">280,000</option>
	<option value="290,000">290,000</option>
	<option value="300,000">300,000</option>
	<option value="310,000">310,000</option>
	<option value="320,000">320,000</option>
	<option value="330,000">330,000</option>
	<option value="340,000">340,000</option>
	<option value="350,000">350,000</option>
	</select> to

	<select name="price range">
	<option value="none" selected="selected">No Max</option>
	<option value="50,000">50,000</option>
	<option value="60,000">60,000</option>
	<option value="70,000">70,000</option>
	<option value="80,000">80,000</option>
	<option value="90,000">90,000</option>
	<option value="100,000">100,000</option>
	<option value="110,000">110,000</option>
	<option value="120,000">120,000</option>
	<option value="130,000">130,000</option>
	<option value="140,000">140,000</option>
	<option value="150,000">150,000</option>
	<option value="160,000">160,000</option>
	<option value="170,000">170,000</option>
	<option value="180,000">180,000</option>
	<option value="190,000">190,000</option>
	<option value="200,000">200,000</option>
	<option value="210,000">210,000</option>
	<option value="220,000">220,000</option>
	<option value="230,000">230,000</option>
	<option value="240,000">240,000</option>
	<option value="250,000">250,000</option>
	<option value="260,000">260,000</option>
	<option value="270,000">270,000</option>
	<option value="280,000">280,000</option>
	<option value="290,000">290,000</option>
	<option value="300,000">300,000</option>
	<option value="310,000">310,000</option>
	<option value="320,000">320,000</option>
	<option value="330,000">330,000</option>
	<option value="340,000">340,000</option>
	<option value="350,000">350,000</option>

	</select>

	</div>
</td>


</tr>

	</table>
	<input type="submit" value="submit" />
     </form>	

	</div>

Link to comment
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.