Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

Web Design & Development Web Design & Development
Search Search
Search for:
Tech Support Guy > > >

Solved: MySql Simple Search - problem


(!)

JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,585 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
30-Jul-2012, 04:56 AM #1
Solved: MySql Simple Search - problem
I am using the following code to do a search on my db...
PHP Code:
        $element $_REQUEST["element"];
        
$criteria trim($_REQUEST["criteria"]);

        
$row mysql_query("SELECT * FROM vehicles WHERE $element LIKE '%$criteria%'") or die(mysql_error()); 
and am getting this response
Quote:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
I cannot for the life of me see why - can you help?
dukevyner's Avatar
dukevyner   (Luke) dukevyner is offline
Computer Specs
Member with 787 posts.
 
Join Date: Nov 2011
Location: Pakenham, VIC, Australia
Experience: Advanced
30-Jul-2012, 05:01 AM #2
I've seen this error before... But don't know why when I did
dukevyner's Avatar
dukevyner   (Luke) dukevyner is offline
Computer Specs
Member with 787 posts.
 
Join Date: Nov 2011
Location: Pakenham, VIC, Australia
Experience: Advanced
30-Jul-2012, 05:07 AM #3
what's on line 1
JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,585 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
30-Jul-2012, 05:17 AM #4
Line 1 is the mysql query line...
dukevyner's Avatar
dukevyner   (Luke) dukevyner is offline
Computer Specs
Member with 787 posts.
 
Join Date: Nov 2011
Location: Pakenham, VIC, Australia
Experience: Advanced
30-Jul-2012, 05:33 AM #5
can you post the full code? where did you specify the mysql_connect?
JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,585 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
30-Jul-2012, 05:48 AM #6
Connect...
PHP Code:
    function OpenDB()
    {
        global 
$result;

        
$con mysql_connect("localhost","surfwhee_jim","********************") or die(mysql_error());

        if (!
$con)
        {
            die(
'Could not connect: ' mysql_error());
        }
        
        
mysql_select_db("surfwhee_vehicles"$con) or die(mysql_error());
    
    } 
(Password obscured)
This is the CMS compatible php code...
PHP Code:
    else if (isset($_REQUEST["action"]) && $_REQUEST["action"] == "do_search")
    {
        
OpenDB();
        
        
$element $_REQUEST["element"];
        
$criteria trim($_REQUEST["criteria"]);

        
$row mysql_query("SELECT * FROM vehicles WHERE $element LIKE $criteria ORDER BY id") or die(mysql_error());

        
BB_RedirectPage("success""Search Results for " $criteria " in " $element, array("action=managevehicles&sec_t=" BB_CreateSecurityToken("managevehicles") . "&order=" $order));
    }
    else if (isset(
$_REQUEST["action"]) && $_REQUEST["action"] == "search")
    {
        
        
$criteria "";
        
$element "";
        
$contentopts = array(
            
"desc" => "Search the Database for specific criteria.",
                
"nonce" => "action",
                
"hidden" => array(
                    
"action" => "do_search",
                ),
                
"fields" => array(
                
"startrow",
                array(
                    
"title" => "Search in...",
                    
"type" => "select",
                    
"name" => "element",
                    
"options" => array("datetime" => "Date""fullname" => "Name""make" => "Make""year" => "Year""model" => "Model",),
                    
"width" => "150px",
                    
"value" => BB_GetValue("element"$element),
                    
"desc" => ""
                
),
                array(
                    
"title" => "Search for...",
                    
"type" => "text",
                    
"name" => "criteria",
                    
"width" => "200px",
                    
"value" => BB_GetValue("criteria"$criteria),
                    
"desc" => ""
                
),
            ),
            
"submit" => "Go",
            
"focus" => true
        
);

        
BB_GeneratePage("Search For"$menuopts$contentopts);

    } 
JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,585 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
30-Jul-2012, 09:16 AM #7
Since we last comm'd I have taken out the element variable by using switch and case viz...
PHP Code:
        $element $_REQUEST["element"];
        
$criteria mysql_real_escape_string($_REQUEST["criteria"]);
        switch (
$element)
        {
        case 
"datetime":
            
$row mysql_query("SELECT * FROM vehicles WHERE datetime LIKE '%$criteria%'") or die(mysql_error());
            break;
        case 
"fullname":
            
$row mysql_query("SELECT * FROM vehicles WHERE fullname LIKE '%$criteria%'") or die(mysql_error());
            break;
        case 
"make":
            
$row mysql_query("SELECT * FROM vehicles WHERE make LIKE '%$criteria%'") or die(mysql_error());
            break;
        case 
"year":
            
$row mysql_query("SELECT * FROM vehicles WHERE year LIKE '%$criteria%'") or die(mysql_error());
            break;
        case 
"suburb":
            
$row mysql_query("SELECT * FROM vehicles WHERE suburb LIKE '%$criteria%'") or die(mysql_error());
        } 
...but it still doesn't work
JiminSA's Avatar
JiminSA   (Jim) JiminSA is offline JiminSA has a Profile Picture
Computer Specs
Member with 1,585 posts.
THREAD STARTER
 
Join Date: Dec 2011
Location: Johannesburg South Africa
Experience: Intermediate
31-Jul-2012, 07:42 AM #8
Conclusion
I found the problem! It was after the search in the display routine - using the wrong select variable...

But thanks for all your help Duke
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑