Jump to content

Can't display data from database


mackiever

Recommended Posts

I'm successfully connecting to my database but cannot retrieve/pull data from my database.  I'm using wamp. I figured I can echo any message I want but as soon as I enter the while loop and try to echo anything or in my case echo the rows from the database it simply does not work.  I don't get any errors when running on wamp and I'm running out of ideas as of why this is happening.

 

I'm using a form tag and extracting the users/people stored in the database with gender = "male/female", depending on which "radio/option" the user decides.

 

Any help would be highly appreciated.


<html>
			<head>
				<title>Enter title here</title>
			</head>
			
			
			
			<body>
			
			<?php
if ($_GET) {
	$gender = $_GET[ 'gender'];
	$connect = mysql_connect("localhost", "root", "mypassword");
	if ($connect) {
			mysql_select_db("ourDatabase", $connect);
			$query = "SELECT * 
							FROM mytable 
							WHERE Gender = ' " . $gender. " ' ";
			$result = mysql_query($query);
			while($row = mysql_fetch_array($result) ) {
					echo $row[ ' Name ' ]  . "<br/>" . $row[ ' Surname ' ] . "<br/>" . $row[ ' Email ' ] ;
				
					}
			} else {
				die (mysql_error() );
					}
					
}
	
		
?>
				
				<form action="" method = "GET">
					find entries that are: <br/>
					<input type= "radio" name = "gender" value = "Male">Male</input><br/>
					<input type = "radio" name = "gender" value = "Female">Female</input><br/>
					<input type ="submit" value = "Select"/>
					
				</form>
			</body>
			
<html>

20pzrx1.jpg

 

 

 

 

 

 

Link to comment
Share on other sites

your query statement contains a space character, both before and after the $gender value, so unless you actually have spaces stored in with your data, the query will never match anything.

 

in a large amount of cases of a beginner using confrontation to build a string in php, they end up with errors due to the clutter. use the simplest, least mix of different contexts, to produce any statement -

$query = "SELECT * FROM mytable WHERE Gender = '$gender'";
Link to comment
Share on other sites

1) I am aware that form can only be post by method=post , not method=get, not sure if yours will work that way.

 

2) You called for Name and Surname to be echoed, since there is a while loop to fetch array results from db, i think you should do this

$Name = $row['Name'];

$SurName = $row['SurName'];

 

then followed by echo '$Name' and echo '$SurName' , step by step instead of combining everything together.

 

before you echo it out.

 

3) $query = "SELECT *

                            FROM mytable
                            WHERE Gender = ' "
. $gender. " ' ";

 

change to 

 

$query = "SELECT *

                            FROM mytable
                            WHERE Gender = '".
$gender."' ";

 

4) I see an email echoing out from your while loop, while there is no email from the form. 

Edited by KaiSheng
Link to comment
Share on other sites

Hey guys I'd like to thank you for your quick response it really helped see what was wrong.  It all came down to this specific line:

1)

WHERE Gender = ' ".$gender." ' ";

it seems the above line will not work this way because the SPACE between '[single quote] "[double-quotes].

 

 

 

2)

WHERE Gender = '".$gender."' ";

will work or its equivalent:

WHERE Gender = '$gender' ";

Both work.  I guess my new question is what's the difference between the two working statements? 

 

Once again, thank you!

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.