I want to extract up to five values from an array and place them in a msql query as such:
$frontpage_hot_list_data = array();
while (#$row = mysql_fetch_array($sql_frontpage_hot_list)) {
$frontpage_hot_list_data[] = $row['id'];
}
$sql_frontpage_hot_down = mysql_query("SELECT * FROM submissions WHERE
id !='$frontpage_hot_list_data[0]' AND id !='$frontpage_hot_list_data[1]' AND
id !='$frontpage_hot_list_data[2]' AND id !='$frontpage_hot_list_data[3]' AND
id !='$frontpage_hot_list_data[4]' AND thumbnail_large=0 AND popular=1 AND
popular_datetime > '$frontpage_hot_hot_three_onlineNowTm' AND
views > '$frontpage_hot_hot_three_views' ORDER BY views DESC LIMIT 4");
The problem here appears to be when I have less than five values, I get the following error:
Notice: Undefined offset: 1 in
D:\Hosting\8847501\html\scripts\timeframes.php on line 298
Notice: Undefined offset: 2 in
D:\Hosting\8847501\html\scripts\timeframes.php on line 299
Notice: Undefined offset: 3 in
D:\Hosting\8847501\html\scripts\timeframes.php on line 300
Notice: Undefined offset: 4 in
D:\Hosting\8847501\html\scripts\timeframes.php on line 301
Any idea how to solve this problem? Maybe placing in the query only the exact number of variables? Im lost...
You can use implode to create a comma-separated list of your IDs, then put this newly created list into a MySQL IN expression (UPDATE: now I see that you're using != in your query, so let's make it NOT IN).
$list = implode(',', $frontpage_hot_list_data);
$sql_frontpage_hot_down = mysql_query("SELECT * FROM submissions WHERE
id NOT IN ($list) AND thumbnail_large=0 AND popular=1 AND
popular_datetime > '$frontpage_hot_hot_three_onlineNowTm' AND
views > '$frontpage_hot_hot_three_views' ORDER BY views DESC LIMIT 4");
Important note: I assumed here that your ids are numeric, so implode() would produce something like 1,5,133,31. If they are strings, then you have to wrap them in apostrophes first using array_map() for example.
Also, if you expect that the array can be empty, you can add another condition that will omit the whole id NOT IN () AND part when necessary.
It seems that there's no $sql_frontpage_hot_list result, because array $frontpage_hot_list_data[] does not contain any of results
I would do following:
$frontpage_hot_list_data = array();
while (#$row = mysql_fetch_array($sql_frontpage_hot_list)) {
$frontpage_hot_list_data[] = $row['id'];
}
if(count($frontpage_hot_list_data))
{
$frontpage_ids_str = implode($frontpage_hot_list_data, ',');
$sql_frontpage_hot_down = mysql_query("SELECT * FROM submissions WHERE
id not in (".$frontpage_ids_str.") AND thumbnail_large=0 AND popular=1 AND
popular_datetime > '$frontpage_hot_hot_three_onlineNowTm' AND
views > '$frontpage_hot_hot_three_views' ORDER BY views DESC LIMIT 4");
}
else
{
$sql_frontpage_hot_down = mysql_query("SELECT * FROM submissions WHERE
thumbnail_large=0 AND popular=1 AND
popular_datetime > '$frontpage_hot_hot_three_onlineNowTm' AND
views > '$frontpage_hot_hot_three_views' ORDER BY views DESC LIMIT 4");
}
So if we have $frontpage_hot_list_data we exclude them, if not then we ignore them.
Related
while ($categories = mysql_fetch_array($category_result)) {
$item_result = mysql_query("SELECT * FROM zw_".$categories['Category']."s WHERE ItemId = '".$categories['Id']."' ORDER BY Level");
$item = mysql_fetch_assoc($item_result);
echo $item['Level'];
The output of this is
15
35
55
75
95
115
135
150
15
27
48
68
83
11
40
62
80
95
110
125
I cant really find a pattern here. I want it to go from the lowest number to highest number, which ORDER BY is supposed to do, right? Level is always INT and this is inside a while loop.
Check this
while ($categories = mysql_fetch_array($category_result)) {
$item_result = mysql_query("SELECT * FROM zw_".$categories['Category']."s WHERE ItemId = '".$categories['Id']."' ORDER BY Level ASC");
$item = mysql_fetch_assoc($item_result);
echo $item['Level'];
Try iterate over the result of the inner query .. eg:
while ($categories = mysql_fetch_array($category_result)) {
$item_result = mysql_query("SELECT * FROM zw_".$categories['Category']."s WHERE ItemId = '".$categories['Id']."' ORDER BY Level");
wwhile($item = mysql_fetch_assoc($item_result)) {
echo 'Category : ' . $categories['Id'] . ' Level : ' . $item['Level'];
}
}
I think you are constructing the table name dynamically, using data you have read from some other table (or maybe elsewhere). Hence, you want records from a collection of separate tables, and then to sort the complete set.
Perhaps you could build your SQL to create a "UNION" for each unique table, so the query runs only once and does its sorting on the entire result set. Possibly, a union clause (OK, the first clause is not introduced with "UNION")!) for each ($categories['Category'], $categories['Id']) combination will be too much for your RDBMS to handle. So, you could stick to one clause per $categories['Category'] and build all the associated $categories['Id']s into an "IN" condition.
Worth considering whether it's better to stick with what you have, and write some code to sort the results you're getting.
The field in database is numeric ? Try to specify the type of sort,... order by Level asc for example.
Checkout http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html
PHP code defining variable sqlshowvalue
$sqlshowvalue = 5;
if(isset($_POST['showmore'])) {
$sqlshowvalue += 5;
}
So I connect to my database and then when I run this SQL query below using the variable that I just defined above,
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit '$sqlshowvalue'");
So I am using mysqli as the method to connect to my DB and it gives me the following error:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in ..
The reason it gives me this error is because something in my query is wrong and what it has to do is $sqlshowvalue, because if I replace sqlshowvalue with with just the number 5 (like shown below), it works fine:
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit 5");
So I am just wondering what I can do to make it so that the value for the limit is a PHP variable that I can be changed and the page updated.
Have you tried making
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit '$sqlshowvalue'");
to
$result = mysqli_query($conn,"SELECT * FROM comments ORDER BY id DESC limit ".$sqlshowvalue);
Remove '' use only $sqlshowvalue:-
$result = mysqli_query($conn,"SELECT * FROM comments
ORDER BY id DESC limit $sqlshowvalue");
for integer value no need of ''
I'm trying to do 2 things.
1) Get the amount of rows in this query
SELECT
COUNT(*)
FROM
`my_table`
WHERE
`column_1` = 152
AND
`column_2` = 42
ORDER BY
`column_3`
As you can see that is no problem ^^
2) Determine the number within the range of rows that is returned by id
Ex: ID 765 is Item 4 of 7 where column_1 = 152 and column_3 = 42
Does anyone have any basic solutions to this problem with almost pure MySQL? I'd like to avoid iterating through all the rows and setup a counter to increment until it matches current id like this:
$sql = '
SELECT
*
FROM
`my_table`
WHERE
`column_1` = 152
AND
`column_2` = 42
ORDER BY
`column_3`
';
$query = mysqli_query($sql);
$current_id = 2523;
$i = 1;
while ($row = mysqli_fetch_assoc($query)) {
if ($row['id'] == $current_id) {
$current_position = $i;
}
$i++;
}
print 'Current position in range is: '. $current_position;
Also please don't worry about the actual syntax, I won't be using this exact script, but you get the logic that I'd like to avoid using. If anyone has a better solution, please let me know. Thanks in advance!!
I'm trying really hard to figure out why information from a database won't show up. I think my query is failing, but I've talked to a few people and they don't know why it's not working.
Here's my code leading up to the query:
$getnum = mysql_query("SELECT * FROM articles ORDER BY artnum DESC LIMIT 1");
while($getnumrow = mysql_fetch_array($getnum))
{
$theartnum=$getnumrow['artnum'];
}
$pagenum=intval($_GET['pg']);
if($pagenum==0 || !isset($pagenum))
{
$pagenum=1;
}
$offsetnum = $theartnum-($pagenum*15)+15;
echo("SELECT * FROM articles ORDER BY artnum DESC LIMIT 15 OFFSET $offsetnum");
$result=mysql_query("SELECT * FROM articles ORDER BY artnum DESC LIMIT 15 OFFSET $offsetnum");
I've went over the variables, and all seem to be working. Echoing the query, I get:
SELECT * FROM articles ORDER BY artnum DESC LIMIT 15 OFFSET 85
Which should work, since I've checked and the amount in articles is 85.
Much later in the code, I have:
while($row = mysql_fetch_array($result))
{
$art_title=$row['art_title'];
$art_title_url=$row['art_title_url'];
$art_author=$row['art_author'];
$art_date=$row['art_date'];
$artnumber=$row['artnum'];
$desc=implode(' ', array_slice(explode(' ', $row['article']), 0, 14))."...";
echo "<div class=\"big\">".$art_title."</div>
<div class=\"small\">".$art_date." — <a title=\"View more by ".$art_author."\" href=\"author.php?a=".$art_author."\">".$art_author."</a></div>
<span class=\"article\">".$desc."</span><br /><br />";
}
If I put
echo "test";
in there, I don't get that either.
My whole code is here:
http://pastebin.com/RUpb0tUG
(note: It's not complete, I'm still working on the previous/next buttons and I can do that fairly easily)
I'm testing it here until it works.
Thanks!
SELECT * FROM articles ORDER BY artnum DESC LIMIT 15 OFFSET 85
Which should work, since I've checked and the amount in articles is
85.
Aha, if you have 85 records in your table, what do you expect to get from the above select from row number 86 to 100 other than an empty result set ??
Any chance the problem lies in your "header.php" include file containing code that uses the $result variable or another variable that will screw up your later code?
I tried to achieve a method in which I create a scoreboard (while loop) and order the fetched results by a certain numeric field (points). But what I need to achieve is like the following
rank----username--point
1st------test-----------3200
2nd-----test2---------1200
etc.. I paginate my results and limit 25 results per page. I tried a while loop in the following way
while($row = mysql_fetch_array($query) || $i<=$totalnumberofrows ){
echo out the results
$i++
}
What this does is number from 1 to 25 perfectly, but on the other hand, on page 2 it numbers again from 1-25.
Is there a trick to achieve what I need to achieve?
I.e. continuous numbering from 1 to (total number of rows) even when paginated.
What you're looking for is the MySQL LIMIT statement. For example, the following query would return entry 0-24 (so, the first 25 entries in your database):
SELECT * FROM entries ORDER BY `points` LIMIT 0, 25
Say that you want to fetch the second page (the next 25 entries), you can do:
SELECT * FROM entries ORDER BY `points` LIMIT 25, 25
Most important thing to note here is that the first argument is the row where it should start, and the second argument is not the last row, but the total amount of rows it should return.
To determine the starting point, simply do (($page_number - 1) * 25), assuming your first page is numbered 1. You could for example do the following:
<?php
$start = (($page_number - 1) * 25);
$query = "SELECT * FROM entries ORDER BY `points` LIMIT {$start}, 25";
// ... rest of your code goes here ...
?>
just add 25 to the number for each page,
for the second page (in my example) - add 25
for the third page - add 50
etc...
$page = 2;
while($row = mysql_fetch_array($query) || $i<=$totalnumberofrows ){
echo out the results
echo (($page-1)*$totalnumberofrows)+$i;
$i++
}
If you know the page number and the offset you should be able to do the following
$rank = $offset * $page_number;