Jokeboy
Jokeboy

Reputation: 35

How to explode() value and save it in a variable?

Right now I'm working on a new homepage for a museum, and I have to use the old database (because it has over 70k entries).

My job now is to change the values in the "dias" table (supporter row) from full names of the supporters to their IDs (supporter table).

Right know I'm using this code:

$result = mysql_query("SELECT pate FROM dias");

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            $pate = explode(" ", print_r($row[0]));
    echo "<br>";
    }

If I now put in $pate[0], I only get "1" as output (along with the names from the print_r).

My question is: How can I split the names and save them seperately in a variable, because I think I need this to be able to compare the names with the one in the supporter table and to write the ids in the supporter row in the "dias" table.

If you need more infomration, feel free to ask.

Upvotes: 1

Views: 6117

Answers (5)

Asuquo12
Asuquo12

Reputation: 835

It looks like you are talking about List() function. list() will enable you explode into variables like;

$address ="Benson Bush|4545547 | living in California" ;

list($name ,$code , $description) = explode("|", $address);

echo $name."<br>";
echo $code."<br>";
echo $description."<br>";

Upvotes: 2

Milind
Milind

Reputation: 71

$result = mysql_query("SELECT pate FROM dias");
while ($row = mysql_fetch_assoc($result)) {
    $data[] = $row;
    print_r($data);
    echo "</br>";
}
print_r($data);

If you would like to stored value in variable then you can use extract function also.

Upvotes: 0

h2ooooooo
h2ooooooo

Reputation: 39522

Don't do this after you've received the data - do it from your query by using SUBSTRING_INDEX:

SELECT SUBSTRING_INDEX(pate, ' ', 1) FROM dias

Upvotes: 0

Ashique C M
Ashique C M

Reputation: 683

$result = mysql_query("SELECT pate FROM dias");

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            $pate = explode(" ", $row[0]);
print_r($pate);
    echo "<br>";
    }

Upvotes: 0

Evan
Evan

Reputation: 123

explode needs a string as the second argument, but you are taking the return value of print_r, which is true in this case. Just take $row[0] and you should have an array of your data in $pate.

Try printing $pate after that.

$result = mysql_query("SELECT pate FROM dias");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        $pate = explode(" ", $row[0]);
        print_r($pate)
        echo "<br>";
}

After that continue :).

Upvotes: 3

Related Questions