Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189

    MySQL search code issue

    Hi all -


    I'm having a search problem with mySQL. I originally thought the problem was related to my beginner's knowledge of wildcards, but I'm not so sure. Anyway, I have no problem creating, populating, and selecting the database.


    So here is the (abbreviated - without comments) php code:

    <?php

    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $search_output = "";

    if(isset($_POST['searchquery']) && $_POST['searchquery'] != "")
    {

    $searchquery = preg_replace('#[^a-z]#i', '', $_POST['searchquery']);
    $sqlCommand = "SELECT id, word FROM etytrial WHERE word LIKE '%$searchquery%'";
    include_once("db/dbconn.php");
    $query = mysql_query($sqlCommand) or die(mysql_error());
    $count = mysql_num_rows($query);

    if($count > 1){
    $search_output .= "$count results for: $searchquery<br />";
    while($row = mysql_fetch_array($query)){
    $id = $row["id"];
    $word = $row["word"];
    $syn = $row["syn"];
    $search_output .= "*<br>$id:<b>$word</b>- <br/><b><i>Translations include:</i></b>$syn<br/>";
    }
    } else {
    $search_output = "<hr />0 results for <strong>$searchquery</strong><hr />$sqlCommand";
    }
    }
    ?>
    When I search by $word as $searchquery, I get 0 results for $searchquery.

    If I search by $syn (I have purposely used "blah" in the syn column for many of my rows) I get output. But, for searches with $syn I also get this type of error:
    Notice: Undefined index: whatever_$row in /public_html/ety.php on line X

    I really can't tell what I'm doing wrong...it doesn't seem to be wildcard related (I've used '%$searchquery' and '$searchquery'). $sqlCommand does not really give me anything I can use to resolve this.

    Please help.
    Last edited by acegreen; Aug 30th, 2013 at 11:08 AM.

  2.  

  3. #2
    Senior Member Webzarus's Avatar
    Join Date
    May 2011
    Location
    South Carolina Coast
    Posts
    3,322
    Member #
    27709
    Liked
    770 times
    doesnt this :

    if(isset($_POST['searchquery']) && $_POST['searchquery'] != "")

    mean if the searchquery is empty ?

    then run the script.


  4. #3
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189
    Quote Originally Posted by Webzarus View Post
    doesnt this :

    if(isset($_POST['searchquery']) && $_POST['searchquery'] != "")

    mean if the searchquery is empty ?

    then run the script.

    My understanding is &&($_POST['searchquery']!="") just means "and if searchquery is not empty" then run. It basically reinforces the the first part of isset.

    In any case I tried this variation:
    (1) if(isset($_POST['searchquery']) //on its own

    And then also changed operators
    (2) if(!empty($_POST['searchquery']) //my understanding is that empty is more robust.

    I even tried:
    (3)if(!empty($_POST['searchquery']) && $_POST['searchquery'] != "")


    In all instances, the issue is the same.


    I get 0 results when using $word. When I search "blah" using $syn I get results for all my rows ("blah" runs through the syn column) with the undefined index error popping up. So I am at a loss...
    Last edited by acegreen; Aug 30th, 2013 at 02:31 PM.

  5. #4
    Member
    Join Date
    Mar 2012
    Posts
    50
    Member #
    31189
    Issue resolved!!! Turns out it was $sqlCommand=SELECT problem. Thanks all


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:12 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com