|
I'm working on an PHP site for work with a backend SQL database, and I have a problem I can't figure out. I don't expect people on here to be able to help as this is a games forum but I wondered if anyone had any links to other forums where someone would be able to help. For anyone who might be able to help, I'm querying a table and displaying the results in a website table but for some reason, the first result is always missed off. |
Can anyone recommend any good PHP/SQL Forums, or help with my problem?
-
-
pjmaybe 70,666 posts
Seen 12 years ago
Registered 20 years agoI'd need to know a bit more about the database....
For instance, are you setting up a primary key? How is each record "differentiated" from another..
What I usually do (because I'm lazy)
1) Set up a field as an integer, and auto-incremental
2) Use that as the primary key
3) Display records as many as I can fit on a page with a "repeat region" and just page through 'em if there are more than can fit on one page.
Doing this kinda stuff at the mo so might be able to help.
Peej -
Well basically I have two tables each with a key field called NSN.
I list all the fields in table 1 but set the key field to be a button. When this button is pressed it lists that item as well as results from table 2 for the same item.
This works, however the first row of results are missing. The query is getting the right result set but missing off the first row bizarrely enough.
It's difficult to explain without being more specific but I'm not about to start posting lines of PHP and links in this forum :s -
Shivoa 6,314 posts
Seen 2 years ago
Registered 20 years agoI just finished \o/ a PHP/MySQL system for work (which rocks and, thanks to me deciding in the end not to be a lazy bugger, doesn't eat up all the CPU time on our test server*) so I'll give a hand if I can.
*test server is a 733MHz Celeron with 512MB of PC133 SDR RAM. Oxford county council doesn't think it a priority to buy us an inactive server to test things for deployment. Taking up all the CPU time on the server can be achieved by moving the mouse about under the WinXP install so managing to not eat up that powerhouse actually forced me to half learn some properly programming conventions and not lazily query whenever I wanted code in a linear program. -
Captain-Fetid 659 posts
Registered 17 years agoI don't get it, really.
A very good forum is Tek Tips, people are very knowledgeable and helpful over there.
I often dabble in PHP, and quite a lot of people around here do so too, so feel free to post code and such.
Solving such stuff is like puzzling; very satisfying. -
Ok guys, you asked for it:
The page that is 'borked' is here:
When you click on the buttons next to each item, the deliveries against that item should be listed below, which they are, but for both of them, there should be an extra one, which is the first in the list but not displayed. There should be 5 results for the item ending 7777 and 2 for the item ending 6666 but there is only 4 and 1 :s
Yes yes I know it needs tarting up but I will do that when the fooking things works.
Anyway the snippet of code that seems to be ferked up is below, there is a whole load more to that page, but I'm sure it's something wrong with the code for that particular table as the query code, further up the page is fine.
See post below for link to page.
Edited by HairyArse at 11:22:44 18-03-2005 -
eviltobz 2,609 posts
Registered 18 years agoi've not used php or mysql so my terminology might be a little different to yours, but the first thing that i'd want to check is how many records are returned from you querying the database. using vb i can do MyRecordset.RecordCount to see how many rows were returned. this will indicate if the issue is with your sql statement not getting all the rows, or your php not processing them correctly. -
Bollocks.. how do I display code? -
OK seeing as I can't paste the code, the php files is zipped and uploaded to here:
linky -
eviltobz 2,609 posts
Registered 18 years agoyou could try the code tag as mentioned next to the textbox when you post.
another thing that springs to mind is when you are looping through a bunch of data using numbers to identify each row, in my experience the is a lot of inconsistency between things being 1 based or 0 based. so in an array you might need to use Array(0) or Array(1) to get the first item, depending on who wrote the code for the array datatype. -
Evil tobz, the array is displaying the correct fields, it's just missing out the first set of results,
i.e. only displaying 4 results when there should be 5, and it's the last 4 and not the first four. It's odd. -
Anyone? -
Captain-Fetid 659 posts
Registered 17 years agoI'm having a look now - I'll see if I can find anything. -
Captain-Fetid 659 posts
Registered 17 years agoIt probably won't solve the problem, but a better way of running through you $row array would be to use:
[code]
echo "(tr)";
foreach ($row as $id=>$content) {
echo "(td)";
echo $content;
echo "(/td)";
}
echo "(/tr");
[/code]
You don't have to do the for() loop then (which is a dangerous thing in this code).
Edit: Same thing with your colheader, actually. That way any change in the database will be immediately reflected in your code output.
Edited by Captain Fetid at 13:04:09 18-03-2005 -
Sounds good captain, but I'm very much a PHP SQL beginner - only been learning it one night a week at college for about 4 months.
Can you explain how that $row array works as at first glance it's not obvious. -
Captain-Fetid 659 posts
Registered 17 years agoWell, this line for example:
[code]
$row = mysql_fetch_array($result2, MYSQL_NUM)
[/code]
returns an array $row, with values:
[code]
row[0]=1000
row[1]=2001-04-20
row[2]=adfsfh
[/code]
Now, what this foreach does, is it looks at the array $row and loops through each element. Thus:
[code]
foreach ($row as $id=>$content) {
}
[/code]
splits row in a variable called $îd, and one called $content. Taking the example above, in the first iteration:
$id = 0;
$content = 1000:
In the second:
$id = 1
$content = 2001-04-20;
and so on.
Edit: A tip - litter your code with
[code]print_r ($variable)[/code]. This prints out the contents of arrays and such.
Edited by Captain Fetid at 13:12:33 18-03-2005 -
Captain Fetid - thanks for that mate, that does simplify arrays which are a constant nightmare to me at the mo. .gif)
Now back to the problem at hand... any ideas what is causing the problem? -
Captain-Fetid 659 posts
Registered 17 years agoOh and something else still (I like PHP
.
There's no need to fetch your values from the database in a MYSQL_NUM way. I always use mysql_fetch_assoc($result). N your case, this would return a $row array with these contents:
[code]
row["Quantity_Delivered"]=1000
row["Date_Delivered"]=2001-04-20
row["Sixforty_Form"]=adfsfh
[/code]
which makes things even easier. -
While we're on the subject, do you know how to force SQL to arrange the dates the right way around? Or do I have to do a strrev on the users input form to reverse the date before updating the database? -
Captain-Fetid 659 posts
Registered 17 years agoHairyArse wrote:
Now back to the problem at hand... any ideas what is causing the problem?
Well, it could be the way you're handling the results of your query (with the numerical for() loop). Try and do print_r ($row2) after your mysql_fetch_array and have a look whether the correct data is being fetched from the database.
The problem by the way lies in that for() loop:
[code]
for($j=11; $j is_smaller_than 14; $j++){
echo $j;
}
[/code]
Would give you:
11
12
13
14
4 results instead of 5.
At 14, the value of $j is compared to the expression $j is_smaller_than14 which evaluates to TRUE. So - the loop is aborted. -
Captain-Fetid 659 posts
Registered 17 years ago -
Captain-Fetid 659 posts
Registered 17 years ago -
Captain-Fetid 659 posts
Registered 17 years agoHairyArse wrote:
While we're on the subject, do you know how to force SQL to arrange the dates the right way around? Or do I have to do a strrev on the users input form to reverse the date before updating the database?
Dates are a bit of a bother to deal with. I'm not sure I get what you mean. Do you want the dates to be displayed in another format, or do you want your results ordered by date? -
But I thought that loop was looping through the field headers being displayed in the array.
The first 10 fields are the fields in table 1 and 11-14 are the 4 fields in table 2. Not the record set.
/is confused...
Php doesn't half fuck with your head when you're a newbie. -
What I meant about dates, is that the user has to enter them in reverse order, i.e 2005-03-17 and I just know that my boss is gonna complain about having to do it that way.
I assume the only way to fix this is to use the strrev (reverse string) function to reverse a normally entered date (17-03-2003) to make it SQL friendly? -
Captain-Fetid 659 posts
Registered 17 years agoHairyArse wrote:
But I thought that loop was looping through the field headers being displayed in the array.
The first 10 fields are the fields in table 1 and 11-14 are the 4 fields in table 2. Not the record set.
/is confused...
Php doesn't half fuck with your head when you're a newbie.
Ah I see what you mean
It can get confusing indeed.
Forget numbers, and think of loops. It's better to check against other conditions than simple numbers, because you're asking for trouble that way.
You know what, give me your email, and I'll send you your file back - completely commented. -
Captain-Fetid 659 posts
Registered 17 years ago -
Captain-Fetid 659 posts
Registered 17 years ago -
Captain-Fetid 659 posts
Registered 17 years ago -
Bloody hell. I typed a whole document on this, and I can't paste because > and < apparently don't translate well between code tags.
Sometimes posts may contain links to online retail stores. If you click on one and make a purchase we may receive a small commission. For more information, go here.

.gif)