Add column


I create php script with SELECT statement and I get table from MySQL database.How to add column to the output in the way that first row has number 1,second number 2,…


Uuh … $i++ in your loop?

Seriously, you’re providing us with way too little information to actually be able to give a decent answer. What does your script look like, what have you tried, what have you searched, which results did you get and why did they not suit your needs?


I know I shouldn’t be doing this and have a feeling Zyppora knew this, but was trying to help our beginners to become better posters, but I am sure they are talking about a id column of some sort in which it auto increments.

I am not sure how are wanting to do this as Zyppora has said not enough information was provided.

You could add a column to your table by means of writing a small script to do so, or using phpMyAdmin or some similar tool.

Ok, just thought of something, is this MySQL, MSSQL or something else.

Zyppora is right, for us to be able to give you any kind of useful information without wasting our time, we need more information from you.



<?php $conn = mysql_connect("localhost", "root", ""); mysql_select_db("db", $conn); $sql = "SELECT L.ID,L.BAR_CODE,,SUM(qty),SUM(pay) FROM table1 AS L INNER JOIN table2 AS A ON L.ID=A.ID1 where date between '$var1' and '$var2' GROUP BY L.ID order by sum(pay) desc"; $result = mysql_query($sql, $conn); print "n"; print "n"; while ($field = mysql_fetch_field($result)){ print " n"; } // end while print "nn"; while ($row = mysql_fetch_assoc($result)){ print "n"; //look at each field foreach ($row as $col=>$val){ print " n"; } // end foreach print "nn"; }// end while print "
n"; ?>


Admin Edit: Added [php] tags for readability. Please refer to for Posting guidelines.


Are you trying to add a new record to the table or are you trying to add entire new column to the table?


I want to add new column in output.(original table haven’t this column)
Column name:RBR
first row:1
second row:2
last row:xxxxx


You will need to look into using the ALTER table command for this. is a good starter for looking up the syntax of this.


I can’t ALTER table because I use SUM function.
One product_id can be find on many rows in table and with SELECT command
I want to receive all product_Id with SUM of their quantity.
How to use loop in my case with i++


while ($myArray = mysql_fetch_array($resource)) {
// Do Other Stuff Here

That’s your looped $i++. It’s really annoying for me (and I’m sure for others that are willing to help out as well) to actually have to post and explain to you the very basics of PHP. All of this can be found in the PHP Manual website, with a little bit of effort. You asking this teaches me that you’re not a PHP developer, nor are willing to put forth the energy to become one, and it makes me reluctant to help you out (however I will do so, considering I’m a bonafide co-user of the Internet).

I assume you’re trying to list products on a page? How about something like this (pseudo-code):

$i = 0;
$mysql > SELECT * FROM products;
while ($product) {
echo $i++;
echo product[details];

You’re going to have to fill the rest in by yourself, as you provided us with no information whatsoever about the environment your script is running in, or about what exactly you need.


there is no need to be nervous!!!
I solve the problem myself!
first i create temporary table and after that add column (auto_increment) to temp table.
Finally put SELECT * FROM TABLE statement .