SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error with code - text followed by database entry

    Hi there,

    I want to enter a code into a MySQL database. I want it to start and continue like this:

    AGPS-1000000
    AGPS-1000001
    AGPS-1000002
    AGPS-1000003
    etc.

    I will code it to always display "AGPS-"

    In the MySQL database I will store 1000000. Then auto-increment it.

    The problem is with coding the retrieval of the code:

    This does not work:

    PHP Code:
    The code $ThisVendorTxCode "AGPS-"$row["vendor"]; 
    This code works:

    PHP Code:
    $ThisVendorTxCode "AGPS-";
    $row["vendor"]; 
    but it does not get the vendor code because the ";" ends the request. But without it it says there is an error in the code.

    How do I get the AGPS- at the start followed by what is in the database? How do I code it?

    Matt.

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should use the concatenation operator. Try "AGPS-" . $row['vendor'];

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - thanks for that. it is working well now.

    But my problem now is i get this error my PHPMyAdmin

    #1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key


    how do i auto-increment more than one?

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    Why do you need to auto-increment more than one?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,495
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    You can't. Why would you want more than one autoincrement column in one table?

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2011
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll use the one.

  7. #7
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    You can also solve this on database level, which should be more efficient:

    If you auto-increment on column vendor:
    Code mysql:
    SELECT CONCAT("AGPS-", vendor) AS vendor

    If you auto-increment on column id, and where id equals the last digits in vendor:
    Code mysql:
    SELECT CONCAT("AGPS-", 1000000 + id) AS vendor

    Alternately (likely slower, but also more versatile if you can't use simple addition):
    Code mysql:
    SELECT CONCAT("AGPS-1", LPAD(id, 6, "000000")) AS vendor
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •