SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is this mySQL error mean ?

    Hi guys, i have this problem with this error here , its giving me tons of problems on my database.okay here is the table layout..i got 2 tbls, contact and school, contact tbl record will have a Foreign key to the school tbl record.

    Example:
    contact tbl record
    Contact_ID(PRI)
    School_ID(FK)
    Name
    Telephone

    school tbl record
    School_ID(PRI)
    School_Name
    School_Add

    so my algorithm was to actually create a new school record on a mySQL database, do a "select * from school", retrive out the latest school record primary key and use it to insert data into the contact tbl record along with the foreign key.

    The error always props up when the database is empty...

    Many thanks in advance for dishing out the advice

    Warning: Offset 1 is invalid for MySQL result index 2 in c:\apache\htdocs\coding\customer\addnewcustomer.php on line 190
    s.SchoolID:2

    it points back to this code in my script

    PHP Code:
    $num =  mysql_num_rows($newSchoolResult);
        
        if(
    $num == 1)
        {    
            echo 
    "No records initially.";
            echo 
    "</br>"
            
    $num ==1
        
        }
        else
        {    
            echo 
    "There are records now.";
            echo 
    "</br>"
            
    $num $num -;
        
        }
        
        echo 
    "</br>";
        echo 
    "current num of records:".$num;
        echo 
    "</br>";
        
        
    $row mysql_data_seek($newSchoolResult,$num); //line 190
        
    $row mysql_fetch_row($newSchoolResult); 

  2. #2
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you already know how many records are in the db, use that variable...
    PHP Code:
    if($num$row mysql_data_seek($newSchoolResult,$num); //line 190 
    Lats...

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hmm so do i replace this portion of the code with your code ?

    Quote Originally Posted by Lats
    As you already know how many records are in the db, use that variable...
    PHP Code:
    if($num$row mysql_data_seek($newSchoolResult,$num); //line 190 

    HI Lats, thanks for replying my post, but just to confirm, do i replace the following code below with your solution ?

    Just curious, which surburb in Victoria your in ? I'm in Frankston

    regards
    PHP Code:
    [I]$num =  mysql_num_rows($newSchoolResult);
        
        if(
    $num == 1)
        {    
            echo 
    "No records initially.";
            echo 
    "</br>"
            
    $num ==0
        
        }
        else
        {    
            echo 
    "There are records now.";
            echo 
    "</br>"
            
    $num $num -;
        
        }
        
        echo 
    "</br>";
        echo 
    "current num of records:".$num;
        echo 
    "</br>";
        
        
    $row mysql_data_seek($newSchoolResult,$num); //line 190[/I] 

  4. #4
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just the 'line 190' bit.

    Your location shows Singapore, that's a bit north of Frankston

    Werribee here.
    Lats...

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Still can't work

    Quote Originally Posted by Lats
    Just the 'line 190' bit.

    Your location shows Singapore, that's a bit north of Frankston

    Werribee here.
    ohh i'm a international student here thats why...aniway, it still can't work. the same error happends

  6. #6
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, try...
    PHP Code:
    if($num 0)... 
    Lats...

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Nopey still negative

    Quote Originally Posted by Lats
    Okay, try...
    PHP Code:
    if($num 0)... 

    nah still the same,problem is with extracting the 1st record,subsequent records are fine, its te first record thats giving me all the problems

  8. #8
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try adding that num test to line 191.
    Lats...

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Should not work

    Quote Originally Posted by Lats
    Try adding that num test to line 191.
    are u referring to the if($num >0 ) ??

    if yes, it will not make a difference, the fact that i INSERT a new school tbl record into the database will always yield a number > 0 when i use the num_rows() method.

  10. #10
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Portion of the code

    Quote Originally Posted by wilsontanweeyang
    are u referring to the if($num >0 ) ??

    if yes, it will not make a difference, the fact that i INSERT a new school tbl record into the database will always yield a number > 0 when i use the num_rows() method.
    i posted the code snippet of whats was my script supposed to do, hope it helos u in gaining a clearer picture ..

    thanks


    PHP Code:
    //**********************************************************************
        // Forming the new school add query
        //**********************************************************************
        
        
    $schoolAddQuery  "INSERT INTO school (School_Name,School_Address,
                            School_Postcode,School_State,School_Contact,
                            School_Email,School_ABN_No,School_ACN_No,isActive,
                            User_ID,Password) VALUES('"
    $schoolName." ','"
                            
    $schoolAddress "','"$schoolPostcode" ','"
                            
    $schoolState ." ','"$schoolContact." ','"
                            
    $schoolEmail." ','"$schoolABNNum ." ','".
                            
    $schoolACNNum ." ','".$isActive."','".$userID." ','"
                            
    $password." ')" ;
                 
             
    //**********************************************************************
        // Executing the add query inserting a new add record
        // on the database.
        //**********************************************************************
             
             
    $addSchoolResult =  mysql_query($schoolAddQuery);
             
             
             
    //**********************************************************************
        // Checks if there is any errors that caused the school query to fail
        //**********************************************************************
             
         
    if(!$addSchoolResult)
        {    
    $queryInfo "There was an error adding the school record to table";
            
    displayQueryError($queryInfo);
        }
        else
        {
            echo 
    "</br>";
            echo(
    "School query succeded");
            echo 
    "</br>";
        }
         
        
    $newSchoolResultQuery "SELECT * from school" ;
        
    $newSchoolResult      mysql_query($newSchoolResultQuery);
            
        
    //**********************************************************************
        // Checks if there is any errors that caused the school query to fail
        //**********************************************************************
            
        
    if(!$newSchoolResult)
        {    
            
    $queryInfo "There was an error retriveing the school record to table";
            
    displayQueryError($queryInfo);
        }
        else
        {
            echo 
    "</br>";
            echo(
    "Select School data query succeded");
            echo 
    "</br>";
        
        
        
    //**********************************************************************
        // Obtains the latest record's primary key for insertion to contact 
        // record as the Foreign Key field
        //**********************************************************************
        
        
    $num =  mysql_num_rows($newSchoolResult);
        echo 
    "</br>";
        echo 
    "current num:".$num;
        echo 
    "</br>";
        if(
    $num == 1)
        {    
            echo 
    "No records initially.";
            echo 
    "</br>"
            
    $num ==0
            
        
        }
        else
        {    
            echo 
    "There are records now.";
            echo 
    "</br>"
            
    $num $num -;
            
        
        }
        
        echo 
    "</br>";
        echo 
    "current num of records:".$num;
        echo 
    "</br>";
         
        
    $positive mysql_data_seek($newSchoolResult,$num); //line 190
        
    echo "positive:".$positive
        echo 
    "</br>";
        if(
    $positive)
        {    
            
    $row mysql_fetch_row($newSchoolResult);
        }    
        
    //echo "</br>";
        //echo "</br>";
        //echo "schoolID:.$row[0]"; 
        
        //**********************************************************************
        // Forming the new contact add query
        //**********************************************************************
        
    $contactSchoolID $row[0];                
              echo 
    "s.SchoolID:".$contactSchoolID;
              echo 
    "</br>";
              
    $contactAddQuery "INSERT INTO contact(School_ID,First_Name,Surname,Title,Job_Title,Mobile_no,
                                  Phone_No,Email_Add) VALUES('"
    $contactSchoolID." ','".$contactFirstName." ','".$contactSurname." ','".$contactTitle." ','".$contactJobTitle." ','".$contactMobileNum." ','".$contactPhoneNum." ','".$contactEmailAdd."')" ;
        
        
    //**********************************************************************
        // Executing the add query inserting a new add record
        // on the database.
        //**********************************************************************
        
        
    $addContactResult mysql_query($contactAddQuery);
        
        } 
    // end if(!$newSchoolResult)
         
        //**********************************************************************
        // Checks if there is any errors that caused the contact query to fail
        //**********************************************************************
            
        
    if(!$addContactResult)
        {    
            
    $queryInfo "There was an error adding the contact record to table";
            
    displayQueryError($queryInfo);
        }
        else
        {
            echo 
    "</br>";
            echo(
    "Contact query succeded");
            echo 
    "</br>";
        } 

  11. #11
    SitePoint Wizard Lats's Avatar
    Join Date
    Jun 2003
    Location
    Melbourne, AU
    Posts
    1,142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Clear as mud

    In this part...
    PHP Code:
        if($num == 1
        {     
            echo 
    "No records initially."
            echo 
    "</br>"
            
    $num ==0
             
         
        } 
    Are you meaning to assign 0 to $num? At the moment it's not doing anything.
    Lats...

  12. #12
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    Singapore
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Lats, i use phpMyAdmin to view the database tbls in the raw form and i realised the following arragnement of records..

    I used phpMyAdmin to check the records on my machine and the problems can be summarised as below

    Assume the following 3 records been entered

    school TABLE
    School_ID:1,School_Name:Monash //record 1
    School_ID:2,School_Name:Monash //record 2
    School_ID:3,School_Name:Monash //record 3

    contact TABLE
    Contact_ID:1,School_ID(FK):1,Name:AA //record 1
    Contact_ID:2,School_ID(FK):2,Name:BB //record 1
    Contact_ID:3,School_ID(FK):3,Name:CC //record 1


    ********AFTER DELETING record 2 in both tbls*********

    school TABLE
    School_ID:1,School_Name:Monash //record 1
    School_ID:3,School_Name:Monash //record 3

    contact TABLE
    Contact_ID:1,School_ID(FK):1,Name:AA //record 1
    Contact_ID:3,School_ID(FK):3,Name:CC //record 1

    ********AFTER Adding 4th record into both tbls*********
    school TABLE
    School_ID:1,School_Name:Monash //record 1
    School_ID:4,School_Name:Monash //record 4
    School_ID:3,School_Name:Monash //record 3

    contact TABLE
    Contact_ID:1,School_ID(FK):1,Name:AA //record 1
    Contact_ID:4,School_ID(FK):3,Name:AA //record 1
    Contact_ID:3,School_ID(FK):3,Name:CC //record 1

    As i try to add a 5th record into the DB, it will insert at the bottom. in the school tbl with the School_ID = 5

    many thanks for helping me out.
    wilson


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
  •