PHP - Select Count Query Problem
The code below is part of some validation code that I have, it is supposed to count the number of entries in the table 'test_selections' where 'userid' equals the user that is logged in and where the 'transfer_in' date is a date within the current month. I am getting an error saying "Error running query Unknown column '10' in 'where clause' ". Does anybody know why this could be? I am hoping that it is something to do with the snippet of code below so I don't need to post the whole lot!!
Any help would be much appreciated. Code: [Select] $current_month = date("m"); $query = "SELECT COUNT(`userid`) as `transfers_count` FROM `test_selections` WHERE `userid` = '{$_SESSION['userid']}' AND Month(`transfer_in`) = `$current_month` GROUP BY `userid`"; Similar TutorialsHello there, Having a nightmare here. It feels like what I need to do is really easy but I just can't get it to work. I have a table called "groups2" that holds a unique id and the name of an activity. I already have a query that finds the users selected groups using a while loop but I also want to show how many other members are in that group by counting the number of times the activity comes up or the id comes up. I don't know whether I need 2 while loops nested or what but I get the error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource on line 32 which is highlighted. Can anyone help. I am no expert at php and still learning so some advice or example code would be great. $result = mysql_query("SELECT * FROM groups2 WHERE L_ID = ". $_SESSION['member_ID'] .";"); $data = mysql_query("SELECT COUNT('name') AS num FROM groups2 WHERE name = " . $row['name'] . ""); $count = mysql_fetch_assoc($data); $numbers = $count['num']; while ( $row = mysql_fetch_assoc($result) ) { echo("<tr> <td><font face='Arial, Helvetica, sans-serif' size='3'><strong>" . $row["name"] . "</strong></font></td> </tr><tr> <td><font face='Arial, Helvetica, sans-serif' size='1' color='#0000FF'><strong>Members (" . $numbers . ")</strong></font></td> </tr><tr> <td><hr width=95%><br></td> </tr>"); } Hi, i'm trying to select some entries from my DB, i don't quite understand what's the problem here, i'm using this: Code: [Select] //Customers Online $query = "SELECT COUNT(*) as Anzahl FROM customers WHERE status = 1"; $queryerg = mysql_query($query) OR die(mysql_error()); while($row = mysql_fetch_array($queryerg)){ $customers_online = $row[0]; } Which works fine, now i want to select by country and i'm doing this: Code: [Select] //Customers DE $query = "SELECT COUNT(*) as Anzahl FROM customers WHERE country = 'de'"; $queryerg = mysql_query($query) OR die(mysql_error()); while($row = mysql_fetch_array($queryerg)){ $customers_de = $row[0]; } Which doesn't work? If i print / echo $customers_de i just get a blank value, not even 0 or anything just blank. Any help ? I'm trying to figure out how to implement SELECT COUNT into a php generated list. My script lists all the users in one table $data = mysql_query("SELECT * FROM YBK_Login") or die(mysql_error()); while($info = mysql_fetch_array( $data )) { Print "<tr>"; Print "<td>".$info['UID'] . " </td>"; Print "<td>"."#"."</td>"; Print "<td>".$info['ID'] . "</td> "; Print "<td>".$info['Allowed'] . "</td> "; Print "<td>".$info['pass'] . " </td>"; Print "<td>".$info['HR'] . " </td>"; } ?> And I want to use the count to count the number of entries each user has in a diffrent table. Like pull the $info['ID'] and check against posts in YBK_Ads with the column name UID Hey, having an odd issue, looking to show players whos within 25 ranks of them lower and higher, i'm unsure as to why the following code doesn't actually do it... would anyone care to explain... Code: [Select] <?php $score = $playerdata['score']; $sql = "SELECT * FROM `players` WHERE '$score' BETWEEN 25 AND 75 ORDER BY `score` DESC"; $query = mysql_query($sql) or die(mysql_error()); $i=0; while($players = mysql_fetch_array($query)) { $i++; ?> Hi fellas, having a bit of trouble getting a count from the database where the field is not empty? $count= "SELECT COUNT(unid) FROM table"; $result2 = mysql_query($count) or die("Select Failed!"); $counter = mysql_fetch_array($result2); echo $counter[0]; From what i have read, this should get all the not null results from the field, but it gets all of them. Some are empty, the ones i want have various id numbers in them. Is there a way to get just the numbers where there is an id number present? This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=313023.0 Hello there,
This is the script i use right know, and i want it to become easier because i have so much categories that i want to show and it will take days to write all those categories.
<?php include 'extern/connect-.php'; //***************************************************************************** // categEGORY A //***************************************************************************** $result = mysql_query("select (select count(1) FROM videos WHERE title LIKE '%Accident%') as Accident , (select count(1) FROM videos WHERE title LIKE '%Acrobatic%') as Acrobatic, (select count(1) FROM videos WHERE title LIKE '%Adorable%') as Adorable , (select count(1) FROM videos WHERE title LIKE '%Adventure%') as Adventure, "); $row = mysql_fetch_assoc($result); foreach($row as $title => $total) { echo ' <div id="categ"> <div class="a"> <a href="search.php?search='. $title . '&submit= ">'. $title.' '. $total .'</a></div></div>'; } ?>So what i was thinking is if it is possible to make .txt file and to select from it like this here below, or if something else could help make the categories faster to write and get the best performance to load the webpage... some_file.txt Accident Acrobatic Adorable Adventure <?php include 'extern/connect-.php'; $something ='some_file.txt'; //***************************************************************************** // categEGORY A //***************************************************************************** $result = mysql_query("select (select count(1) FROM videos WHERE $something ......? , echo 'categories'; ?>Any help is Appreciated Thanks in Advance hi, I want to count the total number of multiple select that user has selected instead of getting the value the user has selected How should I go about doing the counting part? Code: [Select] <form action="1.php" method="post"> <select name="test[]" multiple="multiple"> <option value="one">one</option> <option value="two">two</option> <option value="three">three</option> <option value="four">four</option> <option value="five">five</option> </select> <input type="submit" value="Send" /> </form> <?php $test=$_POST['test']; if ($test){ foreach ($test as $t){echo 'You selected ',$t,'<br />';} } ?> Hello all,
Based on the suggestion of you wonderful folks here, I went away for a few days (to learn about PDO and Prepared Statements) in order to replace the MySQLi commands in my code. That's gone pretty well thus far...with me having learnt and successfully replaced most of my "bad" code with elegant, SQL-Injection-proof code (or so I hope).
The one-and-only problem I'm having (for now at least) is that I'm having trouble understanding how to execute an UPDATE query within the resultset of a SELECT query (using PDO and prepared statements, of course).
Let me explain (my scenario), and since a picture speaks a thousand words I've also inlcuded a screenshot to show you guys my setup:
In my table I have two columns (which are essentially flags i.e. Y/N), one for "items alreay purchased" and the other for "items to be purchased later". The first flag, if/when set ON (Y) will highlight row(s) in red...and the second flag will highlight row(s) in blue (when set ON).
I initially had four buttons, two each for setting the flags/columns to "Y", and another two to reverse the columns/flags to "N". That was when I had my delete functionality as a separate operation on a separate tab/list item, and that was fine.
Now that I've realized I can include both operations (update and delete) on just the one tab, I've also figured it would be better to pare down those four buttons (into just two), and set them up as a toggle feature i.e. if the value is currently "Y" then the button will set it to "N", and vice versa.
So, looking at my attached picture, if a person selects (using the checkboxes) the first four rows and clicks the first button (labeled "Toggle selected items as Purchased/Not Purchased") then the following must happen:
1. The purchased_flag for rows # 2 and 4 must be switched OFF (set to N)...so they will no longer be highlighted in red.
2. The purchased_flag for row # 3 must be switched ON (set to Y)...so that row will now be highlighted in red.
3. Nothing must be done to rows # 1 and 5 since: a) row 5 was not selected/checked to begin with, and b) row # 1 has its purchase_later_flag set ON (to Y), so it must be skipped over.
Looking at my code below, I'm guessing (and here's where I need the help) that there's something wrong in the code within the section that says "/*** loop through the results/collection of checked items ***/". I've probably made it more complex than it should be, and that's due to the fact that I have no idea what I'm doing (or rather, how I should be doing it), and this has driven me insane for the last 2 days...which prompted me to "throw in the towel" and seek the help of you very helpful and intellegent folks. BTW, I am a newbie at this, so if I could be provided the exact code, that would be most wonderful, and much highly appreciated.
Thanks to you folks, I'm feeling real good (with a great sense of achievement) after having come here and got the great advice to learn PDO and prepared statements.
Just this one nasty little hurdle is stopping me from getting to "end-of-job" on my very first WebApp. BTW, sorry about the long post...this is the best/only way I could clearly explaing my situation.
Cheers guys!
case "update-delete": if(isset($_POST['highlight-purchased'])) { // ****** Setup customized query to obtain only items that are checked ****** $sql = "SELECT * FROM shoplist WHERE"; for($i=0; $i < count($_POST['checkboxes']); $i++) { $sql=$sql . " idnumber=" . $_POST['checkboxes'][$i] . " or"; } $sql= rtrim($sql, "or"); $statement = $conn->prepare($sql); $statement->execute(); // *** fetch results for all checked items (1st query) *** // $result = $statement->fetchAll(); $statement->closeCursor(); // Setup query that will change the purchased flag to "N", if it's currently set to "Y" $sqlSetToN = "UPDATE shoplist SET purchased = 'N' WHERE purchased = 'Y'"; // Setup query that will change the purchased flag to "Y", if it's currently set to "N", "", or NULL $sqlSetToY = "UPDATE shoplist SET purchased = 'Y' WHERE purchased = 'N' OR purchased = '' OR purchased IS NULL"; $statementSetToN = $conn->prepare($sqlSetToN); $statementSetToY = $conn->prepare($sqlSetToY); /*** loop through the results/collection of checked items ***/ foreach($result as $row) { if ($row["purchased"] != "Y") { // *** fetch one row at a time pertaining to the 2nd query *** // $resultSetToY = $statementSetToY->fetch(); foreach($resultSetToY as $row) { $statementSetToY->execute(); } } else { // *** fetch one row at a time pertaining to the 2nd query *** // $resultSetToN = $statementSetToN->fetch(); foreach($resultSetToN as $row) { $statementSetToN->execute(); } } } break; }CRUD Queston.png 20.68KB 0 downloads This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=312721.0 $result = mysql_query("SELECT * FROM `friend_system` WHERE `friend_system_accepter` = '$myusername' "); echo mysql_num_rows($result);This displays the total rows in the table. $result = mysql_query("SELECT COUNT(friend_system_accepter) FROM `friend_system` WHERE `friend_system_accepter` = '$myusername' "); echo mysql_num_rows($result);This displays '1', which is incorrect. I want to echo out the number of rows where 'friend_system_accepter' = $myusername Thanks This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=305934.0 Hi I'm not at all good at PHP and haven't really played with it for some years, so bare with me if code examples are old I'm looking for a simple solution to count rows (MySQL), without using multiple queries. The below code is quite simple, counting rows where 'Department' is 'Sales': $sql ="SELECT * FROM DB WHERE Department ='Sales'"; $result=mysqli_query($con,$sql); $rowcount=mysqli_num_rows($result); But what If I need to count the rows for the different departments?: $sql ="SELECT * FROM DB WHERE Department ='Sales'"; $result=mysqli_query($con,$sql); $rowcount=mysqli_num_rows($result); $sql ="SELECT * FROM DB WHERE Department ='Support'"; $result=mysqli_query($con,$sql); $rowcount=mysqli_num_rows($result); $sql ="SELECT * FROM DB WHERE Department ='Online'"; $result=mysqli_query($con,$sql); $rowcount=mysqli_num_rows($result); There must be a better / simpler way to this with a single query? Hope for someone to show me the light
PS. Once this is resolved, I'll probably need further help, to accomplish my final result. Basically, this part of the code is checking the "scanned" table in my mysql database to get the last scanned date for the item at hand. It then takes that date and selects all the vendor items in the odbc table "ARVEND" that are greater than the last purchase date. || This is where I am running into a problem. When it loops through each vendor item it selects from the "associated" mysql table to see if the vendor item has been scanned. If it does not find anything I am trying to get the $checkpurdate variable to add 1 to itself. | In the second script you will see the elseif($checkpurdate>0) .... Here I am saying if there are any vendor items that have not been scanned, highlight the row in yellow. But, this part of my code $checkpurdate = $checkpurdate + 1; does not seem to be counting correctly. Sometimes it counts '4' for an item when there are only two vendor items available. All of this may sound confusing since you may not know what I am trying to do. But, the main problem I am having is the counting part is returning wrong numbers. $checkpurdate = $checkpurdate + 1; Code: [Select] /* $checkpurdate */ $query3 = "SELECT date FROM scanned WHERE `item` = '$ITEM' ORDER BY date DESC LIMIT 1"; $result3 = mysql_query($query3); $checklastdate = ""; while($row3 = mysql_fetch_array($result3, MYSQL_ASSOC)) {$checklastdate= $row3['date']; $checklastdate=date("m/d/y", strtotime($checklastdate)); } //checkpurdate loops $checkassoc="0"; $checkpurdate="0"; $sqlc="SELECT RECNO5 FROM ARVEND WHERE PURDATE1 > '$checklastdate' AND ITEM = '$ITEM'"; $rsc=odbc_exec($conn,$sqlc); if (!$rsc) {exit("Error in SQL");} while (odbc_fetch_row($rsc)) { $checkrecno5=odbc_result($rsc,"RECNO5"); $checkassosquery = "associated WHERE `VENDORID` = '$checkrecno5' AND ITEM = '$ITEM'"; $checkassoc = get_rows($checkassosquery); if($checkassoc>0) { // do nothing } else { $checkpurdate = $checkpurdate + 1; } } Code: [Select] elseif($checkpurdate>0) { echo"<TR class=\"NV\"> <TD>"; } Hi Everyone, I am working on implementing a blog comment system using the query below to display comments. My question is what is the best way to do a row count for the id column with this query or do I need to do a second db query to accomplish this? The purpose of this is to echo out the number of comments for that post, before displaying them. Any help is appreciated. Thanks in advance, kaiman <?php // get url variables $post_id = mysql_real_escape_string($_GET['id']); include ("../../scripts/includes/nl2p.inc.php"); // connects to server and selects database include ("../../scripts/includes/dbconnect.inc.php"); // table name $tbl_name3="blog_comments"; // select info from comments database $result3 = mysql_query ("SELECT count(*) FROM $table_name3 WHERE id='$post_id' ORDER BY id DESC LIMIT 1") or trigger_error("A mysql error has occurred!"); if (mysql_num_rows($result3) > 0 ) { while($row = mysql_fetch_array($result3)) { extract($row); // display number of comments // display date $row_date = strtotime($row['date']); putenv("TZ=America/Denver"); echo "<p class=\post\">On ".date('F, jS, Y', $row_date)." "; // display commenter name if($row['url'] == "") { echo $row['name']." wrote:</p>\n"; } else { echo "<a href=\"".$row['url']."\" target=\"_blank\">".$row['name']."</a> wrote:</p>\n"; } // display content $comments = $row['comment']; echo nl2p($comments); } } else { echo "<p class=\"large_spacer\">No Comments</p>"; } ?> Hello i am working on an application where i want to count from a selected date to another date and query all entries my database right now is composed of the following sales_id barcode_id student_id type of lunch date i have created the count per day, but now dont know how to go about to query the results and the count for the month. Code: [Select] $query = 'SELECT COUNT(*) FROM `sales` WHERE `tlunch` = 1 AND DATE(date) = CURDATE()'; $result = mysql_query($query) or die('Sorry, we could not count the number of results: ' . mysql_error()); $free = mysql_result($result, 0); has any one done something similar This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=330871.0 This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=312690.0 I have a database lets say punch_log The data in the table looks like: --------------------------------------------------------------------------- |punch_log_id | user_id | punch_id | punch_time | --------------------------------------------------------------------------- | 10010 | 21 | 1 | 2010-11-10 15:04:59| | 10011 | 21 | 2 | 2010-11-10 15:50:05| | 10010 | 21 | 1 | 2010-11-11 15:04:59| | 10011 | 21 | 2 | 2010-11-11 15:50:05| | 10010 | 21 | 1 | 2010-11-12 15:04:59| | 10011 | 21 | 2 | 2010-11-12 15:50:05| | 10010 | 21 | 1 | 2010-11-13 15:04:59| | 10011 | 21 | 2 | 2010-11-13 15:50:05| | 10010 | 21 | 1 | 2010-11-14 15:04:59| | 10011 | 21 | 2 | 2010-11-14 15:50:05| | 10010 | 21 | 1 | 2010-11-14 15:50:59| <-- this is why i need this. | 10011 | 21 | 2 | 2010-11-14 15:55:05| <-- this is why i need this. ---------------------------------------------------------------------------- Im currently using : $kust = $_POST['AKuu']; $kuni3 = $_POST['LKuu']; $valitudtootaja = $_POST['TNimi']; mysql_select_db($database, $con); $query2 = "SELECT *, SUM(punch_id) FROM punch_log WHERE user_id = '".$valitudtootaja."' AND punch_time BETWEEN '$kust' AND '$kuni3' AND punch_id ='1' "; $result2 = mysql_query($query2) or die(mysql_error()); while($row = mysql_fetch_array($result2)){ $X = $row['SUM(punch_id)']; When using the current query im getting Workers days at work = 6 it should be 5 but thats why i need some solution to group dates and then sum them. Does anybody have any ideas? |