Need help getting search results from sql database!

I have a SQL database set up with 11 columns: ID, FIRST, LAST, and then ONE through EIGHT. I have it setup to submit data and that works fine but Im having trouble creating the php code to display search results.

Here is the form I am using to create a search.
[php]

Period

1
2
3
4
5
6
7
8

Teacher’s Last Name


submit


[/php]

When I use PhpMyAdmin I can query the database with this[php]SELECT first , last
FROM data
WHERE six LIKE ‘mele’[/php] getting the correct results.

Except I want it to be WHERE (period from form) LIKE '(teacher from form)`

I haven’t been able to get a correct search results from any php code I have tried using.

HELP ME!!!

THANK YOU :smiley:

i just tried using this php code and it came back with an error on line 10.

[php]<?php
mysql_connect (“server”, “user”,“password”) or die (mysql_error());
mysql_select_db (“database”);

$teacher = $_POST[‘teacher’];
$period = $_POST[‘period’];

$sql = ("SELECT first,last FROMdataWHERE%$period%` LIKE ‘%$teacher%’ ");

while ($row = mysql_fetch_array($sql)){
echo ‘
’.$row[‘first’][‘last’];
echo ‘

’;
}

?>[/php]

[php]
echo ‘
’.$row[‘first’][‘last’];
[/php]

You aren’t using a multi-dimensional array so this will just fail. try

[php]
echo ‘
’.$row[‘first’].$row[‘last’];
[/php]

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a9672693/public_html/search.php on line 10

I am still getting an error.

[php]
$sql = ("SELECT first,last FROM data WHERE %$period% LIKE ‘%$teacher%’ ");
[/php]

That should fix it.

THANK YOU SOOOO MUCH!!! :smiley: :smiley: :smiley: :smiley: :smiley: :smiley:

Dont mean to bring up old threads but this is the same thing i want to do, but i altered it a bit, hopefully someone can help:
HTML

<form action="searchfunc.php" method="post"> <p>Period Case ID <input name="caseid" type="text" value="caseid"> Last Name <input name="lname" type="text" value="lname"> </p> <p>submit <input type="submit" name="submit" id="submit" value="Submit"> </p> </form>
PHP
[php]<?php
mysql_connect (“localhost”, “","") or die (mysql_error());
mysql_select_db ("
******”);

$lname = $_POST[‘lname’];
$caseid = $_POST[‘caseid’];

$sql = ("SELECT fname,lname FROM case WHERE %$caseid% LIKE ‘%$lname%’ ");

while ($row = mysql_fetch_array($sql)){
echo ‘
’.$row[‘fname’].$row[‘lname’];
echo ‘

’;
}

?>[/php]

I’ll help you out if you reply asking to show you how to do it in PDO. I’m simply not fixing problems with people’s database scripts any more if they’re using mysql_query as it will be dropped from PHP soon. See here the big warning sitting right on the PHP.net page! http://php.net/manual/en/function.mysql-query.php

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used. See also MySQL: choosing an API guide and related FAQ for more information. Alternatives to this function include:

mysqli_query()
PDO::query()

ive been trying to figure this out for 3+ days now, im trying to search a database for a Last name and Case number, and it doesnt show results if you dont enter both,
lname - Last name string
caseid - Case number
any info would help really, thanks!

Sponsor our Newsletter | Privacy Policy | Terms of Service