How do I add a prefix before my table in php?

Asked

Viewed 55 times

0

How do I add a prefixo before my tabela in php? Follow my code below:

$table = 'news AS N, news_category AS NC';

if (strpos($table, ', ') !== false) { 
    foreach (array($table) as $key => $value) {
        $db = $value;
    }
    echo $db;
} else { 
    echo 'FAIL'; 
}

You are returning me the following value: news AS N, news_category AS NC

Right now I’d like to leave it at that: prefixo_news as n, prefixo_news_category AS NC, within the code that I shared above. Remembering that I don’t want to so: $table = 'prefixo_news AS N, prefixo_news_category AS NC' in the variable $table, inside the if or foreach. It is possible?

  • This foreach loop of yours doesn’t make any sense. What do you want? It’s not quite clear what you want with this code, and probably the best solution is not the one you’re imagining.

  • Well, I’m a beginner, I need help, I wish for the prefixo, help me create good code?

  • That’s not it, you’re building a chunk of SQL, right? Under what conditions do you need the prefix and under which you don’t need it? And what is the variable for $db? It’s hard to help you without understanding what you’re trying to do.

  • All right, come on, I’m putting together a snippet of SQL because the conditions I’m gonna need it for are at function, because I’m wanting to mount a single Function with two types of DB, the selection of normal DB and the selection of DB mult, the normal I already have, is like this: https://pastebin.com/cJApMWgg now I want to put a DB mult inside this function, so I’m using the if with strpos to check if you have , when choosing a table

  • What is a mult DB? Anyway, the other code seems right, and even has prefix treatment in the table. But I still do not understand your doubt.

  • multDB = I will open two tables at once, example: TABLE: News and TABLE: News_category, then you will read like this: readDB('news, news_category'); but with the code I have, you will only enter the prefix in the 1st table, being: readDB('prefixo_news, news_category'); this is where my problem comes in, I need the prefix to enter in the 2nd table also getting: readDB('prefixo_news, prefixo_news_category'); and so on, if you have 3 tables, 4, etc ... all entering the prefix in front.

  • 1

    For all you said, I would create a new function to handle queries that involve more than one table.

Show 2 more comments

2 answers

0

You can use substr([string], [começa_na_posição], [vai_ate]), for example:

if (substr($table, 0, 2) === ', ') {

0


Splits an array into parts and traverses the elements by adding the prefix:

$table = 'news AS N, news_category AS NC';

$prefixed = "";

if (strpos($table, ', ') !== false) {
    //O explode divide a string em um array pelo primeiro parâmetro
    foreach (explode(", ", $table) as $value) {
        //Adiciona a $prefixed o valor com o prefixo
        $prefixed .= ", prefix_$value";
    }
    //Remove o primeiro prefixo
    $prefixed = trim($prefixed, ", prefix_");
    echo $prefixed;
} else { 
    echo 'FAIL'; 
}

Or else:

$prefixed = implode(", prefix_", explode(", ", $table));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.