Jump to content

[PHP][MySQL] Wynik z mysqla do wielokrotnego użytku?


tlaguz

Recommended Posts

Witam.

 

Mam coś takiego:

$result = mysql_query('SELECT * FROM tabela;');

 

i wszystko fajnie mam wynik, tylko teraz co trzeba zrobić, żeby można go było wykorzystać pod np. mysql_fetch_array oraz mysql_fetch_field?

 

Próbowałem stworzyć drugi: $resulta = $result, ale jak zmienię $resulta równolegle zmienia się $result :(

 

Ma ktoś jakiś pomysł? Szukałem w "guglu" ale on tym razem milczy na ten temat.

 

Z góry dziękuje za odpowiedź i pozdrawiam

tlaguz.

Link to comment
Share on other sites

Możesz wynik zapisać do tablicy i później pobierać dane bezpośrednio z niej:

 

$array = array();
while ($row = mysql_fetch_array($result)) {
   $array[] = $row;
}

Za dwadzieścia lat bardziej będziesz żałował tego czego nie zrobiłeś, niż tego co zrobiłeś. Więc odwiąż liny, opuść bezpieczną przystań. Złap w żagle pomyślne wiatry. Podróżuj. Śnij. Odkrywaj." M". Twain.

Link to comment
Share on other sites

Może coś takiego Cię zadowoli?

$quer="SELECT * FROM menu_categories ORDER BY indeks ASC";
$result=mysql_query($quer);
$resulta=mysql_query($quer);
while($wierszm = mysql_fetch_array($result))
{...


while($wierszn = mysql_fetch_array($resulta))
{...

Link to comment
Share on other sites

Witam.

Dzięki za odpowiedź.

 

Jason, wszystko fajnie tylko w tym przypadku niepotrzebnie obciążam bazę. Zakładając iż baza ma 1000 takich zapytań to może zdechnąć.

 

Nie da się jakoś powielić zmiennej mając ją lokalnie?

 

Z góry dziękuje za odpowiedź i pozdrawiam

tlaguz.

Link to comment
Share on other sites

Wielkie dzięki wszystkim za odpowiedź.

 

Zrobiłem tak jak radził Force i rzeczywiście działa.

 

Temat do zamknięcia

Pozdrawiam

tlaguz.

 

Edit:

Wszystko działa, ale jeżeli mam funkcję i przekazuje $result jako parametr (w funkcji mysql_fetch_array) a po zakończeniu tejże funkcji wywołam mysql_data_seek to następna operacja na wyniku zwraca błąd :/ . Zauważyłem, że jeżeli wyrzucę wywołanie tej funkcji to wszystko działa więc mysql_data_seek wywołuję z dobrymi parametrami.

 

Ma ktoś pomysł co robię nie tak?

 

Z góry dziękuje za odpowiedź i pozdrawiam

tlaguz

 

Edit2:

Kod wygląda tak:

 

<?php
  function MySQL_Make_Array($result)
   {
         $table = null;
        // if(mysql_fetch_array($fresult) == null) {return null; Exit;}
         $i = 0;
         while($a = mysql_fetch_array($result))
          {
                $table[$i] = $a;
                $i++;
          }
         return $table;
       }

  function MySQL_Get_Columns($result)
   {
         $table = null;
        // if(mysql_fetch_filed($result) == null) {return null; Exit;}
         $i = 0;
         while($i < mysql_num_fields($result))
          {
                $a = mysql_fetch_field($result);
                $table[$i] = $a->name;
                $i++;
          }
         return $table;
       }

  function MySQL_Get_Columns_Attributes($result)
   {
         $table = null;
        // if(mysql_fetch_filed($result) == null) {return null; Exit;}
         $i = 0;
         while($i < mysql_num_fields($result))
          {
                $a = mysql_fetch_field($result);
                $table[$i]["NOT NULL"] = $a->not_null;
                $table[$i]["PRIMARY_KEY"] = $a->primary_key;
                $table[$i]["UNIQUE_KEY"] = $a->unique_key;
                $table[$i]["MULTIPLE_KEY"] = $a->multiple_key;
                $i++;
          }
         return $table;
       }

  function MySQL_Make_HTML_Table($result)
   {
         $array = MySQL_Make_Array($result);
mysql_data_seek($result, 0);
         $columns = MySQL_Get_Columns($result);
         //mysql_data_seek($result, 0);
         //$attributes = MySQL_Get_Columns_Attributes($result);
         //mysql_data_seek($result, 0);
         $return = '<table border=1 bordercolor=black cellspacing=0 cellpadding=0>
';
         /*for($i = 0; $i < count($array); $i++) //kolejne wiersze
          {
                $return = $return.'<tr>
';
                for($ii = 0; $ii <= count($columns); $ii++) //kolejne komórki
                 {
                   $return = $return.'<td>
';
           if($i == 0) //wtedy jest to nagłówek
            {
                          $return = $return.$columns[$ii].'<b>';
                           if($attributes[$ii]["NOT NULL"]) {$return = $return.'(NOT NULL)';}
                           if($attributes[$ii]["PRIMARY_KEY"]) {$return = $return.'(PRIMARY_KEY)';}
                           if($attributes[$ii]["UNIQUE_KEY"]) {$return = $return.'(UNIQUE_KEY)';}
                           if($attributes[$ii]["MULTIPLE_KEY"]) {$return = $return.'(MULTIPLE_KEY)';}
                          $return = $return.'</b>
';
                        }
                       else //a wtedy dane
                        {
                          $return = $return.$array[$i][$columns[$ii]];
                        }
           $return = $return.'</td>
';
                 }
                $return = $return.'</tr>
';
          }*/
         $return = $return.'
</table>';
     return $return;
       }

global $config_path;
 include $config_path.'mysql.cnf';
 $sql = mysql_connect($host, $user, $pass) or die ("Cannot connect to MySQL database: ".mysql_error());
 if(!$sql) {Exit;}
mysql_select_db($basename);

 $request = 'SELECT * FROM vg_users;';
 $reply = mysql_query($request, $sql) or die("Request failed: " + mysql_error());

 $a = MySQL_Make_HTML_Table($reply);  
?>

 

Nie jestem pewien, czy działa bo wyciągałem potrzebne funkcje (unit jest bardzo duży...) i mogłem coś niechcący obciąć. Ale generalnie błąd jest na początku funkcji MySQL_Make_HTML_Table.

Link to comment
Share on other sites

Z manuala:

Informacja: Funkcja mysql_data_seek() może być uzyta tylko w połączeniu z mysql_query(), a nie z mysql_unbuffered_query().

dlatego zamiast mysql_data_seek przy funkcji getColumns i getColumnsAttributes musisz użyć mysql_field_seek. Poza tym trochę skróciłem zapis funkcji, działanie jest identyczne jak poprzednio.

 

    function MySQL_Make_Array($result)
   {
         $table = array();
         while($a = mysql_fetch_array($result))
               $table[] = $a;
         return $table;
   }

  function MySQL_Get_Columns($result)
   {
         $table = array();
         while($a = mysql_fetch_field($result))
                   $table[] = $a->name;
         return $table;
       }

  function MySQL_Get_Columns_Attributes($result)
   {
         $table = array();
         while($a = mysql_fetch_field($result))
          {
                $arr["NOT NULL"] = $a->not_null;
                $arr["PRIMARY_KEY"] = $a->primary_key;
                $arr["UNIQUE_KEY"] = $a->unique_key;
                $arr["MULTIPLE_KEY"] = $a->multiple_key;
                $table[] = $arr;       
          }
         return $table;
       }

  function MySQL_Make_HTML_Table($result)
   {
         $array = MySQL_Make_Array($result);
print_r($array);

mysql_field_seek($result, 0);
         $columns = MySQL_Get_Columns($result);
print_r($columns);

mysql_field_seek($result, 0);
         $attributes = MySQL_Get_Columns_Attributes($result);
print_r($attributes);

...

Za dwadzieścia lat bardziej będziesz żałował tego czego nie zrobiłeś, niż tego co zrobiłeś. Więc odwiąż liny, opuść bezpieczną przystań. Złap w żagle pomyślne wiatry. Podróżuj. Śnij. Odkrywaj." M". Twain.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Guest
This topic is now closed to further replies.
×
×
  • Create New...