Jam1
Jam1

Reputation: 649

Convert sql result (object) to string variable for future input into sql table (I do not want to echo the result)

I have read most of the questions here and read the php manual in regards to the problem of converting an sql result to a string, however none of them is working for me. The examples given I understand, however they are echoing the sql results, I do not want the result to be echoed, I just want it to be stored in a variable so I can immediately insert it into a next sql table.

This is my code:

    $cnt_fips = mysqli_query($con, "SELECT cc_fips FROM location2 WHERE location_name = '$cnt'");
    $row = mysqli_fetch_assoc($cnt_fips);

These are the codes I have used to convert to string but failed with

    $myStr = !is_array($row) ? trim(addslashes($row)):'';

and

    $myStr = (string)$row;

and

    $myStr = print_r($row,true);

and also

    $myStr = (string)$row;

And insert into the table below

    $query2 = mysqli_query($con, "INSERT INTO location3 VALUES ('','$myStr')");

Upvotes: 2

Views: 3648

Answers (1)

AbraCadaver
AbraCadaver

Reputation: 78994

$row is always an array with column names as the keys, use:

$myStr = $row['cc_fips'];

Also, I'm pretty sure you can do that all in one insert with a sub-select (though maybe not if a row with $cnt doesn't exist). If so, maybe someone will post it.

Upvotes: 1

Related Questions