Erik180486
Erik180486

Reputation: 45

REPLACE query working in SQL but not in PHP

I made the following query:

mysqli_query($conn, "SELECT image_first, REPLACE(image_first,'/home/erik/','')   
     FROM reviews_media WHERE review_id = $id");

I've tested it in PhpMyAdmin and it's working. But when I echo it, it's still showing the /home/erik/ part.

What am I doing wrong?

Upvotes: 1

Views: 63

Answers (1)

u_mulder
u_mulder

Reputation: 54796

Obvioulsy you echo image_first value, but you need to echo result of REPLACE. You can modify query as:

mysqli_query($conn, "SELECT image_first, REPLACE(image_first,'/home/erik/','') as new_img FROM reviews_media WHERE review_id = $id");

See, I added an alias to result of REPLACE function. Now you can echo something like:

echo $row['new_img'];

As you don't do anything to result of REPLACE in a query, you can also simplify it and do a replace with php:

mysqli_query($conn, "SELECT image_first FROM reviews_media WHERE review_id = $id");
// fetching results
echo str_replace('/home/erik/', '', $row['image_first']);

Upvotes: 1

Related Questions