i have a table in mysql and i am fetching the results from this table. but instead of fetching all the rows in this table i only want to fetch every other row. so first get row one then skip the second row, get row 3 and skip row 4 etc.
Is there a way of doing this and if so can someone please show me how.
I've tried this:
function:
function blocked_users_list() {
global $connection;
global $_SESSION;
global $profile_id;
$query = "SELECT
baseview.*
#odd:=1-#odd AS even
FROM
(
SELECT *
FROM ptb_block_user
WHERE
WHERE ptb_block_user.blocked = '1'
AND ptb_block_user.blocked_id = ".$_SESSION['user_id']."
) AS baseview,
(
SELECT #odd:=0
) AS filter
WHERE
even=1
";
$blocked_users_list = mysql_query($query, $connection);
confirm_query($query, $connection);
return $blocked_users_list;
}
php:
<?php
$blocked_users_list = blocked_users_list();
while ($block = mysql_fetch_array($blocked_users_list)) {
?>
but it gives this error:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/ptb1/blocked_users.php on line 44
do it in the query:
SELECT *
FROM (
SELECT
#row := #row +1 AS rownum, [column name]
FROM (
SELECT #row :=0) r, [table name]
) ranked
WHERE rownum % [n] = 1
You can use modulus in mysql (one query)
select * from `table` where `id` % 2 = 1
Retrieves all odd IDs.
Assuming you have a query like
SELECT
col1 AS colA,
col2 AS colB
FROM
sometable
WHERE
something=17
that fetches all rows as a baseline. You can then filter every second row by using
SELECT
baseview.*
#odd:=1-#odd AS even
FROM
(
SELECT
col1 AS colA,
col2 AS colB
FROM
sometable
WHERE
something=17
) AS baseview,
(
SELECT #odd:=0
) AS filter
WHERE
even=1
Use a variable to record the index, then check with mod 2 ($ind % 2) - this will return either 0 or 1.
<?php
$users_list = users_list();
$ind = 0;
while ($user = mysql_fetch_array($users_list)) {
if(($ind++)%2) echo "something";
}
?>
Related
I was wondering how I can check whether users are already in the database or not.
In PHP I have an array with some UserIDs.
i.e. userIDs[0] = 1234; userIDs[1] = 2345;
Now I wanted to build a query to make just one sql call if possible to get following result:
############################
# UserID # Exists #
############################
# 1234 # 0 #
# 2345 # 1 #
############################
Is there a sql solution or do I have to check each ID with a seperate call?
Thank you for your help!
To check existing ids against a list you can do
select userId, userId in (2345,5678) as `exists`
from your_table
order by userId
But to check if a list of ids is in the DB you can do
select tmp.userId, tab.userId is not null as `exists`
from
(
select 1234 as userId
union all
select 2345
union all
select 3456
) tmp
left join db_table tab on tab.userId = tmp.userId
x:
you build up your query in PHP which is string.
$idsFromFacebook = array(1234,2345,3456,4567);
//start query
$strQuery = "select tmp.userId, tab.userId is not null as `exists` from (";
$bfirstRun = true;
//Go through each element of the array and connect them with the query string
foreach( $idsFromFacebook as $userID )
{
//There is no union all before the first element and after the last
if ( !$bFirstRun )
$strQuery .= " union all ";
$strQuery .= "select ".$userID;
if ( $bFirstRun )
{
//"as userId" is only needed the first time
$strQuery .= " as userId";
$bFirstRun = False
}
}
//Finishing the query by joining the generated table with database
$strQuery .= ") tmp left join db_table tab on tab.userId = tmp.userId;";
//Call Query
$result = $dbms->query($strQuery);
//...
That is pretty much it. Because the query is a string, you can build it up just like you want to have it :)
I'm trying to select something from a table and insert that information into another table
For example I've 3 rows in table A which I want to insert into table B, he only inserts 2 of the 3.
I got this: I've tried it with fetch_array() but I get only the error non-object
EDIT: THE PART OF THE SCRIPT
$log = $db->query("SELECT itemname FROM log_mitem WHERE mobname = '".$mobname."' AND game = '".$game."'") or die($db->error);
if($log1 = $log->fetch_object());
{
while($loco = $log->fetch_object())
{
You shouldn't have that first if, just do:
$log = $db->query("SELECT itemname FROM log_mitem WHERE mobname = '".$mobname."' AND game = '".$game."'") or die($db->error);
while($loco = $log->fetch_object()) {
// do something
}
Also note that you don't need a while loop for this trivial task, you can use INSERT INTO ... SELECT syntax
INSERT INTO table1 ( column1 )
SELECT col1
FROM table2
WHERE cond1
Im trying to find a better way to return 2 tables at once.
My first table is:
[ID] [area]
1 13,12,15
6 18,17,13
and the second table is:
[areaname] [singlearea]
textOf12 12
textOf18 18
textOf15 15
Now, I need to return for each [ID] hits area names, for example:
For the ID: 1, I need the following array: (textOf12,textOf15)
and for the ID 6 I need: (textOf18) only.
This is what i have for now (I don't think its a nice code):
$getall = "SELECT * FROM table1";
$resultfull = mysql_query($getall);
while ($res = mysql_fetch_assoc($resultfull))
{
$uarray = array();
$sqlarea = explode(",", $res['area']);
foreach($sqlarea as $userarea)
{
$areaarray = runquery("SELECT areaname From table2 WHERE singlearea = '".$userarea."'");
$value = mysql_fetch_object($areaarray);
array_push($uarray,$value->areaname);
}
var_dump($uarray);
any suggestions?
Thank you very much!
Comma separated ID list and ID value pretty good matching using like:
select t1.id, t2.areaname
from table1 t1, table2 t2
where concat(',', t1.area, ',') like concat('%,', t2.singlearea, ',%')
However It's recommended to use additional link table!
How can I return the 1 or 0 as true and false for my select query? I've tried every combination of fetch_assoc, arrays, etc. It usually returns as null, which doesn't make sense.
$querydetails = "select exists (select * from customer_det where id = $uid)";
$resultdetails = mysql_query($querydetails) or die(mysql_error());
while ($row = mysql_fetch_assoc($resultdetails)) {
$resultdetails1 = $row[0];
}
That's the latest version of my query. I guess it's not an array, but I'm not sure how to pull the value if there's no row to be named?
This is a post question to my previous thread: MySQL Update WHERE
SELECT IF(EXISTS(...),1,0) AS result
simple use COUNT and IF
SELECT IF(COUNT(*) = 0, 0, 1)
FROM table1
WHERE s = 2
SQLFiddle Demo
SELECT count(*) FROM (
SELECT id FROM table WHERE condition LIMIT 1
) AS result;
i am using mysql version 8.0.23 on AMS
SELECT
column1,
column2,
IF (exists(select * from MeasurementItemNos measureItem where measureItem.itemNo = ib.itemNo and measureItem.IsActive = 1),1,0) as isUploadedSizeSpec
from table
I would do:
select * from customer_det where id = $uid LIMIT 1
You will get either 1 row or zero rows.
I'm trying to show only every 2nd row after selecting something from my DB.
My select looks like this:
SELECT * FROM table where partnerID = '1'
Now from the results of that select query I want to get only every 2nd row.
Is that possible ?
$count = 0;
while($row = mysql_fetch_array($results)) {
$count++;
if($count % 2 == 1) continue;
// What you want to do with the rows you don't want to skip here...
}
On a side note, you can use this same strategy to only show every 3rd row, or every 4th row, simply by changing what number you put next to the modulus operator
if($count % 3 != 0) continue; // Show only every third row
if($count % 4 != 0) continue; // Show only every fourth row
SELECT * FROM (
SELECT #n := #n + 1 AS position,
t.*
FROM (SELECT #n:=0) counter,
table t
WHERE partnerID = 1)
AS query
WHERE MOD(position,2) = 0
You can do this in the following way:
SELECT * FROM
(
SELECT
#I := #I + 1 AS rowNumber,
tablename.*
FROM
tablename,
(SELECT #I := 0) VT1
WHERE partnerID = 2
ORDER BY ID
) NumberedRows
WHERE MOD(rowNumber, 2)=0
;
Not sure if this is the right answer, but it looks like you would need to use something like MOD function (http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_mod)
SELECT * FROM `table` WHERE id & 1;
Would return odd rows