-1

I'm trying to write a search engine using pdo/php but I am a beginner still in programming and I need ur help!

The search engine's results should be displayed on the same page as the engine. (preferably in a table) I have been trying to play with various MySql scripts I got from tutorials and w3schools.com but I can't figure this out:

How do I write the piece of code that makes my search.php select from my DB_table what is being searched for in the search engine?

Been trying this last time using mysql :

<form action='./search.php' method='get'>
            <input type='text' name='k' size='50' value='<?php echo $_GET['k']; ?>' />
            <input type='submit' value='Search' />
        </form>
        <hr />
        <?php
            $k = $_GET['k'];

            $terms = explode(" ", $k);
            $query = "SELECT * FROM Callflow WHERE ";
            foreach  ($terms as $each){
                $i++;
                if ($i == 1)
                    $query .= "keywords LIKE '%$each%' "; 
                else
                    $query .= "OR keywords LIKE '%$each%' ";
            }

            <?php
    $db = new PDO('mysql:host=localhost;dbname=voizxl_wachtrij;charset=utf8', 'root', '');
?> 
    $query = mysql_query($query);
        $numrows = mysql_num_rows($query);
        if ($numrows > 0) {

            while ($row = mysql_fetch_assoc($query)){
                $id = $row['calliipid'];
                $title = $row['calleridname'];
                $keywords = $row['calleridnum'];

                echo "<h2><a href='$title'</a></h2>
                $keywords<br /><br />";
        }

        }
        else
            echo "No results found for \<b>$k</b>\"";
        mysql_close();

        ?>

Only when I tried this code I got errors, but I post it so u can see what i'm trying to achieve.

Now in PDO I can't figure out how to write this.. I'm experimenting with codes like :

<?php
    $db = new PDO('mysql:host=localhost;dbname=voizxl_wachtrij;charset=utf8', 'root', '');
?> 



<?php
    foreach($db->query('SELECT * FROM Callflow') as $row) {
        echo $row['calleridname'];
    }
?>
<?php
    $stmt = $db->prepare("SELECT * FROM Callflow WHERE id=:id AND name=:name");
    $stmt->execute(array(':name' => $name, ':id' => $id));
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>


<?php
    $stmt = $db->query('SELECT * FROM table');
    $row_count = $stmt->rowCount();
    echo $row_count.' rows selected';
?>

Could someone please help by explaining the logic in writing the code or by giving an example of how to achieve what I want? Would be very much appreciated! TY in advanced!

Kentje
  • 37
  • 8
  • Did you try any of suggested or related questions this site offers to you? – Your Common Sense Sep 09 '13 at 08:17
  • This code is vulnerable to XSS attacks: http://en.wikipedia.org/wiki/Cross-site_scripting – PeeHaa Sep 09 '13 at 08:18
  • Here's the thing: I am programming for about 2 months now ( I'm still a newbie) and all I know is to look for related questions on stackoverflow to try relate to these codes and find the answer to my question and this way learn what I know through trail and error. I recently bumped into this: http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php This is a helpfull tutorial for me to know what codes to use to achieve certain actions, but the how-to-write-it-out part, I HAVE NO IDEA.. Where can I learn this? Any good links? – Kentje Sep 09 '13 at 08:32
  • All I get nowadayas when I ask something is ppl donwvoting my questions and commenting stuff like: Ur code is deprecated or Don't use this or don't use that or this doesn't work.. How is one supposed to find out /learn what he needs to know this way then? – Kentje Sep 09 '13 at 08:34
  • When asking a question, you have a list of related questions. Follow the links, take your time, read and comprehend. You are downvoted because 1) you use some retarded shorthand for communicating (ppl, ur etc.) which makes you look immature and 2) there are many questions such as yours. You could have found them, just googling "php pdo stackoverflow" would yield plenty of results. This just implies you're hasty and being hasty is a bad thing. Sure, someone could answer all your questions but where's the trial and error process? That's the thing that makes you learn the best. – N.B. Sep 09 '13 at 08:39
  • Well by all means thank you for your honesty but what do you expect from someone who is a beginner? If I tell you in my posted question that I am a newbie and I don't know and can't find how to write-out-a-code and then I ask for links or an example, How is this being hasty? Wouldn't U get the impression that I really don't know how to write something and I am asking for a reference? If i have a max of available characters to write something< I try to do it as short as I can. if that makes me retarded than I don't see what the use is of having a forum where I must keep my question short?? – Kentje Sep 09 '13 at 08:50
  • You don't have to keep your question short, please don't make up stuff, we really aren't dumb. The other thing is that you are supposed to do research on your own, using words of your own to try and find things that interest you. Now, we could do this all day, the fact remains that you need to spend HOURS sometimes to get an answer to your question. And when you finally achieve it, you'll feel happy. If you want to rush, then you might not be cut out for a programmer after all. – N.B. Sep 09 '13 at 09:03

1 Answers1

1

Well by all means thank you for your honesty but what do you expect from someone who is a beginner?

There is one thing. An essential one.
It's about programming.

Most people take it as a sort of hobby, an easy-go thing. But never as a profession which require years of education and experience.
Most people never take programming seriously, like surgery or nuclear physics. So, all their idea of education is to ask someone to guide.

However, the truth is:

YES. Sometimes you are just unable to solve whatever particular task because of lack of education or experience. One cannot build a skyscraper by asking questions on a forum.

If you are a beginner - then you need to learn. Learn basic elements. Learn to create simpler applications. Spend time. And then eventually be able to accomplish more complex tasks without asking people to write all the code for you.

If you can't get any help from dozens of similar questions - then you need to learn first to be able to understand the code from answers.

But again - there is nothing wrong if you can't accomplish your search at once. We all had to learn. We all were unable to do it one day and we all had to grow up first.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345