user4747724
user4747724

Reputation:

Is this the proper way to get a string back from an sql query in PHP?

I'm querying a DB and want that query in string format, not object so what I've been doing is:

$StringHolder = "";
$sql = (some sql)
$result = mysqli_query($conn, $sql);

if($row = mysqli_fetch_row($result)){
    $StringHolder = $row;
    }

$StringHolder = implode($StringHolder);

Is there a better way to go about doing this? As you can probably tell I'm very new to this PHP.

So, one of my actual chunks of code is:

$connection = mysqli_connect($server,$username,$password,$databaseBuilding);
$tenantIdSql= "SELECT tenant_id FROM rooms WHERE room_num = '".$roomNum."'";
$tenantIdObj= mysqli_query($connection, $tenantIdSql);
$tenantID = "";
    if($row = mysqli_fetch_row($tenantIdObj){
        $tenantID = $row;
    }
$tenantID = implode($tenantID);

Upvotes: 1

Views: 36

Answers (2)

Krunal Patel
Krunal Patel

Reputation: 61

you just need to replace $tenantID = $row; with $tenantID = $row['tenant_id']; and if that is not working well then try $tenantID = $row[0];

Upvotes: 0

Niklesh Raut
Niklesh Raut

Reputation: 34914

Try something like this

$tenantID_array = array();
if($row = mysqli_fetch_row($tenantIdObj){
    $tenantID_array[] = $row['tenant_id'];
}

$tenantID_str = implode(",",$tenantID_array);
echo $tenantID_str;

Upvotes: 1

Related Questions