473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP/MySQL Row ID's & Num_Rows

4
Basically, I need a way of getting around a problem I recently found out...

When I don't have a sequential row ID pattern (1,2,3,4), which comes up like (2,5,6), the script will return absolutely nothing.

This is my current PHP code:
[PHP]<?php
$sql_queries[1] = "SELECT id FROM `news`";
$sql_query = mysql_query( $sql_queries[1] ) or die();
$sql_num_rows = mysql_num_rows( $sql_query ) or die();
for ( $i = $sql_num_rows; $i > 0; $i-- )
{
$sql_queries[2] = "SELECT type,date,headline FROM `news` WHERE id = '".$i."'";
$sql_query2 = mysql_query( $sql_queries[2] ) or die();
$sql_array = mysql_fetch_array( $sql_query2 ) or die();
echo "<tr>\n";
echo "<td>".$sql_array['date']."</td>\n";
echo "<td>".$sql_array['headline']."</td>\n";
echo "<td>".$sql_array['type']."</td>\n";
echo "<td align=\"center\"><a href=\"index.php?page=news&act=edit&id=".$i."\"><i mg src=\"images/form_edit.png\" alt=\"Edit\"></a></td>\n";
echo "<td align=\"center\"><a href=\"index.php?submit=true&page=news&act=list&id =".$i."\"><img src=\"images/form_delete.png\" alt=\"Delete\"></a></td>\n";
echo "</tr>\n";
}
?>[/PHP]

So I basically need a workaround for listing a table that will show all "news" rows, even if the IDs weren't in a specific sequence (eg; I deleted a few articles, added and modified some, etc).
Mar 12 '07 #1
9 11298
ak1dnar
1,584 Expert 1GB
For a Small work You have code lots of Thing. ;)
Please Try to change my Coding as suite to Your Requirement.
Here in my Coding I am using a Another Table Named Products.And from that I am going to fetch all the values. And in <TD> i am going to Display product ID again as the out put. Along with that to your edit And delete pages, again the same ID will Pass. Since it is in side the while loop, I can Print it for Any Number of Rows in the table.


[PHP]<?php
require('dbcon.php');
$sql = "SELECT * FROM products"; // change This Line
$result = mysql_query($sql) or die();
while( $row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['p_id']."</td>\n";//change the column Names
echo "<td align=\"center\"><a href=\"index.php?page=news&act=edit&id=".$row['p_id']."\"><img src=\"images/form_edit.png\" alt=\"Edit\"></a></td>\n";
echo "<td align=\"center\"><a href=\"index.php?submit=true&page=news&act=list&id =".$row['p_id']."\"><img src=\"images/form_delete.png\" alt=\"Delete\"></a></td>\n";
echo "</tr><br>";
}
?> [/PHP]
Mar 12 '07 #2
Teckie
4
For a Small work You have code lots of Thing. ;)
Please Try to change my Coding as suite to Your Requirement.
Here in my Coding I am using a Another Table Named Products.And from that I am going to fetch all the values. And in <TD> i am going to Display product ID again as the out put. Along with that to your edit And delete pages, again the same ID will Pass. Since it is in side the while loop, I can Print it for Any Number of Rows in the table.


[PHP]<?php
require('dbcon.php');
$sql = "SELECT * FROM products"; // change This Line
$result = mysql_query($sql) or die();
while( $row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>".$row['p_id']."</td>\n";//change the column Names
echo "<td align=\"center\"><a href=\"index.php?page=news&act=edit&id=".$row['p_id']."\"><img src=\"images/form_edit.png\" alt=\"Edit\"></a></td>\n";
echo "<td align=\"center\"><a href=\"index.php?submit=true&page=news&act=list&id =".$row['p_id']."\"><img src=\"images/form_delete.png\" alt=\"Delete\"></a></td>\n";
echo "</tr><br>";
}
?> [/PHP]
I noticed that in your script you used "while( $row = mysql_fetch_assoc($result))". What exactly does this do? I found this in a few tutorials but none of them seemed to have explained what it serves in the script.

Thanks for your help though, I'll try it out. :)
Mar 12 '07 #3
ak1dnar
1,584 Expert 1GB
you are welcome any time. :) Click here to find out the more about this function.
Mar 12 '07 #4
ronverdonk
4,258 Expert 4TB
Example:
Expand|Select|Wrap|Line Numbers
  1. while (1 == 1) {
  2.   // code
  3. }
  4.  
The 'while' loop executes as long as the condition between the parentheses is true. Since in this example the expression is aways TRUE, the while will run forever.

In your post you have a condition '$row = mysql_fetch_array($result)'. mysql_fetch_array is a function that returns either a row or FALSE (when there are no more rows).

So your statement
Expand|Select|Wrap|Line Numbers
  1. $row = mysql_fetch_array($result)
says:
Execute the code within the while loop as long as the result of the condition ($row=...) is true, i.e. as long as there are rows to retrieve. When there are no more rows the while loop will end.

Hope this is clear.

Ronald :cool:
Mar 12 '07 #5
Teckie
4
Example:
Expand|Select|Wrap|Line Numbers
  1. while (1 == 1) {
  2.   // code
  3. }
  4.  
The 'while' loop executes as long as the condition between the parentheses is true. Since in this example the expression is aways TRUE, the while will run forever.

In your post you have a condition '$row = mysql_fetch_array($result)'. mysql_fetch_array is a function that returns either a row or FALSE (when there are no more rows).

So your statement
Expand|Select|Wrap|Line Numbers
  1. $row = mysql_fetch_array($result)
says:
Execute the code within the while loop as long as the result of the condition ($row=...) is true, i.e. as long as there are rows to retrieve. When there are no more rows the while loop will end.

Hope this is clear.

Ronald :cool:
Ahh, thanks!

Thank you both for your help. The function does work and I've implemented it in all of the areas I needed to, and they all function excellent. This couldn't have been anymore quicker, I'm quite impressed.

Is there a way to reverse this function? Such as "id" 10-1, instead of 1-10?
Mar 12 '07 #6
ak1dnar
1,584 Expert 1GB
from SQL script itself you can make it.
Expand|Select|Wrap|Line Numbers
  1. Select * from news GROUP BY id ORDER BY id asc
for Ascending order you can use ASC
for Descending order you can use DSC
Mar 12 '07 #7
ronverdonk
4,258 Expert 4TB
Ahh, thanks!

Thank you both for your help. The function does work and I've implemented it in all of the areas I needed to, and they all function excellent. This couldn't have been anymore quicker, I'm quite impressed.

Is there a way to reverse this function? Such as "id" 10-1, instead of 1-10?
Yes, when you want to MySQL select only the rows with column 'id' 1-10 in reverse order, do
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM table WHERE id>=1 AND id<=10 ORDER BY id DESC;
Ronald :cool:
Mar 12 '07 #8
Teckie
4
Of course, I was thinking too much on the PHP side, I forgot all about the SQL order by.

Thanks again. :)
Mar 12 '07 #9
ronverdonk
4,258 Expert 4TB
You are welcome! See you next time.

Ronald :cool:
Mar 12 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Paris_Sucks | last post by:
I'm trying to redirect when testing for certain condidtions as shown below. When the conditions are ture, it redirects, but still goes ahead and processes the sql query. What am I doing wrong??? ...
3
by: Richard M | last post by:
Can anyone please lend me a hand, I'm trying to design a cookie based password system. I have managed to do everything bar one thing that is driving me crazy. I have a MYSQL database called...
0
by: Quintin | last post by:
Hi , I'm trying to make an app in vc++ that access my MySQL db and then email me some data. I'm mailing with winsock. Whenever i email with winsock then i can't connect with MySQL db(anymore)....
2
by: PHP_Paul | last post by:
Ok, I'm trying to poineer into the wonderful area of PHP/MySQL programming, but I'm having some difficulties. http://www.paulhq.com/php/freepage.html should register, but when anyone fills something...
1
by: automation | last post by:
There is a truncation error of my Web Application using PHP 5.04, MySQL 5.022, and HTML(IE 6.0) whereby the MySQL Result Set is being truncated on the HTML page, even though the CSS Div Page Height...
221
Atli
by: Atli | last post by:
You may be wondering why you would want to put your files “into” the database, rather than just onto the file-system. Well, most of the time, you wouldn’t. In situations where your PHP application...
1
by: JPhilS | last post by:
Hi to all Webmaster! I have discover this problem when I inserted the scores of the students i a centain subject. I am making a school project with regards to saving students' record. first, I...
6
by: shauna00 | last post by:
Hi guys, I want to basically take in a username and password and then check to see if its in the mySQL database. The problem is though that I can print the variables yet when I use then in the php...
2
bilibytes
by: bilibytes | last post by:
hi, i tried to create sort of a db abstraction layer in order to be able to change of database (Mysql | oracle | pg |...). i implemented that on a obejct oriented way. i made a sql factory in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.