PHP - Problem With Selecting Specific Rows From A Table Based On The Size Of A Number
Similar TutorialsHi All ,
I have a small table with 4 fields namely Day_ID, Dues, Last_Visit, Points. where Day_ID is an auto-increment field. The table would be as follows:
Day_ID -- Dues --- Last_Visit --- Points.
1 --------- 900 -------- 1/12 -------- 6
2 --------- 700 -------- 4/12 -------- 7
3 --------- 600 -------- 7/12 -------- 5
4 --------- 600 -------- 9/12 -------- 6
5 --------- 600 -------- 10/12 ------- 6
6 --------- 600 -------- 14/12 ------- 6
So this is the record of a person's visit to say a club. The last row indicates the last date of his visit to the club. His points on this date are 6. Based on this point value of 6 in the last row I want to retrieve all the previous BUT adjoining all records that have the same Points i.e. 6.
So my query should retrieve for me, based on the column value of Points of the last row (i.e. Day_ID - 6 ), as follows:
4 --------- 600 -------- 9/12 -------- 6
5 --------- 600 -------- 10/12 ------- 6
6 --------- 600 -------- 14/12 ------- 6
This problem stated above had been completely resolved, thanks to a lot of help from Guru Barand by this following query :-
$query = "SELECT cv.day_id, cv.dues, cv.last_visit, cv.points FROM clubvisit cv WHERE last_visit >= ( SELECT MAX(last_visit) FROM clubvisit WHERE points <> ( SELECT points as lastpoints FROM clubvisit JOIN ( SELECT MAX(last_visit) as last_visit FROM clubvisit ) as latest USING (last_visit) ) )";I am using this and it works perfectly except that now there is a slight change in the table because the criteria for points is now dependent on more than one column cv.points and is more like cv.points1, cv.points2, cv.points3 etc. So now I need to make a selection based on each of these cv.points columns. As of now I can still get the results by running the query multiple times for each of the cv.points columns ( seperately for cv.points1, cv.points2, cv.points3) and it works correctly. However I am wondering if there is a better way to do this in just one go. This not only makes the code repetitive but also since the queries are interconnected, involves the use of transactions which I wish to avoid if possible. The values that I require for each of the cv.point columns is 1. day_id of the previous / old day on which the cv.points value changed from the current day value, and 2. cv.points on that old/ previous day. So for example if the table is as below: Day_ID -- Dues --- Last_Visit --- Points1 --- Points2. 1 --------- 900 -------- 1/12 ----------- 9 ------------ 5 2 --------- 600 -------- 4/12 ----------- 6 ------------ 6 3 --------- 400 -------- 7/12 ----------- 4 ------------ 7 4 --------- 500 -------- 9/12 ----------- 5 ------------ 8 5 --------- 600 -------- 10/12 ---------- 6 ------------ 8 6 --------- 600 -------- 11/12 ---------- 6 ------------ 8 7 --------- 600 -------- 13/12 ---------- 6 ------------ 7 8 --------- 500 -------- 15/12 ---------- 5 ------------ 7 9 --------- 500 -------- 19/12 ---------- 5 ------------ 7 Then I need the following set of values : 1. day_id1 -- Day 7, points1 ---- 6, days_diff1 -- (9-7 = 2) . // Difference between the latest day and day_id1 2. day_id2 -- Day 6, points2 ---- 8, days_diff2 -- (9-6 = 3) 3. day_id3 -- .... and so on for other points. Thanks all ! Hi PHPFreaks.com I wrote in here a while ago about updating table rows by selecting rows which end dates is greater than the current day, much like a experation dates with blog posts in a CMS. I was told to make sure my datatype for the end dates in the database was the datetime format is YYYY-MM-DD HH:mm:SS now I have now changed that, but I am still have trouble creating a query that does exactly what I mentioned. mysql_query("UPDATE ". $db_prefix ."events WHERE end_time >= TIMESTAMP: Auto NOW() SET cat = 1" ); Now what I am trying to is that if the current time is greater than the end date I need the "cat" row changed to 1 So thanks in advance if you can help me guys Hi, I have a problem regarding counting rows in a mysql table. Here is the code: Code: [Select] <?php require_once('includes/connection.inc.php'); $conn = dbConnect('read'); $check = 'SELECT COUNT(*) FROM images'; $checkRes = $conn->query($check) or die(mysqli_error()); if($checkRes <= 0) { header('Location: empty_blog.php'); // redirect to another page } else { // do something else... } ?> The error I'm getting: "Notice: Object of class mysqli_result could not be converted to int in C:\xampp\htdocs\photoblog\index.php on line 6" Any help is greatly appreciated! hello, all: I'm a newbie, and been trying to work out a way so that the results from an array (or mysql recordset for that matter), are nicely aranged in a table. I want them to show in a grid-like manner, with rows and columns added according to number of "items". I worked out this small code snippet, with a sample array, and for the life of me, cant figure out how to make it so it automatically "breaks" columns at 5 and start new row. I got it to show me right number of rows, but then it repeats all 10 names within them, as opposed to just showing 5 names in each row... Appreciate the help... Code: [Select] <?php $names = array('Charles','Henry','Manny','Philip','Rose','Evelyn','Peter','Julia','Cary','Sophia'); $numberColumns = 5; $numberNames = count($names); $numberRows = ceil($numberNames / $numberColumns); echo "<table border='1' width='400' cellspacing='0' cellpadding='0'>"; for ($i=1; $i<=$numberRows; $i++) { echo "<tr>"; foreach ($names as $name) { echo "<td>" . $name . "</td>"; } echo "</tr>"; } echo "</table>"; ?> I have a problem that occurred only recently, apparently by some change my webhost made on the server. Some time ago I wrote a script that simulates a Poedit-type editor. The script has about 1500 textareas (the translation fields) with unique names, dynamically generated. All worked fine. Now suddenly I get errors (see below) when I use more than 1000 textareas. It is not a problem of $_POST size, if happens also with empty textareas. I will show here a simple php test file that I called testta.php, which creates the same error as my much more complicated real script: Code: [Select] <?php if(isset($_POST['test'])) { echo 'The textareas were posted successfully!<br>'; } $max = 1500; echo '<form action="testta.php" method="POST">'; echo '<input type="submit" name="test" value="Submit"><br>'; for($i=0; $i < $max; $i++) { echo '<textarea name="test'.$i.'">name is test'.$i.'</textarea><br>'; } echo '</form>'; ?> The error message generated in the browser: 500 Server Error: A misconfiguration on the server caused a hiccup. Check the server logs, fix the problem, then try again. From the Apache log file: [Sat Apr 28 12:47:48 2012] [error] [client 93.172.175.50] Premature end of script headers: testta.php, referer: http://****/*****/testta.php (stars added for privacy) When $max is set to a number < 1000 it works. On the web I have only found answers that deal with max size of posts, but as I mentioned that is not my case. My webhost can't help me out either. Until last Friday all worked fine. Does anyone know what server setting could create this problem? Thanks in advance, Kathy Hi All, I am trying to pull a record from my table using the date of birth column (which is a date column) like so.. Code: [Select] $birthday = ('1936-08-21'); $query = "Select * from my_table where dob = $birthday"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)){ echo "Name is - " .$row['name']; } I know there is a record in the table with a date of birth(dob) value of 1936-08-21 but it does not return any records ? thanks for looking, Tony I am running a query to get a certain student's quiz results from all attempts and print one after the other in a simple table for now. The student is designated by an email address. I need results from all of the rows containing this particular email. The issue I think I am having is that when I use "WHERE" and specify an email address that exists many times in the table it isn't storing the variable correctly. Here is code below that I cannot get working right. When I try to print these variables as a test to see if they are working I get nothing. No errors either. I have checked all names, spellings, and capitalization about 5 times. Thanks for looking at it. Code: [Select] $QuizQuery = mysql_query("SELECT * FROM Quiz_Results WHERE Email = '".$_SESSION['UserEmail']."' AND Quiz_Name = 'M1'") or die(mysql_error()); $i=1; while ($QuizResults = mysql_fetch_array($QuizQuery)){ $UserEmail = $QuizResults['Email']; $Score = $QuizResults['Score']; $Date = $QuizResults['Date_Taken']; echo "<table width='650' border='1'><tr><td>"; echo "Attempt #".$i."<br></td><td>Sco ".$Score."</td><td>"; if($QuizResults['Pass'] == "PASSED"){ echo "Passed"; } else { echo "Failed"; } echo "</td><td>Date Taken: ".$Date."</td></tr></table>"; $i++; } Hi this is what I have set up - SQL tables - Members - memberid - primary key Username Password Orders - Orderid - primary key memberid - foreign key orderno In my sql table there are two members and two orders. 1 order is assigned to 1 member and the other order to the other member. After each member logs in I need to 'echo' that order on the page. I've not been able to write any succesful php code that 'gets' the specific order that is related to the member that has signed in. I would appreciate help on this! Thanks SELECT * FROM `booking_tbl` WHERE booking_status = 'Check In' AND departure_date_time = NOW()"I use the datetime datatype for the departure_date_time so i can get data from that data because it checking the date and time but i want it to check on the date from the datetime datatype So how can i get the data only with that date without the time in the mysql datetime datatype Hey there, this I've no idea how can be done with PHP. I'm looking for a way to randomly select a 7 digits number from a *.txt and then print it on a webpage. Do any one know how can this be done? Thank you. Hi Guys
I'm a bit stuck on a query and hoping someone can help. I'm sure this has a really simple solution.
Below is a simplified representation of a table and I'm trying to select all of the data but where there are instances of the same postID I want to only select the row with the highest version number.
So for instance in a select all on the table below I would expect to get IDs of 2,3 and 4.
id | postID | version Alright, I've been assigned a project at work. I did not develop the application and the individual who did used CodeIgnited framework and mysql as the db.
Here's the problem, I'm not given much OT to do this and in our meeting the best way to proceed was to replicate the database for different parts of the organization. Basically we are a subsidiary and have been using an application that other groups within the organization want to use. Usually I would reconfigure the db schema and add org ids and in the user table add the appropriate organization to go to. However, they are not giving me enough time to do that.
So what I'm thinking is to just create a copy of the database we use (just the structure) and create a new database.
What I want to know is how to use mysql to check to see if a user exists in one database and if they don't then to go on to the next database. I understand this is a very sloppy way to do it, but it's the way we are moving forward.
I found the code to connect to the db in CodeIgnitor... how can I connect to a database, check to see if the user exists, then close that db connection and try the next database?
/** * Select the database * * @access private called by the base class * @return resource */ function db_select() { return @mysql_select_db($this->database, $this->conn_id); }Thanks in advance. We recently upgraded from PHP4 to PHP5 and the below script that was working perfectly in 4 has completely stopped working and I can't figure out why for the life of me. I'm not an experienced PHP programmer--I've done some forms, but this is the first time I've used a database. What needs to (and was) happen: A user enters their username in the form and gets a readout of their participation so far that month. The problem(s): I know that it's storing the variable 'user' because it echoes it back properly, but the database is no longer allowing me to select the row based on that variable. I know it's not that I can't connect to the database because if instead of '$user' I change the code to a username I know is in there, I get the proper readout. This all started as soon as I transferred over to PHP5--before that, no problems at all. The database information is all correct, I just took it out for privacy's sake. <form id="feedback" method="post" action="index.php"> <input name="user" type="text" value="Enter user name" size="20" maxlength="50" /><br /> <input name="send" id="send" type="submit" value="Submit" /> </form> <?php if (isset($_POST['user'])) { $_session['user'] = $_POST['user']; } ?> <p>You entered your username as: <strong><? echo $_session['user'];?></strong>. If this is not correct or you do not see your information below, please re-enter your username and click Submit again.</p> <?php $con = mysql_connect("database","username","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("database", $con); $result = mysql_query("SELECT * FROM March WHERE Username='$user'") or die ('Error: '.mysql_error ()); while($row = mysql_fetch_array($result)) { echo "<table border='0'>"; echo "<tr>"; echo "<td><strong>Username:</strong> </td><td>" . $row['Username'] . "</td></tr>"; echo "<tr><td><strong>Mar. 2 Discussion:</strong></td><td>" . $row['Mar2Q'] . "</td></tr>"; echo "<tr><td><strong>Mar. 2 Poll:</strong></td><td>" . $row['Mar2P'] . "</td></tr>"; echo "<tr><td><strong>Mar. 9 Discussion:</strong></td><td>" . $row['Mar9Q'] . "</td></tr>"; echo "<tr><td><strong>Mar. 9 Poll:</strong></td><td>" . $row['Mar9P'] . "</td></tr>"; echo "<tr><td><strong>Mar. 16 Discussion:</strong></td><td>" . $row['Mar16Q'] . "</td></tr>"; echo "<tr><td><strong>Mar. 16 Poll:</strong></td><td>" . $row['Mar16P'] . "</td></tr>"; echo "<tr><td><strong>March Participation To-Date:</strong></td><td>" . $row['Participation'] . "</td></tr>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> ANY help would be greatly appreciated! I've got a couple hundred people who use this on a regular basis and are starting to ask why it's not working. I wish to return the object in an array with the highest index where its index falls between between integers, and return null should one not exist. For instance, with the following and a min and max of 10,000 and 15,000, it should return OBJ4, and with a min and max of 15,000 and 20,000 it should return NULL. Any thoughts? Thanks function getObj(int $min, int $max):?OBJ { $list=[ 12314=>'OBJ1', 321=>'OBJ2', 42142=>'OBJ3', 14314=>'OBJ4', 123=>'OBJ5', 13314=>'OBJ6' ]; return getIt($list, $min, $max); }
Hi there, In a string for a shopping cart I am storing both the item id and the item size selected in a number like this 12s1 where 12 is the id and 1 is the size. How can I separate these from one another again? I'm guessing it's to do with expand() ? Thanks, Jack Assuming I have a result object: $result = mysqli_query($sql_string); How can I get specific rows, i.e. multiple rows are returned 8 total, but I was rows 2-6. Thanks. So I have an array of IDs that I would only like to select from a database table instead of everything in the table. How might I go about doing that? I have a simple database set up for story awards given out by different groups. Here are some of the fields I'm using: Author Title Year Summary Some of the rows are nearly identical with the same Author and Title, but sometimes a different year or or others have a Summary for the story where otherwise it would be empty for a different row. The problem I'm having is I want to fetch the summary of the story from all rows where the Author and Title are identical. Currently the code is fetching a random row with the same Author and Title, but won't show the Summary if that specific row doesn't have the summary filled in (and where other rows of nearly identical information would have the summary). Here's an example of nearly identical rows: 'Boring Title', 'Boring Author', '', '2012' 'Boring Title', 'Boring Author', 'Boring Summary', '2012' How would I go about fetching all rows with the identical Author and Title and then showing the Summary even if only one of those rows has information in the Summary field? Much obliged for any help. Here's the code I'm working with: Code: [Select] <?php mysql_connect(localhost,db_user,pw); @mysql_select_db(db_database) or die( "Unable to select database"); $quer="SELECT * FROM archives ORDER BY RAND() DESC LIMIT 1"; $re = mysql_query($quer); while ($ro = mysql_fetch_array($re)) { extract($ro); $please = ''; if (strlen($ro[summary]) == 0) { $please = "The summary is empty. Please help the Archives by <a href=\"edit.php?cmd=edit&author=$author&title=$title\">filling out the summary</a>."; } $select_author = mysql_query("SELECT DISTINCT author FROM archives WHERE `title` = '" . mysql_real_escape_string($title) . "' ") or die (mysql_error()); $aut = ""; while ($row2 = mysql_fetch_array($select_author)) { $aut .= "<a href=\"author.php?author={$row2[author]}\">$row2[author]</a> & "; } $aut = substr($aut,0,-3); echo '<div style="margin: 5px;"> '.$summary.''.$please.' <p style="float: right; width: 100%; margin-top: 10px; text-align: right;"><i> ~ <a href="info.php?author='.$author.'&title='.$title.'">'.$title.'</a> by <a href="author.php?author='.$author.'">'.$aut.'</a></i> </div>'; } ?> Hello, i retrieve an array from the database that looks something like this: $var=a:1:{s:2:"cc";a:1:{i:22;s:11:"TESTING";}}; I need to retrieve the number corresponding to the character i: and print it(11 in the first case). For example , If, $var=a:1:{s:2:"cc";a:1:{i:15;s:11:"TESTING";}}; I would need to print 15. $var=a:1:{s:2:"cc";a:1:{i:3;s:11:"TESTING";}}; I would need to print 3. Thanks in advance. |