循环通过MySQL行和拆分。最初的Excel电子表格

时间:2014-01-30 05:00:29

标签: php mysql regex strstr

所以我需要做的是: 我有一个这样的领域:

  

Chris Mayers 2澳大利亚墨尔本安东尼街。

我需要拆分Chris Mayers部分并将其放在名为“Name”的列中,然后离开“2 Anthony Street etc”部分。

我需要拆分大约1200行,并确定我需要使用的分隔符如下: 数字1-10,然后是Level,Suite或Unit。

这是我目前的代码。

<?php
// Create connection
$con = mysql_connect("localhost","root","","db");

// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  } else
    {
        echo "Connection Established.";
    }

    $db_selected = mysql_select_db('db', $con);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}    else
{
    echo "DB Selected";
}

$sql = "SELECT `ADDRESS` FROM `table 1`";
$result = mysql_query($sql) or die(mysql_error());
$i = '1';
while($row = mysql_result($result,$i))
{
    if($name = strstr($row, '1', true))
    //$row contains the company name and address.
    {
        $sql = "UPDATE `table 1` SET `NAME` = '$name'";
        $yeah = mysql_query($sql) or die(mysql_error());
    }   else if($name = strstr($row, '2', true))
        {
            $sql = "UPDATE `table 1` SET `NAME` = '$name'";
            $yeah = mysql_query($sql) or die(mysql_error());
        }   else if($name = strstr($row, '3', true))
            {
                $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                $yeah = mysql_query($sql) or die(mysql_error());
            }   else if($name = strstr($row, '4', true))
                {
                    $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                    $yeah = mysql_query($sql) or die(mysql_error());
                }   else if($name = strstr($row, '5', true))
                {
                    $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                    $yeah = mysql_query($sql) or die(mysql_error());
                }   else if($name = strstr($row, '6', true))
                {
                    $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                    $yeah = mysql_query($sql) or die(mysql_error());
                }   else if($name = strstr($row, '7', true))
                {
                    $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                    $yeah = mysql_query($sql) or die(mysql_error());
                }   else if($name = strstr($row, '8', true))
                {
                    $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                    $yeah = mysql_query($sql) or die(mysql_error());
                }   else if($name = strstr($row, '9', true))
                {
                    $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                    $yeah = mysql_query($sql) or die(mysql_error());
                }   else if($name = strstr($row, 'Level', true))
                {
                    $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                    $yeah = mysql_query($sql) or die(mysql_error());
                }   else if($name = strstr($row, 'Unit', true))
                {
                    $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                    $yeah = mysql_query($sql) or die(mysql_error());
                }   else if($name = strstr($row, 'Suite', true))
                {
                    $sql = "UPDATE `table 1` SET `NAME` = '$name'";
                    $yeah = mysql_query($sql) or die(mysql_error());
                }
            $i++;
}
echo "Cycle finished";
?>

它不起作用。它已在名称列的每个字段中输入相同的名称..

我是这种自动化的初学者,所以任何帮助都表示赞赏。在我身上轻松一下;)

2 个答案:

答案 0 :(得分:0)

您需要==进行比较(不是=

if($name == strstr($row, '2', true))

当然,您必须在使用if的许多=语句中对其进行更改。

答案 1 :(得分:0)

请使用mysqli而不是mysql。 Mysql被折旧,其次在更新行时定义where子句,查询看起来像这样;

$i = 0;
while ( $row = mysql_fetch_array($result) ) {
   if($name == strstr($row['ADDRESS'], '2', true)) {
    $sql = "UPDATE `table 1` SET `NAME` = '".$name."';
    $yeah = mysql_query($sql) or die(mysql_error());  
    $i++;
   }
}