Hello forums I’m trying to put a query result to an array.
I have a table in my mysql database with the following data :
1,6,7
$q = mysql_query("SELECT bookmark FROM table WHERE id = 1 ");
$r = mysql_fetch_array($q);
$bookmark = array($r);
if (in_array(1, $r)) {
echo 'true';
}else{
echo "false";
}
This one keeps echoing false. Any ideas? Thanks
Jaanboy:
^ This
Well yeah I just created another table for bookmarks and just did a left join
What you really need to do is normalize your database
coolR
July 6, 2010, 2:25pm
6
stonedeft:
Nevermind I got it I just need to explode the result
$w = explode(",",$r['bookmarks']);
if (in_array(12, $w)) {
echo "true";
}else{
echo "false";
}
<–echos false
Thanks
if (in_array(12, $w))<– may give you correct results but logically 12 is an int but from mysql it becomes string so best practice is if (in_array(“12”, $w))
Nevermind I got it I just need to explode the result
$w = explode(",",$r['bookmarks']);
if (in_array(12, $w)) {
echo "true";
}else{
echo "false";
}
<–echos false
Thanks
coolR
July 6, 2010, 2:08pm
8
It should work if bookmark for id=1 is ‘1,6,2’
$q = mysql_query("SELECT bookmark FROM table WHERE id = 1 ");
$r = mysql_fetch_array($q);
$r = explode(",",$r[0]);
if (in_array("1", $r)) {
echo 'true';
}else{
echo "false";
}
I did this test without the query stuff and it’s working
$w = array("1","6","2");
if (in_array(6, $w)) {
echo "true";
}else{
echo "false";
}
<– echos true. So how do I make the mysql result to display like :
$r =array(“1”,“6”,“2”);
Do I need to split it?
Nope I am not asking mysql to display id only bookmarks which is 1,6,7
I print_r-ed the result and it gives me this:
Array ( [0] => 1,6,2 [bookmarks] => 1,6,2 ) so it is an array
when I run the in_array() function for 1 it echoes true, but when I run in_array() for 6 and 2 it displays false. bump.
if (in_array(1, $r)) {
echo 'true';
}else{
echo "false";
}
echos true
if (in_array(2, $r)) {
echo 'true';
}else{
echo "false";
}
echos false… :sick:
An easy way to debug this would have been to simply do:
print_r ($r);
to show what was in the array.
You are not asking MySQL to SELECT id but you are asking to display id in the results.
Try this:
$q = mysql_query("SELECT id, bookmark FROM table WHERE id = 1 ");
Also insert these two lines, the results are usually very informative.
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', LOCALHOST ? 'On' : 'Off');
.