0

So what I need to do is this: I have a field like this:

Chris Mayers 2 Anthony Street Melbourne, Australia.

I need to split the Chris Mayers part and put it in a column called 'Name' and then leave the "2 Anthony Street etc" part.

I have around 1200 rows I need to split and I have determined that the delimiters I will need to use are as follows: The numbers 1-10 and then Level, Suite or Unit.

This is the code I currently have.

<?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";
?>

It's not working. It has entered the same name into every field on the name column..

I'm a beginner at this kind of automation so any help is appreciated. Go easy on me ;)

KriiV
  • 1,882
  • 4
  • 25
  • 43

2 Answers2

0

You need == for comparison (not =)

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

Of course, you will have to change it in the many if statements where you have used =.

mseifert
  • 5,390
  • 9
  • 38
  • 100
0

Please use mysqli not mysql. Mysql is depreciated and secondly while updating the rows define where clause and your query look like this;

$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++;
   }
}
Agha Umair Ahmed
  • 1,037
  • 7
  • 12
  • That just gives me this: Warning: strstr() expects parameter 1 to be string, array given in .... Line 27. – KriiV Jan 30 '14 at 05:54