MySQL PHP Grabbing rows not relevant to query - php

I have no clue what I could be doing wrong. This has only ever happened with the Change logs that I've been trying to develop. I coded this from scratch, and it really couldn't be much simpler.
For some reason when I try to grab rows that are only
is_dev='1' (Is in development)
OR
planned='1' (Is planned)
AND
website_id='13' (ID of website)
It shows a result from another website ID ("9").
This is my current query:
**$getdev = mysql_query("SELECT * FROM changelog_entries WHERE in_dev='1' OR planned='1' AND website_id='13' ORDER BY id DESC");**
This is what I get in return, I wrote on the picture what website id they belong to.
https://img.rnjrweb.com/errorchangelogs.PNG
I've also tried this query:
**$getdev = mysql_query("SELECT * FROM changelog_entries WHERE website_id='13' AND in_dev='1' OR planned='1' ORDER BY id DESC");**
and when I do, it returns even more irrelevant rows.
Any clues? I'm pretty stuck here. Thank you!

You need parenthesis around your OR:
SELECT * FROM changelog_entries WHERE (in_dev='1' OR planned='1') AND website_id='13' ORDER BY id DESC
If you don't put them any website_id row having in_dev=1 will match.

Related

php update record based on fetch

[UPDATE: found solution. see my own answer, below]
i'm trying to learn php and i can't figure how to do something that seems extremely basic, i.e. update a record based on fetching criteria. here's the part of my code that successfully fetches the record:
mysql_select_db('top_choice_system');
$query = "SELECT item_name FROM main ORDER BY cw DESC LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo "-- item name --" . $row['item_name'];
how do i update the specifically fetched item?
ftr below is the code i successfully use to update a record. however, in the code below, i specify the record myself in the WHERE portion:
$sql = 'UPDATE main
SET column5=28
WHERE ITEM=15';
my point is i can't figure how to make the WHERE match the fetched record. (or, better yet, how to fetch AND update with the simplest, shortest method.) thank you in advance for any help.
rephrasing my question: i'm learning several things from online tutorials, but for some reason i can't find the most basic info about updating a record but NOT based the record's id: for example, you want to update the specific record that matches a specific criteria. (as in the example above, wanting to update whichever record will be at the top of a specific column if that specific column is ordered from top to bottom.)
update main set column5 =
(SELECT item_name FROM main ORDER BY CW DESC)
you can use query like this update and select together your question is not cleared what data you want and which data you want to update so you can search about update select query and make your query as per your requirement....
EUREKA:
$sql = 'UPDATE main
SET testfield="YES" ORDER BY columnX DESC LIMIT 1';
note:the ORDER BY condition did not work with a WHERE line. it worked when i added it to the SET line.
(ftr found code on http://www.phpknowhow.com/mysql/update-statement/)

MySQL remember last position

I have made a small app where a table of data is presented to the user. The data can be sorted by different column headers and filter using inputs.
When the user clicks on a row it opens a small popup which contains two arrows for going to the next and previous record, in the same order as they appear in the table.
Originally I had (for "previous" as an e.g.):
SELECT ed.id
FROM entity_details AS ed, users
WHERE ed.id > ?
AND ed.typeRef = ?
AND ed.ownerRef = users.id
$filter
$SQLOrder LIMIT 1
Which works fine if the table is sorted by ed.id, but will not work properly if sorted by another column e.g. ed.name, because the next alphabetical name might have a much higher or lower id.
FYI $filter could be something like:
AND branchRef = 2
and $SQLOrder could be:
ORDER BY ed.name DESC
What do I need to do to make it cycle through the records properly, respecting current order and record position?
All the sorting and filtering parameters come through over AJAX, e.g:
$JSON->selectedbranch ;
I've come to the conclusion that all I need to know is how to start the query from a row with column X containing value Y, is that possible?
You should store the number of the row you displayed, not the ID. Then just do the ordering in SQL as your application requirements imply, then apply the knowledge contained here:
Skipping first n results in MySQL
To simplify the job, and to make this answer usable for future SO dwellers:
SELECT ed.id
FROM entity_details AS ed, users
WHERE ed.typeRef = ?
AND ed.ownerRef = users.id
$filter
$SQLOrder
LIMIT $currentRowNum,1
This scheme smells however: using this to navigate your rows implies a SQL query for each navigation action. That might have an bad effect on your response time...
PHP's mysql_data_seek function may helps.
mysql_data_seek
I found it, seeing that other guys answer gave me an idea, but his answer has disappeared :(
For the next button I have:
$result = $dbh->prepare("SELECT ed.id
FROM entity_details AS ed, users
WHERE $WHERE < ?
AND ed.typeRef = ?
AND ed.ownerRef = users.id
$filter
ORDER BY ed.name DESC LIMIT 1") ;
$WHERE is just the column name "ed.name".
I just have to sort out the dynamics for $where and the ORDER BY clause and it'll be good to go.
Thanks for every ones input!

Order By DESC/ASC not working within PHP Query

I have this code so far which is within 2 while loops:
mysql_query("SELECT * FROM listing WHERE
(category_id='$category' OR category_id_2='$category' OR category_id_3='$category')
AND listing_status='1' AND listing_type='1' AND listing_id='$listing_id'
ORDER BY overall DESC");
The data is showing exactly what I want, however the ORDER BY simply isn't working. I'm not too sure what it's ordering by. The overall column itself is DECIMAL(12,2).
The values are saved to only 2 decimal places. For instance, in each row it could be 2.56, 2.89. In this case I want the 2.89 to show before the 2.56. However, it's not.
Many thanks in advance.
I believe you are only selecting one element at a time in your query, something like
while(...){
$category = ...;
$listing_id = ...;
// Your query which only returns one result here
}
Then since your query only returns one result it has nothing to sort, and you see the results in the order the queries are executed.
You need to rewrite your query to select all the rows you want in one go instead of having it in a loop if you want ORDER BY to work. Using IN in your query may help you.
Have you tried casting the field as a decimal in the order by?
ORDER BY CAST(overall as DECIMAL) DESC
I have managed to solve the problem.
By implementing the 'overall' column in the first loops table, instead of the second. It orders the data first by overall, and then goes ahead and gathers the other data from the second table.
Many thanks for your help.
Try:
mysql_query("SELECT * FROM listing WHERE
(category_id='$category' OR category_id_2='$category' OR category_id_3='$category')
AND listing_status='1' AND listing_type='1' AND listing_id='$listing_id'
ORDER BY overall+0 DESC");
The query is fine. The problem is probably with how you iterate returned data. Try changing it.
If not, provide us with the whole relevant piece of your PHP code.
I don't know why, but I found out by copying and pasting from phpmyadmin that this worked to solve a similar problem. The ' is changed to ` - dunno if it's important. But definitely ASC worked with the second way.
Important - the have been stripped from the second method, put them around the table name and the column names.
instead of
$sql = "SELECT * FROM 'dczcats' ORDER BY 'first' , 'second' ASC";
I typed this
$sql = "SELECT * FROM `dczcats` ORDER BY `dczcats`.`first` , `dczcats`.`second` ASC";

MySQL Query problem in CodeIgniter

So I'm using the following:
$r = new Record();
$r->select('ip, count(*) as ipcount');
$r->group_by('ip');
$r->order_by('ipcount', 'desc');
$r->limit(5);
$r->get();
foreach($r->all as $record)
{
echo($record->ip." ");
echo($record->ipcount." <br />");
}
Standard:
SELECT `ip`, count(*) as ipcount FROM (`soapi`) GROUP BY `ip` ORDER BY `ipcount` desc LIMIT 5;
And I only get the last (fifth) record echo'ed out and no ipcount echoed.
Is there a different way to go around this? I'm working on learning DataMapper (hence the questions) and need to figure some of this out. I haven't quite wrapped my head around the whole ORM thing.
Is there a way to set the count(*) as ipcount without the funny select() statement? I don't think it's triggering for some reason. This could also be a bug in DataMapper, but I'm less certain of that.
Also I found that even if I use the $r->query() method it doesn't return anything except the last entry if I use something like SELECTipFROMsoapiWHERE 1;. It will however return everything (like it should) if I say SELECT * FROM soapi WHERE 1;. If it doesn't have the * it only returns the last line.
Just verified with the new query, anything except selecting all columns (*) only returns the last record. Any help with this would be great. You can craft a statement like select *, count(*) as ipcount but then you still don't have access to it via $record->ipcount.
For your case, once you use COUNT() function in MySQL, it will only return 1 value. Hence you ip result data would not display out.
I suggest you just split this 2 queries.
1. for COUNT(*)
2. for ip
Hope this help.

Sorting by ratings in a database - Where to put this SQL? (PHP/MySQL)

OK - I'll get straight to the point - here's the PHP code in question:
<h2>Highest Rated:</h2>
<?php
// Our query base
$query = $this->db->query("SELECT * FROM code ORDER BY rating DESC");
foreach($query->result() as $row) {
?>
<h3><?php echo $row->title." ID: ";echo $row->id; ?></h3>
<p class="author"><?php $query2 = $this->db->query("SELECT email FROM users WHERE id = ".$row->author);
echo $query2->row('email');?></p>
<?php echo ($this->bbcode->Parse($row->code)); ?>
<?php } ?>
Sorry it's a bit messy, it's still a draft. Anyway, I researched ways to use a Ratings system - previously I had a single 'rating' field as you can see by SELECT * FROM code ORDER BY rating DESC. However I quickly realised calculating averages like that wasn't feasible, so I created five new columns - rating1, rating2, rating3, rating4, rating5. So when 5 users rating something 4 stars, rating4 says 5... does that make sense? Each ratingx column counts the number of times the rating was given.
So anyway: I have this SQL statement:
SELECT id, (ifnull(rating1,0) + ifnull(rating2,0) + ifnull(rating3,0) + ifnull(rating4,0) + ifnull(rating5,0)) /
((rating1 IS NOT NULL) + (rating2 IS NOT NULL) + (rating3 IS NOT NULL) + (rating4 IS NOT NULL) + (rating5 IS NOT NULL)) AS average FROM code
Again messy, but hey. Now what I need to know is how can I incorporate that SQL statement into my script? Ideally you'd think the overall query would be 'SELECT * FROM code ORDER BY (that really long query i just stated) DESC' but I can't quite see that working... how do I do it? Query, store the result in a variable, something like that?
If that makes no sense sorry! But I really appreciate the help :)
Jack
You should go back to the drawing board completely.
<?php
$query = $this->db->query("SELECT * FROM code ORDER BY rating DESC");
foreach($query->result() as $row) {
$this->db->query("SELECT email FROM users WHERE id = ".$row->author;
}
Anytime you see this in your code, stop what you're doing immediately. This is what JOINs are for. You almost never want to loop over the results of a query and issue multiple queries from within that loop.
SELECT code.*, users.email
FROM code
JOIN users ON users.id = code.author
ORDER BY rating DESC
This query will grab all that data in a single resultset, removing the N+1 query problem.
I'm not addressing the rest of your question until you clean up your question some and clarify what you're trying to do.
if you would like to change your tables again, here is my suggestion:
why don't you store two columns: RatingTotal and RatingCount, each user that rates it will increment RatingCount by one, and whatever they vote (5,4,4.2, etc) is added to RatingTotal. You could then just ORDER BY RatingTotal/RatingCount
also, I hope you store which users rated each item, so they don't vote multiple times! and swing the average their way.
First, I'd decide whether your application is write-heavy or read-heavy. If there are a lot more reads than writes, then you want to minimize the amount of work you do on reads (like this script, for example). On the assumption that it's read-heavy, since most webapps are, I'd suggest maintaining the combined average in a separate column and recalculating it whenever a user adds a new rating.
Other options are:
Try ordering by the calculated column name 'average'. SQL Server supports this. . not sure about mysql.
Use a view. You can create a view on your base table that does the average calculation for you and you can query against that.
Also, unrelated to your question, don't do a separate query for each user in your loop. Join the users table to the code table in the original query.
You should include it in the SELECT part:
SELECT *, (if ....) AS average FROM ... ORDER BY average
Edit: assuming that your ifnull statement actually works...
You might also want to look into joins to avoid querying the database again for every user; you can do everything in 1 select statement.
Apart from that I would also say that you only need one average and the number of total votes, that should give you all the information you need.
Some excellent ideas, but I think the best way (as sidereal said that it's more read heavy that write heavy) would be to have columns rating and times_rated, and just do something like this:
new_rating = ((times_rated * rating) + current_rating) / (times_rated + 1)
current_rating being the rating being applied when the person clicks the little stars. This simply weights the current user's rating in an average with the current rating.

Categories