Turli jadval nomlari bilan bir xil so'rovni bajarish

Ba'zi jadval nomlari va oldindan belgilangan SQL so'rovi bilan qator bor Ushbu qatordagi barcha jadvallarni bir xil so'rovni bajarishni xohlayman. Mening yondashuv jadvallar qatorini o'tish va mavjud jadval nomi bilan keyingi ( +1 ) almashtirish edi:

$tabs = array("abc", "def", "ghi", "jkl");//array of tables

$query = "SELECT `col1`, `col2` FROM `abc`";//example query

for($i=0; $i

Bu chiqish:

SELECT `col1`, `col2` FROM `abc`
SELECT `col1`, `col2` FROM `def`
SELECT `col1`, `col2` FROM `ghi`
SELECT `col1`, `col2` FROM `jkl`

Biroq, bu holatda, bu mavjud bo'lmagan indeksni (bu holda 4 ) mavjud emas. Shunday qilib, bu xato (yoki xabar) paydo bo'ladi:

Notice:  Undefined offset: 4 in [...][...] on line 11

Yaxshi yondashuv bormi?

0

6 javoblar

I'd do two things in your code: first i'd use count since sizeof is an alias for count and i'd assign it to a variable since as per PHP manual this will lead to a better performance. Then I'd use an if to prevent the error. this not to rewrite your code but keep using the for

$query = "SELECT `col1`, `col2` FROM `abc`";//example query
$max = count($tabs);
for($i=0; $i<$max; $i++){
   //... execute ...

    echo $query;//for debugging
    if($tabs[$i+1]<$max){
        $query = str_replace ( $tabs [$i], $tabs [$i+1], $query );//replacing table names
    }
}
1
qo'shib qo'ydi

Sizeof() o'rniga, count() dan foydalaning

Ehtimol, $ i + 1 ni str_replace ichida xatoga yo'l qo'yganligi bilan farq qildingiz. Men kodni yanada ko'proq yozgan bo'lardim:

$tabs = array('abc', 'def', 'ghi', 'jkl'); $query = 'SELECT col1, col2 FROM :table:'; foreach($tabs as $i => $tab) { $sql = str_replace(':table:', $tab, $query); echo $sql;//for debugging //... execute ... }

Bu esa, foreach va kerakli so'rovlar mag'lubiyatini ($ sql) saqlash uchun ikkinchi o'zgaruvchidan foydalanib, qator ko'rsatkichlari bilan har qanday muammolarni oldini oladi.

1
qo'shib qo'ydi
$tabs = array('abc', 'def', 'ghi', 'jkl');//array of tables

foreach($tabs as $tab) {
    $query = sprintf('SELECT `col1`, `col2` FROM `%s`', $tab);

   //do something with $query
}

Izoh:

  • Single quotes are faster than double quotes.
  • sprintf is faster than str_replace and more flexible because you can add extra parameters.
  • foreach iterates over every item in the array and you don't need to worry about the array index. If you wish to use the array index, just change foreach($array as $value) to foreach($array as $index => $value) and you can use the $index.
  • This method is the most efficient & easiest to read solution to the problem of those provided so far - and solves the answer in the fewest lines of code.
0
qo'shib qo'ydi
@yivi - Siz bilan rozi bo'lasiz, lekin kodni juda yaxshi hujjatlashtirilgani kabi ochiq-oydin bo'lishi kerak deb o'ylamagan edim. Biroq, men sizning fikringizni bilib oldim va bir necha o'qni qo'shdim.
qo'shib qo'ydi muallif Gravy, manba
Ushbu kod snippetasi savolni hal qilishi mumkin, ammo tushuntirish , albatta, takomillashtirishga yordam beradi Sizning postingizning sifati. Kelajakda o'quvchilarga bo'lgan savolingizga javob berishni unutmang va ular sizning kod taklifingizni sabablarini bilmasligi mumkin. Iltimos, kodingizni izohli izohlar bilan to'ldirishga urinmang, bu kodni va tushuntirishlarni o'qiydiganligini pasaytiradi!
qo'shib qo'ydi muallif yivi, manba

Andoza so'rovi bilan ishlash.

$tabs = array("abc", "def", "ghi", "jkl");//array of tables

$tmp_query = "SELECT `col1`, `col2` FROM `%table%`";//template query

echo $tmp_query ;//for debugging

foreach($tabs as $tab){

    $query = str_replace ('%table%', $tab, $tmp_query  );//replacing table names

    echo $query ;//for debugging

   //... execute ...
}
0
qo'shib qo'ydi
$ query = str_replace ('% table%', $ tab, $ tmp_query);//jadval nomlarini echo $ query; ;
qo'shib qo'ydi muallif splash58, manba
@ splash58 oooops
qo'shib qo'ydi muallif JustOnUnderMillions, manba

So'rovingizda markerni qo'yish va har bir jadval uchun quyidagi kabi o'zgartirish mumkin:

<?php

$tabs = array("abc", "def", "ghi", "jkl");//array of tables
$query = "SELECT `col1`, `col2` FROM `%my_table%` ";//example query

foreach($tabs as $tab) {
    $query = str_replace("%my_table%", $tab, $query);
    echo $query;
}
0
qo'shib qo'ydi

Buni ishga tushiring

$tabs = array("abc", "def", "ghi", "jkl");//array of tables

$basequery = "SELECT `col1`, `col2` FROM ";//example query
foreach ($tabs as $tab){
   $query = $basequery." `".$tab."`";
}
0
qo'shib qo'ydi
Yoki qilmang. Hech qanday "harakat" yo'q. A yaxshi javob har doim nima sodir qilinganligi va nima uchun u faqat OP uchun emas, balki kelajakda tashrif buyuruvchilar uchun shunday amalga oshirilganligi haqida tushuntirishlarga ega bo'ladi.
qo'shib qo'ydi muallif Jay Blanchard, manba
PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin