0

I have the php code as below:

<?php
    $conn = mysql_connect("localhost","root","") or die(mysql_error());
    mysql_select_db("dbcsv",$conn);
    $data = array();
    $sql = "select orig,dlvSourceIp from tblcsv group by dlvSourceIp";
    $res = mysql_query($sql) or die(mysql_error());
     echo '<table border="1px">';
     echo "<tr><td>Dormain</td><td>ip of the domain</td><td>Total Mail</td><td>Mail Fail</td><td>Percentage</td></tr>";
        while($data = mysql_fetch_array($res)){
             //echo "<tr><td>".$data['orig']."</td><td>".$data['dlvSourceIp']."</td><td></td><td></td></tr>";
            $t = $data['orig'];
             $getData = array_unique($t);
             var_dump($getData);
}
?>

Note

For $data['orig'] is the array of data that I select it from database it will display: cat cat dog cat dog. I want output like this:cat dog that why I use array unique.

The problem:

I got the error message array_unique() expects parameter 1 to be array, null given in C:\wamp\www\CountLine_CSV\csv2mysql.php on line 21 How do I fix it? Anyone help me please,Thanks.

5
  • 7
    You fix it by passing an array in parameter 1 when calling array_unique instead of a null value ... the error message is pretty clear. The value of $t in your code is quite obviously null. Commented Aug 29, 2012 at 2:48
  • 1
    try doing print_r($data['orig']) . it appears as though the variable $data['orig'] is not an array. It must be an array to use that function. Commented Aug 29, 2012 at 2:48
  • Could you add a little context as to what you're trying to do, and what the content of orig will be? Commented Aug 29, 2012 at 2:49
  • @rdlowrey duh, deleting tht stupid comment in 3, 2, 1... Commented Aug 29, 2012 at 3:18
  • @bfavaretto No problem -- I do stupid stuff from time to time and try really hard to admit it when I do :) Commented Aug 29, 2012 at 3:19

1 Answer 1

1

Your array was stored into the table as a string value (so I think). You need to grab the entry, explode it and create an array, then use array_unique to get your desired effect. Try the following and see if it works:

$t = $data['orig']
$array = explode(" ", $t);
$array = array_unique($array);
print_r($array);

Also, look into using MySQLi or PDO for your query.

So in full, something like the following should suffice:

<?php   
    $mysqli_connection = new mysqli("hostname", "username", "password", "database");
    if ($mysqli_connection->connect_errno) {
        echo ("Connection Failure");
        exit();
    }

    $sql = "SELECT orig, dlvSourceIp FROM tblcsv GROUP BY dlvSourceIp";
    echo '<table border="1px">';
    echo "<tr><td>Dormain</td><td>ip of the domain</td><td>Total Mail</td><td>Mail Fail</td><td>Percentage</td></tr>";
    while($data = $sql->fetch_array(MYSQLI_ASSOC)) {
        $t = $mysqli_connection->real_escape_string($data['orig']);
        $array = explode(" ", $t);
        $array = array_unique($array);
        // Do something with the array, or:
        print_r($array);
    }
    $sql->free();
    $mysqli_connection->close();
?>
Sign up to request clarification or add additional context in comments.

6 Comments

You are right, except for the first sentence. MySQL does not have an array data type, so the data is stored as a string. Maybe it was an PHP array imploded before insertion.
Yes, you are correct. I meant to imply the the data was inserted into the table as a string regardless of the user's desire to preserve it as an array.
See rdlowrey's comment above, and the error message in the question. The query isn't returning any rows. Looks like you wrote a solution to the next problem the OP will face after he fixes that! :)
@bfavaretto Ah, well you seem to be even more correct. Probably should have refreshed and re-read before posting. Regardless, hopefully OG will find this useful for when the time comes.
Many thanks,but I got the error message Parse error: syntax error, unexpected T_DOUBLE_ARROW in C:\wamp\www\CountLine_CSV\csv2mysql.php on line 51. How to fix it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.