Skip to content
Advertisement

STR_REPLACE to add prefix to mysql query in php

This is my query

JavaScript

This is the function to add prefix to tables (This function is taken from MySqliDB class)

JavaScript

When I echo rawAddPrefix($q,"demo_") I get only prefix added to tbl_accounts table only. I need to add prefix to all tables.

What’s wrong here ?

Advertisement

Answer

That function above does only point and make changes to one occurrence.

I’d suggest a preg_replace_callback and str_replace:

JavaScript

If for some reason the table names are user input you’ll need to have a whitelist to make your query safe as well.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement