/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] SQL is not pleased by my shenanigans.

[CODE]$result = $db->query(“SELECT * FROM uploads ORDER BY ID DESC WHERE ‘$crit’ = ‘$search'”);

Fatal error: Call to a member function fetch_assoc() on a non-object in /home/roadwolf/public_html/railsexy/gallery/pages/search.php on line 5[/CODE]

Line 5 being the fetch_assoc. It is also worth noting that crit is the category, and search is the string being searched in that category.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@thraddashJan 02.2011 — The WHERE clause should come before the ORDER BY, and Im not sure if your $crit should really be in quotes?

[CODE]$result = $db->query("SELECT * FROM uploads WHERE $crit = '$search' ORDER BY ID DESC");[/CODE]
Copy linkTweet thisAlerts:
@Not_to_mentionauthorJan 02.2011 — Thanks, didnt know it mattered.
Copy linkTweet thisAlerts:
@Not_to_mentionauthorJan 02.2011 — Well now the query is just acting weird. I changed the code to

$result = $db->query("SELECT * FROM uploads WHERE '$crit' LIKE '%$search%' ORDER BY ID DESC");

If i leave the box blank it works fine. If I put anything in at all, it returns with nothing.

Try the search box on and you will see what i mean.

www.railsexy.com/gallery
Copy linkTweet thisAlerts:
@thraddashJan 02.2011 — If $crit is a column name in the uploads table, you shouldn't put quotes around it.
Copy linkTweet thisAlerts:
@thraddashJan 02.2011 — If your still having problems, this is what I would do...

[code=php]$result = $db->query('SELECT * FROM uploads' . ($crit && $search ? ' WHERE ' . $crit . ' LIKE "%' . $search . '%"' : '') . ' ORDER BY ID DESC');[/code]

Basically, only do a filter if both the criteria and search variables have a value, otherwise return all entries.
Copy linkTweet thisAlerts:
@Not_to_mentionauthorJan 02.2011 — Thanks man, worked like a charm!
×

Success!

Help @Not_to_mention spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...