Reply
Getting values from MySql using PHP
Old 03-17-2005, 10:08 PM Getting values from MySql using PHP
Experienced Talker

Posts: 36
Hi:

I wonder if someone can help me with the following:

I'm trying to retrieve data from a MySql row using an appropriate SELECT statement. I would like to do this based on the last row of data that exists in the table. Also, get all entries except the first, which in this case would be "order_no", and any fields which may be empty or have zeroes for values.

I'm using the current select statement:

PHP Code:
$sql="SELECT * FROM order_desc ORDER BY DESC 0,1"
Is this statement correct? Also, I think I probably need a foreach statement but I have no idea on how to code it.

Any ideas?

Last edited by common_sense : 03-17-2005 at 10:09 PM. Reason: Forgot to properly identify the php code being stated
common_sense is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 03-18-2005, 07:54 AM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
The code you have there just assigns your sql to a string. It won't do anything to the database until you call one of PHPs mySQL functions. Also your ORDER BY syntax is incorrect - you have to tell it a column name to sort by. You will also need to connect to your database at the start of the script if you are not doing this already.

PHP Code:
mysql_connect("localhost","your database name","your database password");

$sql="SELECT * FROM order_desc WHERE order_no != 0 AND some_other_field IS NOT NULL ORDER BY order_no DESC");

$result mysql_query($sql); // This is what performs the query

while($row mysql_fetch_array($result)) {
  echo 
$row['order_no'];
  echo 
"<br />";


That code above will connect to your database, find all the records which have an order_no that is not 0, and makes sure that some_other_field (which you replace with whatever field name you want to check) has a value set and is not null. Then the while loop at the bottom goes through the results one by one and echoes the order_no of each record to the browser.
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-18-2005, 12:49 PM
Experienced Talker

Posts: 36
Hi:

Looking at what Oberon stated, its true that the select statement is written wrong, it should read as follows:

PHP Code:
$result mysql_query("SELECT * FROM order_desc ORDER BY order_no DESC LIMIT 0,1") or die("SELECT Error: ".mysql_error()); 
.

I'm just quoting the piece of code I'm working on as I've already connected and selected the database.

The following piece of code gets me all values in the row:

PHP Code:
$get_info mysql_fetch_array($resultMYSQL_ASSOC); 
Just to clear up the picture of what I'm trying to do: From a particular order form (order_info.html) data is sent to the table order_info and recorded in a particular row. As I'm trying to retrieve data only from the last row for my purposes, I need to get all data except order_no and any fields that may be null or have zero as value. Let me mention that a row consists of 16 fields but not all of them will be filled at any one time.

As an example, if the order consists of QTY_1, DESC_1, and QTY/PAGE1, that leaves me with 13 other fields that I don't want. What if instead of just 1 entry, there is one as follows:

QTY_1 DESC_1 QTY/PAGE1
QTY_2 DESC_2 QTY/PAGE2
QTY_3 DESC_3 QTY/PAGE3

how can I get just this data and nothing else. I must mention that after the entry of 3 different forms, a form must be produced using data that may exist on the last row only and that entries may consist of 3(4 with order_no) or 15(16 with order_no).
common_sense is offline
Reply With Quote
View Public Profile
 
Old 03-18-2005, 04:03 PM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
Edit: Never mind, that post was dumb.
__________________
Free Teacher Websites

Last edited by Phaedrus : 03-18-2005 at 04:57 PM.
Phaedrus is offline
Reply With Quote
View Public Profile
 
Old 03-18-2005, 06:26 PM
paid2surf's Avatar
Skilled Talker

Posts: 59
Location: WI
order_desc is the name of your table? that seems a bit odd.

there should be a lot of examples at www.php.net/mysql
paid2surf is offline
Reply With Quote
View Public Profile Visit paid2surf's homepage!
 
Old 03-18-2005, 07:22 PM
Gaffer Sports's Avatar
Ultra Talker

Posts: 397
Name: Steve
Location: Scotland
Quote:
Originally Posted by paid2surf
order_desc is the name of your table? that seems a bit odd.

there should be a lot of examples at www.php.net/mysql
Odd? I may be guessing, but it will more than likely be 'Order Description'


Common_Sense:

as much as I have tried to decypher what you are wanting, unless I see more of the code I cannot say, sorry bud.

Steve.
__________________
Media Help & Discussion
Gaffer Sports is offline
Reply With Quote
View Public Profile Visit Gaffer Sports's homepage!
 
Old 03-27-2005, 11:19 AM
Experienced Talker

Posts: 36
Got it guys, thanks for the different tips (I tried all of them to see which one would do what I was looking for)!
common_sense is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Getting values from MySql using PHP
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.47333 seconds with 12 queries