Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: WHERE Statement

  1. #1
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default WHERE Statement

    I have a database with a whole bunch of columns, for a search thing I'm building.

    I can recall data from the DB, but I can't get it to work with variables.

    Here's where I have trouble:
    PHP Code:
    $keywords $_GET['query'];
    $topic mysql_query("SELECT keywords FROM webindex WHERE keywords=$keywords"); 
    I think it's the variable in the WHERE statement.

    Am I doing something wrong?

    Thanks,
    X96
    Last edited by Snookerman; 05-09-2009 at 05:44 PM. Reason: added “Resolved” prefix
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  2. #2
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Add ticks (`) around column names and enclose strings with quotes, like so:

    PHP Code:
    $keywords $_GET['query'];
    $topic mysql_query("SELECT `keywords` FROM webindex WHERE keywords='$keywords'"); 
    Should fix it.

  3. #3
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    Nope - it just displays the entire table... not filtering...

    Here's my entire PHP code:
    PHP Code:
                <?php
                    
    // All Code (C) 2009 X96 Web Design
                    
    if(isset($_GET['n'])) {
                        
    $con mysql_connect("localhost","myuser","mypass");
                        if (!
    $con) {
                            die(
    'Could not connect: ' mysql_error());
                        }
                        
    mysql_select_db("index"$con);
                        
    $get $_GET['n'];
                        
    $results mysql_query("SELECT * FROM webindex");
                        
    $topic mysql_query("SELECT `keywords` FROM webindex WHERE keywords='$get'");
                        while(
    $row mysql_fetch_array($results)) {
                            if(isset(
    $_GET['n'])){
                                echo 
    '<h2>Showing Results for <em>'.$_GET['n'].'</em></h2>';
                                echo 
    '<p class="result"><a href="'.$row['url'].'" target="shared">'.$row['title'].'</a><br/><span><span>Indexed on '.$row['added'].'</span><br/>'.$row['description'].'</span><br/><span class="url">'.$row['url'].'</span></p>';
                            }
                            else {
                                echo 
    '<h1>No Pages Found!</h1><p>No results could be found for the keyword(s) you entered.</p>';
                            }
                        }
                        
    mysql_close($con);
                    }
                    elseif(isset(
    $_GET['n']) == null) {
                        echo 
    'Please enter a keyword into the search form.';
                    }
                    else {
                        echo 
    'Hacker.';
                    }
    No matter what keywords are entered, it still displays all of the table data...
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  4. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    Well it's because you're using the wrong query, you're getting the array from

    PHP Code:
    $results mysql_query("SELECT * FROM webindex"); 
    So all rows are being selected from webindex, then you use this a bit later on:

    PHP Code:
    while($row mysql_fetch_array($results)) 
    So of course the whole table is being echoed, you need to change the query to $topic:

    PHP Code:
    while($row mysql_fetch_array($topic)) 
    Maybe I'm wrong, but no matter - Once you've defined the $topic variable you don't use it anywhere else in your code, so this will be the problem.

    Make the above alteration and see what happens.

    Since you'll still need the $results variable, due to you echoing out all those columns, like url etc, but I don't know where in the code you want to use the keyword.

  5. #5
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    It didn't work... But I scrapped my project, and am working on something else...

    Thanks for your help!

    // X \\
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  6. #6
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    This helped me but with some mods...
    Code:
    SELECT column FROM table WHERE column='$variable' and diffcolmn is not nul
    l


    this is not using mysql though....also i have this enclosed in php tags to render it differntly which you'll need if your trying to process it.

  7. #7
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    I get this:
    Parse error: syntax error, unexpected T_STRING in /home/public_html/members.php on line 17
    All the column names are right...

    Are you sure it's not SQL?

    // X96 \\
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  8. #8
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    Never Mind - I got it... I used this:
    Code:
    $sessionuser = $_SESSION['username'];
    $sql = mysql_query("SELECT `column1` FROM `table` WHERE `column2` = '$sessionuser'");
    $row = mysql_fetch_row($sql);
    and then echoed it:
    Code:
    echo $row[0];
    Problem solved.

    // X96 \\
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  9. #9
    Join Date
    Feb 2009
    Posts
    303
    Thanks
    18
    Thanked 36 Times in 36 Posts

    Default

    Now... How do I get it to echo all of the table contents instead of just the first row? It'll show me the data from the first row, but that's it...

    Thanks.
    Alex Blackie, X96 Design
    My Website
    I specialize in: HTML5, CSS3, PHP, Ruby on Rails, MySQL, MongoDB, Linux Server Administration

  10. #10
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    PHP Code:
    $sql mysql_query("SELECT * FROM table");

    while(
    $row mysql_fetch_array($sql)) { // We'll say there's a field called name

    echo 'Hello, in this record the person\'s name is: ' $row['name'];


    Then add in relevant fields.

    Or if this is happening after the query has completed then you can use:

    PHP Code:
    foreach ($row as $record) {
    echo 
    $record '<br />';


  11. The Following User Says Thank You to Schmoopy For This Useful Post:

    X96 Web Design (05-09-2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •