Solved

Why aren't all queries taken care of?

Posted on 2014-03-16
7
169 Views
Last Modified: 2014-03-16
aktuellt.php

This code outputs $SQL1 like this:
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,1
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,2
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,3
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,4
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,5
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,6
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,7
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,8
SQL1: SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,9
but it does not print the very first line. However. all the other lines are printed.
Could you experts, please, help me to get also the very first line to query and print.
0
Comment
Question by:lericson
  • 3
  • 2
  • 2
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39932895
Please post the CREATE TABLE statement and a few rows of test data.  I would like to give you a tested and working example.  Thanks, ~Ray
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39932905
Unrelated, but you might want to consider getting off the MySQL extension.  PHP is doing away with it.  This article explains why and what you must do to keep your scripts running in the future.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 
LVL 58

Expert Comment

by:Gary
ID: 39932920
Code makes absolutely no sense!
Why are you selecting all records just to get the number of rows?
Then you have a loop equal to number of records (without using the data) and 'create' sql statements that you never use except for the very last one that is created? Which you could easily get just by doing
$SQL1 = "SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,$n";

What are you trying to do?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39932988
Agree with Gary (I usually do) and I hope you can show us the data structure and describe the business objectives in your application.  There are probably good design patterns that can help you, if we can understand the overall goals.
0
 

Author Comment

by:lericson
ID: 39933060
I am trying to achieve the following:

In the database I have n lines of records. I want to display these records one by one, one after the other,
I tried to single them out and present them by using this piece of code:
$SQL100 = "SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,$i";
That works for all but the one with the lowest "resanr" value. I trust code like "LIMIT 1,0" does not work. Therefore I tried to grab hold of that record and use the if sentence
if($SQL100 == "SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1,0")
{
      $SQL1 = "SELECT * FROM evenemang ORDER BY resanr ASC LIMIT 1";
}
to convert it from "LIMIT 1,0" to "LIMIT 1". That doesn't work for one reason or the other.
I wanted to see all the queries produced by the code piece  echo "SQL1: ".$SQL1."<br />";. To my satisfaction also the line with"LIMIT 1" is printed out. But it is not used when listing all the records.
The values used in the test for "$beskrivning" are as follows:

Where resanr equals                       The value for beskrivning is
  1                                                             Resa 1
  2                                                             Resa 2
  3                                                             Resa 3
  ...
  9                                                             Resa 9

I had expected to see an output like the following
Resa 1
Resa 2
Resa 3
...
Resa 9

But instead I get
Resa 2
Resa 3
...
Resa 9
Resa 1 is lacking. I would like to show all the n (in this case n=9) values.
The problem is probably associated with the code "LIMIT 1,$n". But I don't know what code to use so I get all the n records.
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39933065
Why are you limiting if you want all records? Just do

$SQL1 == "SELECT * FROM evenemang ORDER BY resanr ASC")

$ret1 = mysql_query($SQL1);
if (!$ret1) { echo( '[1] '.mysql_error()); }
else {
while ($row = mysql_fetch_array($ret1)) {
// echo our your rows

Open in new window


All the other code is just a waste.
0
 

Author Closing Comment

by:lericson
ID: 39933078
Thanks Gary!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now