0

Hi guys i have been trying to rap my head around this for some time now. What I'm trying to achieve is get some data from my database, store it in a variable and compare it to a value that is stored in my array. Problem is it keeps returning the wrong output. From the SQL query below, the mysql_result $total_cat returns a value of 16. Once this value is stored, the code is meant to output echo "this value is in the array"; but its not working. Where am i going wrong?

This is my Code

Create an array to store my values in

$lunchbox = array(12,13,14,16,20,24,33,32);

Set up my SQL database query

$query = mysql_query("SELECT * FROM table WHERE id = '$the_persons_id'");

Catch my result and store it in a variable

$total_cat = mysql_result($query,0,"category_id");

Clean my result

$total_cat  = str_replace("|", " ", $total_cat);

Check if my sql result matches my any of my results stored in my array

if (in_array($total_cat, $lunchbox)) {
    echo "this value is in the array";
}else{
    echo "this value is not in the array";
Code
  • 438
  • 1
  • 5
  • 17
  • do you mean to say there is only one column in the table that has a value of 16 for your query parameters? curious since i see you are doing a `select *` – redDevil Jul 03 '14 at 19:03
  • how does *category_id* looks like in the database? Your "cleaning" makes me assume, it might look like 1|2|16 ... So, you would actually generate "1 2 16" by "cleaning" it, which is not in the array... – dognose Jul 03 '14 at 19:04
  • 2
    Sidenote: `$total_cat = str_replace("|", " ", $total_cat);` you're replacing `|` with a space and your array doesn't contain any spaces. In doing so, you'd either need to do `$total_cat = str_replace("|", "", $total_cat);` or use `trim()`. – Funk Forty Niner Jul 03 '14 at 19:04
  • There is more than one column in the table but I'm just testing the first value that i displayed on the screen – Code Jul 03 '14 at 19:05
  • the value prints out like this `9|16`. but im only worried about finding `16` – Code Jul 03 '14 at 19:07
  • @dognose yes that how the data gets displayed if i run `echo "$total_cat";` – Code Jul 03 '14 at 19:14
  • This Q&A http://stackoverflow.com/q/23270686/ might be of help and http://stackoverflow.com/q/23260372/ – Funk Forty Niner Jul 03 '14 at 19:16
  • However, you may have better luck using `IN` or `FIND_IN_SET()` directly from your query. – Funk Forty Niner Jul 03 '14 at 19:17
  • @Fred-ii- how would i write that query can you give me an example – Code Jul 03 '14 at 19:28
  • For `FIND_IN_SET()` see http://stackoverflow.com/q/23504764/ and http://stackoverflow.com/q/22480418/ and http://stackoverflow.com/q/19602637/ and http://stackoverflow.com/q/19462626/ and for `IN()` see http://www.tutorialspoint.com/mysql/mysql-in-clause.htm I hope this helps. – Funk Forty Niner Jul 03 '14 at 19:39
  • 1
    found the answer guys thanks for the help. All i needed to do was use php function substr so it would look like this `$total_cat = substr($total_cat, 1);` `echo "$total_cat";` and the result will display as 16 making my end result evaluate to true causing my value to be found in my array. :) – Code Jul 03 '14 at 19:54
  • Great, glad to hear it. You can post it as an answer yourself you know ;-) – Funk Forty Niner Jul 03 '14 at 19:56

1 Answers1

0

I found the answer.

This is my code:

Create an array to store my values in

$lunchbox = array(12,13,14,16,20,24,33,32);

Set up my SQL database query

$query = mysql_query("SELECT * FROM table WHERE id = '$the_persons_id'");

Catch my result and store it in a variable

$total_cat = mysql_result($query,0,"category_id");

Clean my result

$total_cat  = str_replace("|", " ", $total_cat);

This will output as 9 16

Now we run the php function substr to remove the first part of the string.

$total_cat = substr($total_cat, 1);

This will give us the output of 16

Now we check if my SQL result matches my any of my results stored in my array

if (in_array($total_cat, $lunchbox)) {
    echo "this value is in the array";
}else{
    echo "this value is not in the array";

Which it does and now my IF statement evaluates to TRUE and the code is complete.

Thanks for all the help team.

Happy Coding :)

Community
  • 1
  • 1
Code
  • 438
  • 1
  • 5
  • 17