Warning: mysql_fetch_assoc(): supplied argument is not a val

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Why did all the OR's change to AND's?
oskare100
Forum Commoner
Posts: 80
Joined: Sun Oct 29, 2006 5:47 am

Post by oskare100 »

Hello,
OK, do you mean like this:

Code: Select all

'".$item_amount."'
? When I tried that it still didn't echo anything. Was it that you ment? And/or is there anything else I should change?

Best Regards
Oskar R
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oskare100 wrote:OK, so I should use something like;

Code: Select all

AND ((`identify_pos` LIKE '%$item_title%') OR (`identify_pos2` LIKE '%$item_title%'))
instead of

Code: Select all

AND ((`identify_pos` OR `identify_pos2`) LIKE %$item_title%)
then? Or how should I do it?
That takes care of the first issue but not
volka wrote:But that's still wrong, you would need something like
AND (('%ebooks%' LIKE 'Templates, test, ebooks') OR ('%templates%' LIKE 'Templates, test, ebooks') )
Either add the appropriate % characters to your records or use e.g. INSTR, see http://dev.mysql.com/doc/refman/4.1/en/ ... tions.html

Same with
AND ((`identify_neg` OR `identify_neg2`) NOT LIKE '%$item_title%')
and the purpose of
AND ((`identify_pos` OR `identify_pos2` OR `identify_neg` OR `identify_neg2`) != $item_title)
I do not understand at all.
'%kare%' LIKE 'oskare100' -> true
but you have: 'kare' LIKE '%oskare100%' -> false
oskare100
Forum Commoner
Posts: 80
Joined: Sun Oct 29, 2006 5:47 am

Post by oskare100 »

Hello,
feyd wrote:Why did all the OR's change to AND's?
Because I both keywords must match, not only one of them. I though that was the way to do that.. Isn't that correct?
volka wrote:'%kare%' LIKE 'oskare100' -> true
but you have: 'kare' LIKE '%oskare100%' -> false
I don't understand that, should I have something like '".%$item_title%."' instead or what do you mean? Should I add/remove something else?

Best Regards
Oskar R
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I mean you need to understand how LIKE works.
Please explain LIKE in your own words. See http://dev.mysql.com/doc/refman/5.1/en/ ... tions.html
oskare100
Forum Commoner
Posts: 80
Joined: Sun Oct 29, 2006 5:47 am

Post by oskare100 »

Hello,
OK, now I've really tried to fix this but I couldn't come longer than this;

Code: Select all

$item_amount="2.95";
$item_title="Templates, test, ebooks";

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$trimmed = trim($item_title, " \t.");
$keywords = explode(",", $trimmed);
echo $keywords;
echo $keywords[0];
echo $keywords[1];
echo $keywords[2];
echo $keywords[3];
And that echos this;

Code: Select all

ArrayTemplates test ebooks
So I guess that's good? Because it was from the beginning "Templates, test, ebooks" but now "Templates" "test" "ebooks" and from what I understand I needed to do that for the LIKE function to function as it should.

The thing I can figure out is how to get a working MySQL querey of that, I've tried but it doesn't work... Here is one, among many others, that I've tried with;

Code: Select all

"SELECT * FROM `items` WHERE ((`identify_pos` LIKE '%".$keywords[0]."%' OR '%".$keywords[1]."%' OR '%".$keywords[2]."%'OR '%".$keywords[3]."%' OR '%".$keywords[4]."%' OR '%".$keywords[5]."%') AND (`identify_pos2` LIKE '%".$keywords[0]."%' OR '%".$keywords[1]."%' OR '%".$keywords[2]."%'OR '%".$keywords[3]."%' OR '%".$keywords[4]."%' OR '%".$keywords[5]."%')) AND ((`identify_neg` NOT LIKE '%".$keywords[0]."%' OR '%".$keywords[1]."%' OR '%".$keywords[2]."%'OR '%".$keywords[3]."%' OR '%".$keywords[4]."%' OR '%".$keywords[5]."%') AND (`identify_neg2` NOT LIKE '%".$keywords[0]."%' OR '%".$keywords[1]."%' OR '%".$keywords[2]."%'OR '%".$keywords[3]."%' OR '%".$keywords[4]."%' OR '%".$keywords[5]."%'))"
So could you please help me with the querey? Or at least tell me what to do? I would ge really greatful if you could.. Thanks in advance,
Best Regards
Oskar R
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

volka wrote:Either add the appropriate % characters to your records or use e.g. INSTR, see http://dev.mysql.com/doc/refman/4.1/en/ ... tions.html

Code: Select all

<?php
$host = 'localhost';
$username = 'localuser';
$password = 'localpass';
$db_name = 'test';

$mysql = mysql_connect($host, $username, $password)or die(mysql_error());
mysql_select_db($db_name, $mysql)or die(mysql_error());

$item_amount='2.95';
$item_title= mysql_real_escape_string('Templates, test, ebooks', $mysql);
$query="SELECT
		*
	FROM
		`items`
	WHERE
		`price` = $item_amount
		AND ('$item_title' LIKE `identify_pos` AND '$item_title' LIKE `identify_pos2`)
		AND ('$item_title' NOT LIKE `identify_neg` AND '$item_title' NOT LIKE `identify_neg2`)
	";

echo 'Debug: ', $query, "</br >\n";
$result = mysql_query($query, $mysql) or die(mysql_error());
if ( false===($row=mysql_fetch_assoc($result)) ) {
	echo 'no such row', $query, "<br />\n";
}
else {
	echo 'item_id: ', $row['item_id'], "<br />\n";

Code: Select all

-- 
-- Dumping data for table `items`
-- 

INSERT INTO `items` (`item_id`, `item_name`, `price`, `identify_pos`, `identify_pos2`, `identify_neg`, `identify_neg2`, `file_id`, `pack_id`) VALUES
(1, '15GB package', '2.95', '%15gb%', '%templates%', '%test%', '', 0, 0),
(2, 'not 15gb package', '2.95', '%ebooks%', '%templates%', '%15gb%', '', 0, 0);
if the initial test `price` = $item_amount only leaves a handful of records this should be ok.
Post Reply