Undefined Variable - Do Switch Statements have scope in PHP

守給你的承諾、 提交于 2019-12-11 01:36:18

问题


Hello to all this is my first time posting, thought it would be good since im utterly stuck. It was my understanding that switch and If/else statements in PHP do not have variable scope.

My issue is I have a CSV file ( a sample one) with about 5 rows of values and I need to get it put into a mySQL DB table( the column headers are represented in my "cases" for my switch statement) BUt anyways I'm parsing the CSV file and checking to make sure that the data is in the column it should be and storing it in a variable. I am then storing all of the variables in an array that is getting serialized and then getting passed into a my SQL query.

I continue to get the error SCREAM: Error suppression ignored for ( ! ) Notice: Undefined variable: company in C:\wamp\www\lcimport\serialize.php on line 98

But I know those variables are there cause I can echo them and they will be called. But unless those errors go away my query will not run thus not populating my db table.

what am I doing wrong?

   <?php
//define some constants
$db = 'lc';
mysql_connect('localhost', 'root', ''); 
mysql_select_db($db);
 mysql_error();
$uid = md5(uniqid(time()));

//we only have this here to be a row counter
$row = 1;
if (($handle = fopen("C:\\wamp\\www\\lcimport\\4records.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        //var_dump($data);
       // echo $data[0];
        echo '';
        if ($row === 1) {
        $header = $data;
       }     

        $row++;
        for ($c=0; $c < $num; $c++) {
            //echo $data[$c] . "<br />\n";
            if ($row === 2) continue;
             switch ($header[$c] ){
              case "Contact":
                      $contact = explode (" ", $data[$c]);
                      $firstName =  $contact[0];
                      $lastName =  $contact[1];
              break;

              case "Company":
                    $company = $data[$c];


              case "Address1":
              //store
                    $address1 = $data[$c];

              break;

              case "Address2":
                    $address2 = $data[$c];

              break;

              case "Address3":
                    $address3 = $data[$c];

              break;

              case "City":
                    $city = $data[$c];

              break;

              case "State":
                    $state = $data[$c];

              break;

              case "Zip":
                    $zip = $data[$c];

              break;

              case "Phone1":
                    $phone1 = $data[$c];
              break;

              case "Phone2":
                     $phone2 = $data[$c];
              break; 

              case "Phone 3":
                    $phone3 = $data[$c];
              break;

              case "Fax":
                    $fax = $data[$c];
              break;          

              case "Accountno":
                    $accountNo = $data[$c];
              break;

              default:
                $junk = $data[$c];
              break;


             }




        }       
        echo $company;  
            $meta = serialize(Array(
                    "firstname" => $firstName,
                    "lastname" => $lastName,
                    "lawfirmname" => $company,
                    'address' => $address1,
                    'city' => $city,
                    'state' => $state,
                    'zip' => $zip,
                    'fulladdress' => '',
                    'officenumber' => $phone1,
                    'faxnumber' => $fax,
                    'mobilenumber' => $phone2,
                    'email' =>'nothing',
                    'website' => 'somthing.com',
                    'privacy' => 0,
                    'status' =>1
                    )); 




        mysql_query("INSERT INTO `mg_profiles` (meta) VALUES ($meta)");
        mysql_error();

    }
    fclose($handle);
    }


?>

回答1:


If every else variables are existing and works correctly, then the missing break; should be the problem on the case of Company.

It is not a good idea to communicate with a relational database within a loop, it is more memory and process efficient to store the results to an array and after all data has ben got, just insert with a simple request, because

INSERT INTO `mg_profiles` (meta) VALUES (...), (...), (...)

works perfectly.

PHP has a command to handle processes like this:

while (($data = fgetcsv($handle, 1000, ",")) !== false) {
    if ($row == 1 || $row == 2) {
        $row++;
        continue;
    }
    list ($contact, $company, $address1 /*, ...*/) = $data;
}



回答2:


Just define it before the conditional statement (before the if())

$company = '';

Since $company comes up only inside a conditional statement, when condition fails, it wud be undefined, hence the error



来源:https://stackoverflow.com/questions/13392132/undefined-variable-do-switch-statements-have-scope-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!