Complicated (I think!) beginner's question...

Hi all,

I’m very new to all this, so hopefully even if I can’t get a solution, maybe somebody can point me in the right direction at least.

I’ve been given a project at work so I can improve my PHP/website skills, however I have come to a halt.

What I am trying to do is have a search box on a webpage, used to search our database (It’s MSSQL, not MySQL by the way) and then display the result as a dropdown box with just dates showing (on the same page), i.e. the dates that the search term has been found on.

I have figured out the sql query, I am already connected to the database in the php file, so what I would like some pointers in, is the best method to display my search result on the webpage. There is already a drop down box on the page with a list of all dates so that users can retrieve data from any given date, this will hopefully be used to display the search result dates as well.

What do I need to supply (if anything) to help people provide any answers/suggestions? I can’t link to the page as it’s an internal website, and I also have to be aware of posting too much here for security reasons. Sorry to be so vague, I have tried searching the manual and various other tutorials and sources, but nothing seems to quite fit my exact scenario.

Thank you in advance.

-Pete.

execute your query
fetch data to an associative array
do a loop for each result in the array and print out the option line

pseudo code, you should be using mysqli or pdo, I have a pdo tutorial linked in my signature if you want to take a look at it. Using mysqli or pdo with parameterized queries (like below) you do not have to worry about escaping data to avoid sql injection.
[php]$search = !empty($_GET[‘search’]) ? $_GET[‘search’] : ‘’;

$stmt = $mysqli->prepare(‘SELECT * FROM data WHERE something = ?’);
$stmt->bind_param(‘s’, $search);
$result = mysqli_fetch_assoc($stmt);

?>

<?php foreach ($result as $row) { ?> <?= $row['date'] ?> <?php } ?> [/php]

Thanks Jim, you are on the ball tonight!

Just got home from work, otherwise I would have replied earlier. I’ll take a look at your code (and MANY thanks for taking the time!) and the tutorial in your sig that you mention, even if it’s just out of interest. Not really knowing much about mysqli, but would I be correct in assuming that works with MySQL, as I am using MSSQL, or does it not make any difference?

Thanks again,

-Pete.

He’s using MSSQL, I believe he has to use PDO for mysqli is dependent on MySQL.

You and strider are correct :slight_smile: I was so hung up on showing parameterized queries I forgot you were using mssql! I prefer pdo anyway, so in my tutorial everything should work. Just remember to change mysql: to mssql: in the DB.php file :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service