So I got a database with keywords. The keywords is setup like so: &quot;PHP, HTML, CSS&quot;. I want to split the keywords up, and paste them into a select tag. I wrote the following code which does that: $query10 = &quot;SELECT * FROM projects&quot;;     if($result=mysqli_query($conn,$query10))         while ($row=mysqli_fetch_assoc($result)) {             $mark = explode(&quot;,&quot;, $row['projectKeywords']);                                                  foreach ($mark as $out) {                 ?&gt; &lt;option&gt;&lt;?php echo $out ?&gt;&lt;/option&gt; &lt;?php             }                                }    ?&gt;  It works fine, but I get duplicates. How do I delete does duplicates. I have tried with array_unique, but I couldn't figure it out. -Matteo
9730
How can I remove duplicate values from an array in PHP?
45432
I've asked the same question on , but got no answer, so that's why I'm asking it here (I've read that simple sql questions should be asked at SO, but I have no choice).  Suppose I have 3 tables: posts, post_categories and categories. I'm implementing some kind of page with filters where I can filter posts by categories. User can select multiple categories. When he selects more than one, they should be summed. I can't get it working. What I have now is simple IN() SQL clause:  SELECT posts.id, post_categories.category_id FROM posts  JOIN post_categories ON posts.id = post_categories.post_id  WHERE post_categories.category_id IN (1,2,3) LIMIT 10;   But it is not matching all ids, it is OR and I need AND. What I need here is to find all posts that have categories with id=1 AND id=2 AND id=3, but instead this query returns posts that have at least one category from the IN() list.
27089
I am looking for a solution that allows me to print the following:  Cat 1        Cat 2        Cat 3  Post 1       Post 1       Post 1  Post 2       Post 2       Post 2  Post 3                    Post 3                            Post 4   EDIT  I am looking for something that will only require one database query! So if you have a foreach in your code followed by a new WP_Query then that is not what I am looking for (I am planning to put this onto my website's homepage).
62823
I'm trying out the following query:  SELECT A,B,C FROM table WHERE field LIKE 'query%' UNION SELECT A,B,C FROM table WHERE field LIKE '%query' UNION SELECT A,B,C FROM table WHERE field LIKE '%query%' GROUP BY B ORDER BY B ASC LIMIT 5   That's three queries stuck together, kinda sorta. However, the result set that comes back reflects results from query #3 before the results from query #1 which is undesired.  Is there any way to prioritize these so that results come as all for query #1, then all for query #2 then all for query #3? I don't want to do this in PHP just yet (not to mention having to control for results that showed up in the first query not to show in the second and so forth).
41532
I have information spread out across a few databases and want to put all the information onto one webpage using PHP.  I was wondering how I can connect to multiple databases on a single PHP webpage.  I know how to connect to a single database using:  $dbh = mysql_connect($hostname, $username, $password)          or die("Unable to connect to MySQL");   However, can I just use multiple "mysql_connect" commands to open the other databases, and how would PHP know what database I want the information pulled from if I do have multiple databases connected.
31232
I used the following code to give out the category name (without link) in a single post.  How would it be possible for the category name that is given out to only appears in small letters ?  I just think this has to be made with substring, but I dont get it :) Could somebody please help me :)  &lt;?php foreach((get_the_category()) as $category) { echo $category-&gt;cat_name . ' '; } ?&gt;
77724
I would like to write an SQL query that searches for a keyword in a text field, but only if it is a "whole word match" (e.g. when I search for "rid", it should not match "arid", but it should match "a rid".  I am using MySQL.  Fortunately, performance is not critical in this application, and the database size and string size are both comfortably small, but I would prefer to do it in the SQL than in the PHP driving it.
39428
I'm looking to find records in a table that match a specific number that the user enters.  So, the user may enter 12345, but this could be 123zz4-5 in the database. I imagine something like this would work, if PHP functions worked in MySQL. SELECT * FROM foo WHERE preg_replace(&quot;/[^0-9]/&quot;,&quot;&quot;,bar) = '12345'  What's the equivalent function or way to do this with just MySQL? Speed is not important.
74148
Does anyone know of a program, a utility, or some programmatic library, preferably for Linux, that takes an unformatted SQL string and pretty prints it?  For example I would like the following  select * from users where name = 'Paul'   be changed to something like this  select *  from users where    name = 'Paul'   The exact formatting is not important. I just need something to take a large SQL string and break it up into something more readable.
37749
I am working on a project involving cards from the Magic: The Gathering game. To be of any use, I must load the basic information about each card into my database, but some of the fields may contain arrays of text, that is, they may contain multiple values .  For example, a card can be classified to be both "green" and "blue". Or it can have several different creature types specified, and so on.  My first thought was to simply store the values as a string, and then search using the LIKE keyword. In combination with the NOT keyword, it should be possible to search for any desired combination of words, and also exclude certain words.   However, LIKE is going to be (relatively) slow as it can't use an index and has to rely on text pattern matching, and probably has other drawbacks as well. With normalization I could improve speed by assigning an integer key to each keyword, and then create a table that connects each card with the relevant keys (see  for a clearer description).  But with normalized tables I have to deals with joins (severely increased code complexity, not only in the SQL part), transactions (to update all relevant tables or rollback everything), and possibly decreased performance due to joins (see ).  AFAIK there are roughly 20000-25000 different magic cards in existence (and more being released every year). With one row for each card, is it worth it overall to normalize the data, or is the benefit not worth the costs? Why/Why not? And how do you decide this question in general?  EDIT: I am currently using a MySQL database, but I would also appreciate answers that apply to any of the other non-commercial/open source databases out there. Who knows - I might switch to a different database at some point.  Answers should not provide a technical solution only, but explain why it would/might be the right solution in terms of best/good enough performance for least amount of effort.
90821
If I have a loop running from a category query like :  &lt;?php $the_query = new WP_Query('cat=1&amp;showposts=50&amp;orderby=title&amp;order=asc');?&gt; &lt;ul&gt; &lt;?php while ($the_query-&gt;have_posts()) : $the_query-&gt;the_post();?&gt; &lt;li&gt;.. &lt;/li&gt;&lt;?php wp_reset_query(); ?&gt; &lt;?php endwhile; ?&gt; &lt;/ul&gt;   How would I create an if clause that breaks the list at a certain interval, and starts a new one. So for example at the 10th post, return a &lt;/ul&gt; and start a new one &lt;ul&gt; at 11.  This is incorrect but to illustrate my objective:  &lt;?php $count =0;     while($count &lt;=50){         if ($count == 9){             echo "&lt;li&gt;&lt;a href='&lt;?php the_permalink(); ?&gt;'&gt;                       &lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;";             }          elseif ($count == 10){         echo "&lt;ul&gt;&lt;li&gt;&lt;a href='&lt;?php the_permalink(); ?&gt;'&gt;                           &lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;";         }         else {         echo "&lt;li&gt;&lt;a href='&lt;?php the_permalink(); ?&gt;'&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;";         }   What is the correct way to include this logic into the loop?
28523
Here's a PHP example of mine. Can anyone find a shorter/easier way to do this?  &lt;? foreach($posts as $post){?&gt;     &lt;div class="&lt;?=($c++%2==1)?‘odd’:NULL?&gt;"&gt;         &lt;?=$post?&gt;     &lt;/div&gt; &lt;? }?&gt;  &lt;style&gt;     .odd{background-color:red;} &lt;/style&gt;   Examples in other languages would be fun to see as well.
12526
Would you recommend using a  or a  field, and why (using MySQL)?   I'm working with PHP on the server side.
55771
Anyone know how to combine PHP prepared statements with LIKE? i.e.   "SELECT * FROM table WHERE name LIKE %?%";
7196
Is there an easy way to run a MySQL query from the Linux command line and output the results in  format?  Here's what I'm doing now:  mysql -u uid -ppwd -D dbname &lt;&lt; EOQ | sed -e 's/        /,/g' | tee list.csv select id, concat("\"",name,"\"") as name from students EOQ   It gets messy when there are a lot of columns that need to be surrounded by quotes, or if there are quotes in the results that need to be escaped.
98204
During my work with databases I noticed that I write query strings and in this strings I have to put several restrictions in the where-clause from a list/array/collection. Should look like this:  select * from customer  where customer.id in (34, 26, ..., 2);   You can simplify this by reducing this to the question that you have collection of strings and want to create a comma-separated list of this strings in just one string.  My approach I have used so far is something like that:  String result = ""; boolean first = true; for(String string : collectionOfStrings) {     if(first) {         result+=string;         first=false;     } else {         result+=","+string;     } }   But this is as you can see very ugly. You cannot see what happens there on the first look, especially when the constructed strings (like every SQL query) is getting complicated.  What is your (more) elegant way?
31050
I need to run a query like:  SELECT p.id, p.name,         (SELECT name            FROM sites s           WHERE s.id = p.site_id) AS site_list   FROM publications p   But I'd like the sub-select to return a comma separated list, instead of a column of data.  Is this even possible, and if so, how?