/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] searching database

Good evening everyone. (Manila Time). ?

I have a question regarding searching the mysql database with the use of PHP. I have a code that searches for a specific output. Now it works fine when searching on one column. I searched for the position applied of the user. It works. Now I want my search to have another entry which is age. I want it to be on condition with the first.

[code=php]//form.php
<form method=”post” action=”search.php”>
<select name=”search”>
<option value=”Secretary”>Secretary</option>
<option value=”Accountant”>Accountant</option>
</select>
<input type=”text” name=”age” value=”” />
<input type=”Submit” name=”Submit” value=”Submit”>
</form>[/code]

[code=php]//search.php
<?php
require_once(‘admin_authenticate.php’);
include ‘db_connect.php’;

$search=$_POST[‘search’];
$age = $_POST[‘age’];

$result = mysql_query(“SELECT * FROM users WHERE position_applied LIKE ‘%$search%'”);

echo “The system found ” . mysql_num_rows($result) . ” results.” . “<br />”;

while($r=mysql_fetch_array($result))
{
$id = $r[‘id’];
$position = $r[‘position_applied’];
$username = $r[‘username’];
$emailadd = $r[’emailadd’];
$firstname = $r[‘firstname’];
$midname = $r[‘midname’];
$lastname = $r[‘lastname’];

echo “<b>Position Applied :</b>” . ” ” . $position . “<br />”;
echo “<b>Username :</b>” . ” ” . $username . “<br />”;
echo “<b>Email Address :</b>” . ” ” . $emailadd . “<br />”;
echo “<b>Name :</b>” . ” ” . $firstname . ” ” . $midname . ” ” . $lastname . “<br />”;
echo “<br />”;
}

?>[/code]

The script above works. However I want to display only those records who have an age greater than the one I will input on the text field. How is it?

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@LetmeinJan 04.2010 — LIKE '&#37;$search%' AND 'Age' > $age
Copy linkTweet thisAlerts:
@kingdmauthorJan 04.2010 — I didn't know its possible on the query to put a >. :p

Thanks Letmein. I could let you in here in our house, ?

By the way what are the other conditions I can put in the query?
Copy linkTweet thisAlerts:
@LetmeinJan 04.2010 — You can also do LIKE '&#37;$search%' AND 'Age' > $age ORDER BY 'Age'

You have quite a few operators e.g

">=" Greater than Equal to

"<" Less than

"NOT LIKE"

The % kind of searches the field, so for example if "position_applied" is definately only either Secretary or Accountant, then position_applied = "Secretary" should work... However if position_applied contains things like "This is a Secretary" then you could use "%" however that wouldnt work if position_applied contained "This is a Secretary not an Accountant" as LIKE "%Accountant%" would bring that up too ?
Copy linkTweet thisAlerts:
@kingdmauthorJan 04.2010 — [I][B]You can also do LIKE '%$search%' AND 'Age' > $age ORDER BY 'Age'[/B][/I]

Is this ascending or descending?
Copy linkTweet thisAlerts:
@LetmeinJan 04.2010 — You can use ASC or DESC however I think the default is Ascending ? (cant remember, lol)

ORDER BY 'Age' ASC, 'OtherFieldYouCouldWant' DESC
Copy linkTweet thisAlerts:
@kingdmauthorJan 04.2010 — Thank you Letmein for explaining.

Can you help me with my code, my focus problem now is the script keep on running even before the submit is click. I did set and isset() but it seems that it doesnt function at all. ?

Here is my code.

[code=php]<?php
require_once('admin_authenticate.php');
include 'db_connect.php';

if(isset($_POST['submit']))
{
$search=$_POST['position'];
$result = mysql_query("SELECT * FROM users WHERE position_applied LIKE '%$search%' AND age <= 30");

echo "The system found " . mysql_num_rows($result) . " results." . "<br />";

while($row=mysql_fetch_array($result))
{

$id = $row['id'];
$position = $row['position_applied'];
$username = $row['username'];
$emailadd = $row['emailadd'];
$firstname = $row['firstname'];
$midname = $row['midname'];
$lastname = $row['lastname'];
$age = $row['age'];

echo "<b>Position Applied :</b>" . " " . $position . "<br />";
echo "<b>Username :</b>" . " " . $username . "<br />";
echo "<b>Email Address :</b>" . " " . $emailadd . "<br />";
echo "<b>Name :</b>" . " " . $firstname . " " . $midname . " " . $lastname . "<br />";
echo "<b>Age :</b>" . " " . $age . "<br />";
}
}
else
{}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="100" border="0" align="center">
<tr>
<th scope="col">
<select name="position">
<option value="Secretary">Secretary</option>
<option value="Accountant">Accountant</option>
</select>
</th><th scope="col"><input type="submit" name="submit" value="Search" style="font-family:Verdana, Geneva, sans-serif; font-size:12px;" /></th>
</tr>
</table>

</form>
</html>[/code]


I want the output to be displayed on a new <tr> in the table of the html. The result is keep getting displayed on top of the form.
Copy linkTweet thisAlerts:
@LetmeinJan 04.2010 — [CODE]

<html xmlns="http://www.w3.org/1999/xhtml">
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<table width="100" border="0" align="center">
<tr>
<th scope="col">
<select name="position">
<option value="Secretary">Secretary</option>
<option value="Accountant">Accountant</option>
</select>
</th><th scope="col"><input type="submit" name="submit" value="Search" style="font-family:Verdana, Geneva, sans-serif; font-size:12px;" /></th>
</tr><tr><th>

<?php
require_once('admin_authenticate.php');
include 'db_connect.php';

if(isset($_POST['submit']))
{
$search=$_POST['position'];
$result = mysql_query("SELECT * FROM users WHERE position_applied LIKE '%$search%' AND age <= 30");

echo "The system found " . mysql_num_rows($result) . " results." . "<br />";

while($row=mysql_fetch_array($result))
{

$id = $row['id'];
$position = $row['position_applied'];
$username = $row['username'];
$emailadd = $row['emailadd'];
$firstname = $row['firstname'];
$midname = $row['midname'];
$lastname = $row['lastname'];
$age = $row['age'];

echo "<b>Position Applied :</b>" . " " . $position . "<br />";
echo "<b>Username :</b>" . " " . $username . "<br />";
echo "<b>Email Address :</b>" . " " . $emailadd . "<br />";
echo "<b>Name :</b>" . " " . $firstname . " " . $midname . " " . $lastname . "<br />";
echo "<b>Age :</b>" . " " . $age . "<br />";
}
}
else{ echo "&nbsp;"; }
?>
</th></tr>
</table>

</form>
</html>


[/CODE]


I dont know if this is right because I cant test it... However please try that ?
Copy linkTweet thisAlerts:
@kingdmauthorJan 04.2010 — Thanks for replying Letmein.

I did try that, I edited a little of it, such as placed the require and include on the top of the page. With your help and the other guys on phpbuilder, this problem is solved. (as of now) ?

Thanks once again.
Copy linkTweet thisAlerts:
@LetmeinJan 04.2010 — No problem happy to help ?
×

Success!

Help @kingdm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...