Errors parsing CSV - Page 2
Page 2 of 2 FirstFirst 12
Results 16 to 25 of 25

Thread: Errors parsing CSV

  1. #16
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,830
    Quote Originally Posted by timstring View Post
    Gimme half a duck: changing this

    PHP Code:
    $mysqli = new mysqli($host$user$password$Dbname) or die('host doesnt work ' $mysqli->error);

    if(!
    $stmt $mysqli->prepare("SELECT `WorkDate` FROM `in_time_appts` WHERE `WorkDate` = ?")) ( 
    to this:
    PHP Code:
    $cxn = new mysqli($host$user$password$Dbname) or die('host doesnt work ' $mysqli->error);

    if(!
    $stmt $cxn->prepare("SELECT `WorkDate` FROM `in_time_appts` WHERE `WorkDate` = ?")) 

    Now, I'm getting errors on down the line. Film at 11:00
    An important realization in PHP is that the PHP file must have valid syntax before PHP will even try to run a single line of code. If a script does not have valid syntax, then PHP may tell you this (as well as where the syntax problem was found) by showing an error, but you may also get NO ERROR OUTPUT AT ALL if PHP is configured with error reporting off. It all depends on one's error_reporting settings.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  2. #17
    Senior Member
    Join Date
    Sep 2012
    Posts
    269
    Y;'al need to permit the author of a post to delete it. I started editing the post less than 3 minutes after creating it. Now, I'm getting replies based on the original post and not the revisions that are in process.



    hey dopie is closed off. Amazing I must have figured that out.

    I am not getting any syntax errors.

    Now,
    PHP Code:
     if(!$stmt $cxn->prepare("SELECT `WorkDate` FROM `in_time_appts` WHERE `WorkDate` = ?")) 
    {
        echo 
    "Hey Dopie  " $mysqli->error;
    }

    if(!
    $stmt->bind_param('s'$WorkDate)) 
    {
    echo 
    "Yo Moron  " $mysqli->error;
    }
    if(!
    $stmt->execute());   ///  this is always tripping TRUE and continuing straight down
        


    Now, the insert statement isn't working with this error:
    Code:
     Commands out of sync; you can't run this command now

    You're extremely right about building houses. I did a couple of relatively easy routines here, and now the powers that be expect a few miracles.
    Last edited by timstring; 05-08-2013 at 05:47 PM.

  3. #18
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,830
    1) If 'hey dopie' was actually closed off, then you didn't send me your actual code, in which case I was commenting on what, exactly? Surely you can appreciate that commenting on inaccurate code is a waste of time?
    2) If you are getting a 'blank screen' then I bet you DO have a syntax error. Unfortunately, I can only assume that you are referring to the fact that you tried to access your script, which is running on a server somewhere, by loading its url in a browser. Sadly, you do not appear to have read my prior post about how SOMETIMES YOU WILL NOT GET AN ERROR MESSAGE DEPENDING ON YOUR PHP CONFIGURATION or you would know that a blank screen might in fact mean a syntax error.

    If you cannot write code without syntax errors, you are going to have a really awful time.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  4. #19
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by timstring View Post
    PHP Code:
    if(!$stmt->execute());   ///  this is always tripping TRUE and continuing straight down 
    What do you mean by "continuing straight down"?

    edit:
    nevermind; I just saw it. You have an error:
    PHP Code:
    if(!$stmt->execute()); //<-- note the semicolon.  This is the same as saying "if this is falsy, do nothing."
    {   //<-- with the if() closed off by the semicolon, 
        // this bracket opens a block of code that has no condition attached to it (and therefore, will always run).

    // this is one of the reasons I prefer brackets on the right:
    if( something ){
        
    cool
    }

    // observe:
    if( something );
    {
        
    easy to miss
    }

    if( 
    something );{
        
    just looks wrong

    /edit

    Quote Originally Posted by timstring View Post
    Now, the insert statement isn't working with this error:
    Code:
     Commands out of sync; you can't run this command now
    When you're done with a statement, you need to use mysqli_stmt.close (or mysqli_stmt.reset) before trying to use the connection again.
    Last edited by traq; 05-08-2013 at 11:31 PM.

  5. #20
    Senior Member
    Join Date
    Sep 2012
    Posts
    269
    I have gone from one extreme to another. Now, the 'if' statement is always true and skips around the insert statement. I just need another pair of eyes

    PHP Code:
    <?php

    $lf
    [1] = '';

    $file "TOA";
    if(
    is_dir($file))
      {
      echo (
    "$file is a directory <br>");
      }
    else
      {
      echo (
    "$file is not a directory");
      }
      
    ini_set('auto_detect_line_endings'true);

    $WorkDate $_SESSION['WorkDate'];

    echo 
    $WorkDate;


    $host="localhost";
    $user="root";
    $password="nowhere";
    $Dbname "calls_to_dispatch";

    $cxn = new mysqli($host$user$password$Dbname) or die('host doesnt work ' $mysqli->error);

    // Check connection
    if ($cxn->connect_errno) {
        echo 
    "Hey you dolt, Failed to connect to MySQL: (" $mysqli->connect_errno ") " $mysqli->connect_error;
    }




    if(!
    $stmt $cxn->prepare("SELECT WorkDate FROM in_time_appts WHERE WorkDate = ?")) 
    {
        echo 
    "Hey Dopie  " $cxn->error;
    }

    if(!
    $stmt->bind_param('s'$WorkDate)) 
    {
    echo 
    "Yo Moron  " $cxn->error;
    }


    if(
    $stmt->execute() === FALSE)  /// Now this is tripping incorrectly and skipping from here:
        
    {    
        
        echo 
    'where are you now' '<br>';

    $InputPath "TOA/";


    $try glob($InputPath 'In Time_Late_Early appointments*.csv');
    foreach (
    $try as $ImportFile)
      {
          
    $ReadFile fopen($ImportFile"r") or die("You dope, this file is unreachable");
          
        
    while (
    $LineArray fgetcsv($ReadFile1000",") !== FALSE
    {
        if (!
    stripos($LineArray[0], 'Prov'0))
        {
            
    $BBT $LineArray[0];
            
    $NumAppts $LineArray[1];
            
    $InTime $LineArray[2];
            
    $Late $LineArray[3];
            
    $Early $LineArray[4];
            
    $NotStarted $LineArray[5];
            
    $Cancelled $LineArray[6];
                
            
    $BBTArray explode("-"$BBT);
            
    $TechNum $BBTArray[0];
            


     
            
    $Quest "INSERT INTO in_time_appts
            (WorkDate,
            TechNum, 
            NumAppts, 
            InTime, 
            Late,
            Early,
            NotStarted,
            Cancelled
            ) 
            VALUES ('
    $WorkDate',  
                    '
    $TechNum', 
                    '
    $NumAppts', 
                    '
    $InTime', 
                    '
    $Late',
                    '
    $Early',
                    '
    $NotStarted',
                    '
    $Cancelled
                    )" 
    or die('cant insert' $cxn->error);
                    
                    if(!
    $cxn->query($Quest))
                    
                    {
        echo 
    "Hello out there:  " $cxn->error
    }  


                    
                    for (
    $x 0$x<=4$x++) $BBTArray[$x]='';
                    
                    
            } 
    /// all the way down here 
    }
      }
      
    fclose($ReadFile);
    // unlink($ImportFile) or die('unlink failed  ' . mysql_error());
      

      
    }

  6. #21
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,393
    See the red "Warning" box on the manual page for stripos() that warns you to use a strict comparison operator (e.g. to differentiate between FALSE and 0).

  7. #22
    Senior Member
    Join Date
    Sep 2012
    Posts
    269
    good catch on the stripos, but my problem is outside the bounds of that particular 'if' statement. Besides, the stripos statement has been working.

  8. #23
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,830
    I count about 6 if statements in your code. It would be helpful if you were more concise in describing your problem. I assume you mean this one:
    PHP Code:
        if (!stripos($LineArray[0], 'Prov'0)) 
    You are asking us why this isn't doing what you want when we have no idea what is in your data file? Have you bothered to verify what $LineArray[0] is? You might find some echo statements useful.

    From what I can tell, it looks like you are trying to check the first column of the data row that you have just read from your CSV file to see if it contains the string 'prov'. If you were to RTFM on stripos, you would see its return values described:
    Quote Originally Posted by The Friendly PHP Manual
    Returns the position of where the needle exists relative to the beginnning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.

    Returns FALSE if the needle was not found.
    The important part of that manual entry is where it says "note that string positions start at 0, and not 1".

    Now, imagine that $LineArray[0] starts with Prov. In that case, stripos($LineArray[0], 'Prov', 0) would return zero in which case the code in your if block would be executed. This would also be executed if $LineArray[0] does not contain 'Prov' at all (stripos would return FALSE in that case).

    Given that you have not described to us the contents of $LineArray[0] nor have you described the conditions under which you want the insert command to be executed, we are probably not going to be able to help much.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  9. #24
    Senior Member
    Join Date
    Sep 2012
    Posts
    269
    You guys are majoring in minors. As I said, I am not having any trouble with 'stripos' statement. It works just fine as is. I well understand that stripos starts counting at zero. However, in my data file, the needle is always starts at position 3. I am eliminating the first row of every '.csv' file that I'm importing into MySQL. I have put in echoes and removed echoes and over again.

    However, that doesn't really matter much because this statement isn't working:

    PHP Code:
    if($stmt->execute() === FALSE)
        { 
    This if statement always returns TRUE and skips the next batch of code until the 'if {' is closed off. Take a look at that in my code. I put comments as to the problem area.

  10. #25
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,830
    Perhaps, if you RTFM, you would know that a call to mysqli_ stmt:: execute returns TRUE if the query executed, FALSE if it failed. The friendly manual also goes so far as to inform you that you must use mysqli_stmt_fetch function to find out the result of a query that returns a result:
    Quote Originally Posted by The Friendly Manual
    if the query yields a result set the mysqli_stmt_fetch() function is used.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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