Jump to content

[SOLVED] Querying MySQL with an Array


Gorkfu

Recommended Posts

I'm sure this is possible, but I am struggling to figure it out. I want to use an array's values to search MySQL records. I figured maybe using regexp in MySQL would do the trick but I'm not familiar with regexp usage in MySQL.

 

I want to accomplish this with only one query if possible.

 

Here's a rough idea of what I'm trying to accomplish.

 

Array ( [0] => Item0 [1] => Item1 [2] => Item2 [3] => Item3 [4] => Item4. )

 

$result = $mysqli->query("SELECT `name` FROM `table1` WHERE `name` = '$array1' ORDER BY CHAR_LENGTH(name) DESC");

 

Any ideas?

 

Update:

I just realized sending an array wouldn't work and that I'll need to recombine it into a regular string. Thing is I would need MySQL to search each word or phrase independently.

Link to comment
Share on other sites

Well I figured it out with some php, although I'll have to tweak it a bit further with other things, but this is basically what I was going for...

 

$query = "SELECT `name` FROM `table1` WHERE ";
for($i = 0; $i < count($array1); $i++) {
	$query .= "`name` = '".trim($array1[$i])."'";
		if ($i < count($array1)-1) {
			$query .= " OR ";
		}
}
}
$query .= " ORDER BY CHAR_LENGTH(name) DESC";

Link to comment
Share on other sites

You could of just used implode

 

$query = "SELECT `name` FROM `table1` WHERE `name` IN('". implode("','", $array1) . "')  ORDER BY CHAR_LENGTH(name) DESC";

 

Which will produce this query:

SELECT `name` FROM `table1` WHERE `name` IN('Item0','Item1', etc)  ORDER BY CHAR_LENGTH(name) DESC

 

Using IN() instead name='name1' ORr name='name2' etc is more efficient.

Link to comment
Share on other sites

Thanks for that method Barand, but I tried a similar approach before and the the problem with that method is it uses a lot more memory than my original method. I'm still wondering if it is even possible to do the trim alongside with an IN in MySQL? It looks like you can only use one or the other in a query.

Link to comment
Share on other sites

Well I shrunk down my original code for now till I figure out something better.

 

$query = "SELECT `name` FROM `table1` WHERE `name` IN(";
for($i = 0; $i < count($array1); $i++) {
	$query .= "'".trim($array1[$i])."', ";
}
$query .= " 'other term') ORDER BY CHAR_LENGTH(name) DESC";

Link to comment
Share on other sites

Your could use array_map instead

 

Oh pure genius, thank you wildteen88.  ;D It works so much faster now and less memory usage it seems. I learned a lot here too.  ;)

 

Although one thing confuses me dealing with the php alternative I was using. The php was leaving out the last array value from the MySQL Query some of the time. Your method doesn't have this issue at all.

Link to comment
Share on other sites

This is due to the comparison operator you are using here

for($i = 0; $i < count($array1); $i++) {

 

the < means less than.  Say your array contained 3 items.

 

The for loop will only iterate 2 times not 3. In order for it to iterate 3 times you'll need to use the less than or equal to operator (<=).

 

EDIT: Ignore me.

Link to comment
Share on other sites

Lol, you must of been thinking $i = 1 at first. Don't feel embarrassed, I actually confused myself at one point and thought the same thing. Lack of sleep does evil things to the brain.  ;D

 

That's why it confused me, when I printed the array results it showed all the items correctly in the array but then when I would go and print the MySQL query it showed them all but the last array value and this behavior didn't even happen on every page. (Dynamic results.) Most likely the problem was right under my nose in some other code.

 

So I dunno, I'm not gonna worry about it, your solution works much better anyways. So one of you guys can mark this solved if you want. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.