Heim >Backend-Entwicklung >PHP-Tutorial >php 在线 mysql 大数据导入程序,mysql数据导入_PHP教程
<span><br /> 1</span> <?<span>php </span><span> 2</span> <span>header</span>("content-type:text/html;charset=utf-8"<span>); </span><span> 3</span> <span>error_reporting</span>(<span>E_ALL</span><span>); </span><span> 4</span> <span>set_time_limit</span>(0<span>); </span><span> 5</span> <span>$file</span>='./test.sql'<span>; </span><span> 6</span> <span>$data</span>=<span>file</span>(<span>$file</span><span>); </span><span> 7</span> <span> 8</span> <span>echo</span> "<pre class="brush:php;toolbar:false">"<span>; </span><span> 9</span> <span>//</span><span>print_r($data);</span> <span>10</span> <span>$data_new</span>=<span>array</span><span>(); </span><span>11</span> <span>$tmp</span>=<span>array</span><span>(); </span><span>12</span> <span>foreach</span> (<span>$data</span> <span>as</span> <span>$line</span><span>) { </span><span>13</span> <span>14</span> <span>$line</span>=<span>trim</span>(<span>$line</span><span>); </span><span>15</span> <span>if</span>(<span>strlen</span>(<span>$line</span>)==0<span>){ </span><span>16</span> <span>continue</span><span>; </span><span>17</span> <span> } </span><span>18</span> <span>if</span>(<span>substr</span>(<span>$line</span>,0,2)=='--'<span>){ </span><span>19</span> <span>continue</span><span>; </span><span>20</span> <span> } </span><span>21</span> <span>if</span>(<span>substr</span>(<span>$line</span>,0,2)=='/*'<span>){ </span><span>22</span> <span>continue</span><span>; </span><span>23</span> <span> } </span><span>24</span> <span>$tmp</span>[]=<span>$line</span><span>; </span><span>25</span> <span>if</span>(<span>substr</span>(<span>$line</span>,-1)==';'<span>){ </span><span>26</span> <span>$query</span>=<span>implode</span>('',<span>$tmp</span><span>); </span><span>27</span> <span>$tmp</span>=<span>array</span><span>(); </span><span>28</span> <span>$data_new</span>[]=<span>$query</span><span>; </span><span>29</span> <span> } </span><span>30</span> <span>31</span> <span>} </span><span>32</span> <span>33</span> <span>34</span> <span>35</span> <span>$mysqli</span>=<span>new</span> mysqli('localhost','root','root','test'<span>); </span><span>36</span> <span>if</span>(<span>$mysqli</span>-><span>connect_errno){ </span><span>37</span> <span>exit</span>('数据库连接失败!'<span>); </span><span>38</span> <span>} </span><span>39</span> <span>$mysqli</span>->query("set names utf8"<span>); </span><span>40</span> <span>41</span> <span>$error</span>=<span>array</span><span>(); </span><span>42</span> <span>foreach</span>(<span>$data_new</span> <span>as</span> <span>$sql</span><span>){ </span><span>43</span> <span>$mysqli</span>->query(<span>$sql</span><span>); </span><span>44</span> <span>$r</span>=<span>$mysqli</span>-><span>error; </span><span>45</span> <span>if</span>(<span>$r</span>) <span>$error</span>[]=<span>$r</span><span>; </span><span>46</span> <span>} </span><span>47</span> <span>print_r</span>(<span>$r</span><span>); </span><span>48</span> <span>49</span> <span>50</span> <span>/*</span> <span>51</span> <span>52</span> <span>53</span> <span>mysql>ALTER TABLE tbl2 DISABLE KEYS; </span><span>54</span> <span>Query OK, 0 rows affected (0.00 sec) </span><span>55</span> <span>56</span> <span>mysql>INSERT INTO tbl2 SELECT * FROM tbl1; </span><span>57</span> <span>Query OK, 2000000 row affected (36.30 sec) </span><span>58</span> <span>Records: 2000000 Duplicates: 0 Warnings: 0 </span><span>59</span> <span>60</span> <span>mysql>ALTER TABLE tbl2 ENABLE KEYS; </span><span>61</span> <span>Query OK, 0 rows affected (44.55 sec) </span><span>62</span> <span>63</span> <span>64</span> <span>*/</span>
适用情况:phpmyadmin导出的sql文件过大,无法导入到线上phpmyadmin中
把该程序和sql文件上传到空间中,用完后删除即可。