1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645
|
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2010 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Zeev Suraski <zeev@zend.com> |
| Zak Greant <zak@mysql.com> |
| Georg Richter <georg@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id: php_mysql.c 300792 2010-06-28 01:19:08Z felipe $ */
/* TODO:
*
* ? Safe mode implementation
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "php_globals.h"
#include "ext/standard/info.h"
#include "ext/standard/php_string.h"
#include "ext/standard/basic_functions.h"
#ifdef ZEND_ENGINE_2
# include "zend_exceptions.h"
#else
/* PHP 4 compat */
# define OnUpdateLong OnUpdateInt
# define E_STRICT E_NOTICE
#endif
#if HAVE_MYSQL
#ifdef PHP_WIN32
# include <winsock2.h>
# define signal(a, b) NULL
#elif defined(NETWARE)
# include <sys/socket.h>
# define signal(a, b) NULL
#else
# if HAVE_SIGNAL_H
# include <signal.h>
# endif
# if HAVE_SYS_TYPES_H
# include <sys/types.h>
# endif
# include <netdb.h>
# include <netinet/in.h>
# if HAVE_ARPA_INET_H
# include <arpa/inet.h>
# endif
#endif
#include "php_ini.h"
#include "php_mysql_structs.h"
/* True globals, no need for thread safety */
static int le_result, le_link, le_plink;
#ifdef HAVE_MYSQL_REAL_CONNECT
# ifdef HAVE_ERRMSG_H
# include <errmsg.h>
# endif
#endif
#define SAFE_STRING(s) ((s)?(s):"")
#if MYSQL_VERSION_ID > 32199 || defined(MYSQL_USE_MYSQLND)
# define mysql_row_length_type unsigned long
# define HAVE_MYSQL_ERRNO
#else
# define mysql_row_length_type unsigned int
# ifdef mysql_errno
# define HAVE_MYSQL_ERRNO
# endif
#endif
#if MYSQL_VERSION_ID >= 32032 || defined(MYSQL_USE_MYSQLND)
#define HAVE_GETINFO_FUNCS
#endif
#if MYSQL_VERSION_ID > 32133 || defined(FIELD_TYPE_TINY)
#define MYSQL_HAS_TINY
#endif
#if MYSQL_VERSION_ID >= 32200
#define MYSQL_HAS_YEAR
#endif
#define MYSQL_ASSOC 1<<0
#define MYSQL_NUM 1<<1
#define MYSQL_BOTH (MYSQL_ASSOC|MYSQL_NUM)
#define MYSQL_USE_RESULT 0
#define MYSQL_STORE_RESULT 1
#if MYSQL_VERSION_ID < 32224
#define PHP_MYSQL_VALID_RESULT(mysql) \
(mysql_num_fields(mysql)>0)
#else
#define PHP_MYSQL_VALID_RESULT(mysql) \
(mysql_field_count(mysql)>0)
#endif
ZEND_DECLARE_MODULE_GLOBALS(mysql)
static PHP_GINIT_FUNCTION(mysql);
typedef struct _php_mysql_conn {
MYSQL *conn;
int active_result_id;
int multi_query;
} php_mysql_conn;
#if MYSQL_VERSION_ID >= 40101
#define MYSQL_DISABLE_MQ if (mysql->multi_query) { \
mysql_set_server_option(mysql->conn, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
mysql->multi_query = 0; \
}
#else
#define MYSQL_DISABLE_MQ
#endif
/* {{{ arginfo */
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_connect, 0, 0, 0)
ZEND_ARG_INFO(0, hostname)
ZEND_ARG_INFO(0, username)
ZEND_ARG_INFO(0, password)
ZEND_ARG_INFO(0, new)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_pconnect, 0, 0, 0)
ZEND_ARG_INFO(0, hostname)
ZEND_ARG_INFO(0, username)
ZEND_ARG_INFO(0, password)
ZEND_ARG_INFO(0, flags)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo__optional_mysql_link, 0, 0, 0)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_select_db, 0, 0, 1)
ZEND_ARG_INFO(0, database_name)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo__void_mysql_arg, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_set_charset, 0, 0, 1)
ZEND_ARG_INFO(0, charset_name)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_query, 0, 0, 1)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_db_query, 0, 0, 2)
ZEND_ARG_INFO(0, database_name)
ZEND_ARG_INFO(0, query)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_list_fields, 0, 0, 2)
ZEND_ARG_INFO(0, database_name)
ZEND_ARG_INFO(0, table_name)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_escape_string, 0, 0, 1)
ZEND_ARG_INFO(0, string)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_real_escape_string, 0, 0, 1)
ZEND_ARG_INFO(0, string)
ZEND_ARG_INFO(0, link_identifier)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_result, 0, 0, 2)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, row)
ZEND_ARG_INFO(0, field)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo__result_mysql_arg, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_fetch_object, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, class_name)
ZEND_ARG_INFO(0, ctor_params)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_fetch_array, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, result_type)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_data_seek, 0, 0, 2)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, row_number)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_fetch_field, 0, 0, 1)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, field_offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_field_seek, 0, 0, 2)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, field_offset)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_mysql_field_name, 0, 0, 2)
ZEND_ARG_INFO(0, result)
ZEND_ARG_INFO(0, field_index)
ZEND_END_ARG_INFO()
/* }}} */
/* {{{ mysql_functions[]
*/
static const zend_function_entry mysql_functions[] = {
PHP_FE(mysql_connect, arginfo_mysql_connect)
PHP_FE(mysql_pconnect, arginfo_mysql_pconnect)
PHP_FE(mysql_close, arginfo__optional_mysql_link)
PHP_FE(mysql_select_db, arginfo_mysql_select_db)
#ifndef NETWARE /* The below two functions not supported on NetWare */
#if MYSQL_VERSION_ID < 40000
PHP_DEP_FE(mysql_create_db, arginfo_mysql_select_db)
PHP_DEP_FE(mysql_drop_db, arginfo_mysql_select_db)
#endif
#endif /* NETWARE */
PHP_FE(mysql_query, arginfo_mysql_query)
PHP_FE(mysql_unbuffered_query, arginfo_mysql_query)
PHP_FE(mysql_db_query, arginfo_mysql_db_query)
PHP_FE(mysql_list_dbs, arginfo__optional_mysql_link)
PHP_DEP_FE(mysql_list_tables, arginfo_mysql_select_db)
PHP_FE(mysql_list_fields, arginfo_mysql_list_fields)
PHP_FE(mysql_list_processes, arginfo__optional_mysql_link)
PHP_FE(mysql_error, arginfo__optional_mysql_link)
#ifdef HAVE_MYSQL_ERRNO
PHP_FE(mysql_errno, arginfo__optional_mysql_link)
#endif
PHP_FE(mysql_affected_rows, arginfo__optional_mysql_link)
PHP_FE(mysql_insert_id, arginfo__optional_mysql_link)
PHP_FE(mysql_result, arginfo_mysql_result)
PHP_FE(mysql_num_rows, arginfo__result_mysql_arg)
PHP_FE(mysql_num_fields, arginfo__result_mysql_arg)
PHP_FE(mysql_fetch_row, arginfo__result_mysql_arg)
PHP_FE(mysql_fetch_array, arginfo_mysql_fetch_array)
PHP_FE(mysql_fetch_assoc, arginfo__result_mysql_arg)
PHP_FE(mysql_fetch_object, arginfo_mysql_fetch_object)
PHP_FE(mysql_data_seek, arginfo_mysql_data_seek)
PHP_FE(mysql_fetch_lengths, arginfo__result_mysql_arg)
PHP_FE(mysql_fetch_field, arginfo_mysql_fetch_field)
PHP_FE(mysql_field_seek, arginfo_mysql_field_seek)
PHP_FE(mysql_free_result, arginfo__result_mysql_arg)
PHP_FE(mysql_field_name, arginfo_mysql_field_name)
PHP_FE(mysql_field_table, arginfo_mysql_field_seek)
PHP_FE(mysql_field_len, arginfo_mysql_field_seek)
PHP_FE(mysql_field_type, arginfo_mysql_field_seek)
PHP_FE(mysql_field_flags, arginfo_mysql_field_seek)
PHP_FE(mysql_escape_string, arginfo_mysql_escape_string)
PHP_FE(mysql_real_escape_string, arginfo_mysql_real_escape_string)
PHP_FE(mysql_stat, arginfo__optional_mysql_link)
PHP_FE(mysql_thread_id, arginfo__optional_mysql_link)
PHP_FE(mysql_client_encoding, arginfo__optional_mysql_link)
PHP_FE(mysql_ping, arginfo__optional_mysql_link)
#ifdef HAVE_GETINFO_FUNCS
PHP_FE(mysql_get_client_info, arginfo__void_mysql_arg)
PHP_FE(mysql_get_host_info, arginfo__optional_mysql_link)
PHP_FE(mysql_get_proto_info, arginfo__optional_mysql_link)
PHP_FE(mysql_get_server_info, arginfo__optional_mysql_link)
#endif
PHP_FE(mysql_info, arginfo__optional_mysql_link)
#ifdef MYSQL_HAS_SET_CHARSET
PHP_FE(mysql_set_charset, arginfo_mysql_set_charset)
#endif
/* for downwards compatability */
PHP_FALIAS(mysql, mysql_db_query, arginfo_mysql_db_query)
PHP_FALIAS(mysql_fieldname, mysql_field_name, arginfo_mysql_field_name)
PHP_FALIAS(mysql_fieldtable, mysql_field_table, arginfo_mysql_field_seek)
PHP_FALIAS(mysql_fieldlen, mysql_field_len, arginfo_mysql_field_seek)
PHP_FALIAS(mysql_fieldtype, mysql_field_type, arginfo_mysql_field_seek)
PHP_FALIAS(mysql_fieldflags, mysql_field_flags, arginfo_mysql_field_seek)
PHP_FALIAS(mysql_selectdb, mysql_select_db, arginfo_mysql_select_db)
#ifndef NETWARE /* The below two functions not supported on NetWare */
#if MYSQL_VERSION_ID < 40000
PHP_DEP_FALIAS(mysql_createdb, mysql_create_db, arginfo_mysql_select_db)
PHP_DEP_FALIAS(mysql_dropdb, mysql_drop_db, arginfo_mysql_select_db)
#endif
#endif /* NETWARE */
PHP_FALIAS(mysql_freeresult, mysql_free_result, arginfo__result_mysql_arg)
PHP_FALIAS(mysql_numfields, mysql_num_fields, arginfo__result_mysql_arg)
PHP_FALIAS(mysql_numrows, mysql_num_rows, arginfo__result_mysql_arg)
PHP_FALIAS(mysql_listdbs, mysql_list_dbs, arginfo__optional_mysql_link)
PHP_DEP_FALIAS(mysql_listtables,mysql_list_tables, arginfo_mysql_select_db)
PHP_FALIAS(mysql_listfields, mysql_list_fields, arginfo_mysql_list_fields)
PHP_FALIAS(mysql_db_name, mysql_result, arginfo_mysql_result)
PHP_FALIAS(mysql_dbname, mysql_result, arginfo_mysql_result)
PHP_FALIAS(mysql_tablename, mysql_result, arginfo_mysql_result)
PHP_FALIAS(mysql_table_name, mysql_result, arginfo_mysql_result)
{NULL, NULL, NULL}
};
/* }}} */
/* Dependancies */
static const zend_module_dep mysql_deps[] = {
#if defined(MYSQL_USE_MYSQLND)
ZEND_MOD_REQUIRED("mysqlnd")
#endif
{NULL, NULL, NULL}
};
/* {{{ mysql_module_entry
*/
zend_module_entry mysql_module_entry = {
#if ZEND_MODULE_API_NO >= 20050922
STANDARD_MODULE_HEADER_EX, NULL,
mysql_deps,
#elif ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"mysql",
mysql_functions,
ZEND_MODULE_STARTUP_N(mysql),
PHP_MSHUTDOWN(mysql),
PHP_RINIT(mysql),
PHP_RSHUTDOWN(mysql),
PHP_MINFO(mysql),
"1.0",
PHP_MODULE_GLOBALS(mysql),
PHP_GINIT(mysql),
NULL,
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
#ifdef COMPILE_DL_MYSQL
ZEND_GET_MODULE(mysql)
#endif
void timeout(int sig);
#define CHECK_LINK(link) { if (link==-1) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "A link to the server could not be established"); RETURN_FALSE; } }
#if defined(MYSQL_USE_MYSQLND)
#define PHPMY_UNBUFFERED_QUERY_CHECK() \
{\
if (mysql->active_result_id) { \
do { \
int type; \
MYSQL_RES *mysql_result; \
\
mysql_result = (MYSQL_RES *) zend_list_find(mysql->active_result_id, &type); \
if (mysql_result && type==le_result) { \
if (mysql_result_is_unbuffered(mysql_result) && !mysql_eof(mysql_result)) { \
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Function called without first fetching all rows from a previous unbuffered query"); \
} \
zend_list_delete(mysql->active_result_id); \
mysql->active_result_id = 0; \
} \
} while(0); \
}\
}
#else
#define PHPMY_UNBUFFERED_QUERY_CHECK() \
{ \
if (mysql->active_result_id) { \
do { \
int type; \
MYSQL_RES *mysql_result; \
\
mysql_result = (MYSQL_RES *) zend_list_find(mysql->active_result_id, &type); \
if (mysql_result && type==le_result) { \
if (!mysql_eof(mysql_result)) { \
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Function called without first fetching all rows from a previous unbuffered query"); \
while (mysql_fetch_row(mysql_result)); \
} \
zend_list_delete(mysql->active_result_id); \
mysql->active_result_id = 0; \
} \
} while(0); \
} \
}
#endif
/* {{{ _free_mysql_result
* This wrapper is required since mysql_free_result() returns an integer, and
* thus, cannot be used directly
*/
static void _free_mysql_result(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
MYSQL_RES *mysql_result = (MYSQL_RES *)rsrc->ptr;
mysql_free_result(mysql_result);
MySG(result_allocated)--;
}
/* }}} */
/* {{{ php_mysql_set_default_link
*/
static void php_mysql_set_default_link(int id TSRMLS_DC)
{
if (MySG(default_link) != -1) {
zend_list_delete(MySG(default_link));
}
MySG(default_link) = id;
zend_list_addref(id);
}
/* }}} */
/* {{{ php_mysql_select_db
*/
static int php_mysql_select_db(php_mysql_conn *mysql, char *db TSRMLS_DC)
{
PHPMY_UNBUFFERED_QUERY_CHECK();
if (mysql_select_db(mysql->conn, db) != 0) {
return 0;
} else {
return 1;
}
}
/* }}} */
/* {{{ _close_mysql_link
*/
static void _close_mysql_link(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
void (*handler) (int);
handler = signal(SIGPIPE, SIG_IGN);
mysql_close(link->conn);
signal(SIGPIPE, handler);
efree(link);
MySG(num_links)--;
}
/* }}} */
/* {{{ _close_mysql_plink
*/
static void _close_mysql_plink(zend_rsrc_list_entry *rsrc TSRMLS_DC)
{
php_mysql_conn *link = (php_mysql_conn *)rsrc->ptr;
void (*handler) (int);
handler = signal(SIGPIPE, SIG_IGN);
mysql_close(link->conn);
signal(SIGPIPE, handler);
free(link);
MySG(num_persistent)--;
MySG(num_links)--;
}
/* }}} */
/* {{{ PHP_INI_MH
*/
static PHP_INI_MH(OnMySQLPort)
{
if (new_value != NULL) { /* default port */
MySG(default_port) = atoi(new_value);
} else {
MySG(default_port) = -1;
}
return SUCCESS;
}
/* }}} */
/* {{{ PHP_INI */
PHP_INI_BEGIN()
STD_PHP_INI_BOOLEAN("mysql.allow_persistent", "1", PHP_INI_SYSTEM, OnUpdateLong, allow_persistent, zend_mysql_globals, mysql_globals)
STD_PHP_INI_ENTRY_EX("mysql.max_persistent", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_persistent, zend_mysql_globals, mysql_globals, display_link_numbers)
STD_PHP_INI_ENTRY_EX("mysql.max_links", "-1", PHP_INI_SYSTEM, OnUpdateLong, max_links, zend_mysql_globals, mysql_globals, display_link_numbers)
STD_PHP_INI_ENTRY("mysql.default_host", NULL, PHP_INI_ALL, OnUpdateString, default_host, zend_mysql_globals, mysql_globals)
STD_PHP_INI_ENTRY("mysql.default_user", NULL, PHP_INI_ALL, OnUpdateString, default_user, zend_mysql_globals, mysql_globals)
STD_PHP_INI_ENTRY("mysql.default_password", NULL, PHP_INI_ALL, OnUpdateString, default_password, zend_mysql_globals, mysql_globals)
PHP_INI_ENTRY("mysql.default_port", NULL, PHP_INI_ALL, OnMySQLPort)
#ifdef MYSQL_UNIX_ADDR
STD_PHP_INI_ENTRY("mysql.default_socket", MYSQL_UNIX_ADDR,PHP_INI_ALL,OnUpdateStringUnempty, default_socket, zend_mysql_globals, mysql_globals)
#else
STD_PHP_INI_ENTRY("mysql.default_socket", NULL, PHP_INI_ALL, OnUpdateStringUnempty, default_socket, zend_mysql_globals, mysql_globals)
#endif
STD_PHP_INI_ENTRY("mysql.connect_timeout", "60", PHP_INI_ALL, OnUpdateLong, connect_timeout, zend_mysql_globals, mysql_globals)
STD_PHP_INI_BOOLEAN("mysql.trace_mode", "0", PHP_INI_ALL, OnUpdateLong, trace_mode, zend_mysql_globals, mysql_globals)
STD_PHP_INI_BOOLEAN("mysql.allow_local_infile", "1", PHP_INI_SYSTEM, OnUpdateLong, allow_local_infile, zend_mysql_globals, mysql_globals)
PHP_INI_END()
/* }}} */
/* {{{ PHP_GINIT_FUNCTION
*/
static PHP_GINIT_FUNCTION(mysql)
{
mysql_globals->num_persistent = 0;
mysql_globals->default_socket = NULL;
mysql_globals->default_host = NULL;
mysql_globals->default_user = NULL;
mysql_globals->default_password = NULL;
mysql_globals->connect_errno = 0;
mysql_globals->connect_error = NULL;
mysql_globals->connect_timeout = 0;
mysql_globals->trace_mode = 0;
mysql_globals->allow_local_infile = 1;
mysql_globals->result_allocated = 0;
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
ZEND_MODULE_STARTUP_D(mysql)
{
REGISTER_INI_ENTRIES();
le_result = zend_register_list_destructors_ex(_free_mysql_result, NULL, "mysql result", module_number);
le_link = zend_register_list_destructors_ex(_close_mysql_link, NULL, "mysql link", module_number);
le_plink = zend_register_list_destructors_ex(NULL, _close_mysql_plink, "mysql link persistent", module_number);
Z_TYPE(mysql_module_entry) = type;
REGISTER_LONG_CONSTANT("MYSQL_ASSOC", MYSQL_ASSOC, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_NUM", MYSQL_NUM, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_BOTH", MYSQL_BOTH, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_CLIENT_COMPRESS", CLIENT_COMPRESS, CONST_CS | CONST_PERSISTENT);
#if MYSQL_VERSION_ID >= 40000
REGISTER_LONG_CONSTANT("MYSQL_CLIENT_SSL", CLIENT_SSL, CONST_CS | CONST_PERSISTENT);
#endif
REGISTER_LONG_CONSTANT("MYSQL_CLIENT_INTERACTIVE", CLIENT_INTERACTIVE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MYSQL_CLIENT_IGNORE_SPACE", CLIENT_IGNORE_SPACE, CONST_CS | CONST_PERSISTENT);
#ifndef MYSQL_USE_MYSQLND
#if MYSQL_VERSION_ID >= 40000
if (mysql_server_init(0, NULL, NULL)) {
return FAILURE;
}
#endif
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(mysql)
{
#ifndef MYSQL_USE_MYSQLND
#if MYSQL_VERSION_ID >= 40000
#ifdef PHP_WIN32
unsigned long client_ver = mysql_get_client_version();
/*
Can't call mysql_server_end() multiple times prior to 5.0.46 on Windows.
PHP bug#41350 MySQL bug#25621
*/
if ((client_ver >= 50046 && client_ver < 50100) || client_ver > 50122) {
mysql_server_end();
}
#else
mysql_server_end();
#endif
#endif
#endif
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(mysql)
{
#if !defined(MYSQL_USE_MYSQLND) && defined(ZTS) && MYSQL_VERSION_ID >= 40000
if (mysql_thread_init()) {
return FAILURE;
}
#endif
MySG(default_link)=-1;
MySG(num_links) = MySG(num_persistent);
/* Reset connect error/errno on every request */
MySG(connect_error) = NULL;
MySG(connect_errno) =0;
MySG(result_allocated) = 0;
return SUCCESS;
}
/* }}} */
#ifdef MYSQL_USE_MYSQLND
static int php_mysql_persistent_helper(zend_rsrc_list_entry *le TSRMLS_DC)
{
if (le->type == le_plink) {
mysqlnd_end_psession(((php_mysql_conn *) le->ptr)->conn);
}
return ZEND_HASH_APPLY_KEEP;
} /* }}} */
#endif
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(mysql)
{
#if !defined(MYSQL_USE_MYSQLND) && defined(ZTS) && MYSQL_VERSION_ID >= 40000
mysql_thread_end();
#endif
if (MySG(trace_mode)) {
if (MySG(result_allocated)){
php_error_docref("function.mysql-free-result" TSRMLS_CC, E_WARNING, "%lu result set(s) not freed. Use mysql_free_result to free result sets which were requested using mysql_query()", MySG(result_allocated));
}
}
if (MySG(connect_error)!=NULL) {
efree(MySG(connect_error));
}
#ifdef MYSQL_USE_MYSQLND
zend_hash_apply(&EG(persistent_list), (apply_func_t) php_mysql_persistent_helper TSRMLS_CC);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(mysql)
{
char buf[32];
php_info_print_table_start();
php_info_print_table_header(2, "MySQL Support", "enabled");
snprintf(buf, sizeof(buf), "%ld", MySG(num_persistent));
php_info_print_table_row(2, "Active Persistent Links", buf);
snprintf(buf, sizeof(buf), "%ld", MySG(num_links));
php_info_print_table_row(2, "Active Links", buf);
php_info_print_table_row(2, "Client API version", mysql_get_client_info());
#if !defined (PHP_WIN32) && !defined (NETWARE) && !defined(MYSQL_USE_MYSQLND)
php_info_print_table_row(2, "MYSQL_MODULE_TYPE", PHP_MYSQL_TYPE);
php_info_print_table_row(2, "MYSQL_SOCKET", MYSQL_UNIX_ADDR);
php_info_print_table_row(2, "MYSQL_INCLUDE", PHP_MYSQL_INCLUDE);
php_info_print_table_row(2, "MYSQL_LIBS", PHP_MYSQL_LIBS);
#endif
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ php_mysql_do_connect
*/
#define MYSQL_DO_CONNECT_CLEANUP() \
if (free_host) { \
efree(host); \
}
#define MYSQL_DO_CONNECT_RETURN_FALSE() \
MYSQL_DO_CONNECT_CLEANUP(); \
RETURN_FALSE;
#ifdef MYSQL_USE_MYSQLND
#define MYSQL_PORT 0
#endif
static void php_mysql_do_connect(INTERNAL_FUNCTION_PARAMETERS, int persistent)
{
char *user=NULL, *passwd=NULL, *host_and_port=NULL, *socket=NULL, *tmp=NULL, *host=NULL;
int user_len, passwd_len, host_len;
char *hashed_details=NULL;
int hashed_details_length, port = MYSQL_PORT;
long client_flags = 0;
php_mysql_conn *mysql=NULL;
#if MYSQL_VERSION_ID <= 32230
void (*handler) (int);
#endif
zend_bool free_host=0, new_link=0;
long connect_timeout;
#if !defined(MYSQL_USE_MYSQLND)
if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING,
"Headers and client library minor version mismatch. Headers:%d Library:%ld",
MYSQL_VERSION_ID, mysql_get_client_version());
}
#endif
connect_timeout = MySG(connect_timeout);
socket = MySG(default_socket);
if (MySG(default_port) < 0) {
#if !defined(PHP_WIN32) && !defined(NETWARE)
struct servent *serv_ptr;
char *env;
MySG(default_port) = MYSQL_PORT;
if ((serv_ptr = getservbyname("mysql", "tcp"))) {
MySG(default_port) = (uint) ntohs((ushort) serv_ptr->s_port);
}
if ((env = getenv("MYSQL_TCP_PORT"))) {
MySG(default_port) = (uint) atoi(env);
}
#else
MySG(default_port) = MYSQL_PORT;
#endif
}
if (PG(sql_safe_mode)) {
if (ZEND_NUM_ARGS()>0) {
php_error_docref(NULL TSRMLS_CC, E_NOTICE, "SQL safe mode in effect - ignoring host/user/password information");
}
host_and_port=passwd=NULL;
user=php_get_current_user();
hashed_details_length = spprintf(&hashed_details, 0, "mysql__%s_", user);
client_flags = CLIENT_INTERACTIVE;
} else {
/* mysql_pconnect does not support new_link parameter */
if (persistent) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!l", &host_and_port, &host_len,
&user, &user_len, &passwd, &passwd_len,
&client_flags)==FAILURE) {
return;
}
} else {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!s!bl", &host_and_port, &host_len,
&user, &user_len, &passwd, &passwd_len,
&new_link, &client_flags)==FAILURE) {
return;
}
}
if (!host_and_port) {
host_and_port = MySG(default_host);
}
if (!user) {
user = MySG(default_user);
}
if (!passwd) {
passwd = MySG(default_password);
}
/* disable local infile option for open_basedir */
#if PHP_API_VERSION < 20100412
if (((PG(open_basedir) && PG(open_basedir)[0] != '\0') || PG(safe_mode)) && (client_flags & CLIENT_LOCAL_FILES)) {
#else
if ((PG(open_basedir) && PG(open_basedir)[0] != '\0') && (client_flags & CLIENT_LOCAL_FILES)) {
#endif
client_flags ^= CLIENT_LOCAL_FILES;
}
#ifdef CLIENT_MULTI_RESULTS
client_flags |= CLIENT_MULTI_RESULTS; /* compatibility with 5.2, see bug#50416 */
#endif
#ifdef CLIENT_MULTI_STATEMENTS
client_flags &= ~CLIENT_MULTI_STATEMENTS; /* don't allow multi_queries via connect parameter */
#endif
hashed_details_length = spprintf(&hashed_details, 0, "mysql_%s_%s_%s_%ld", SAFE_STRING(host_and_port), SAFE_STRING(user), SAFE_STRING(passwd), client_flags);
}
/* We cannot use mysql_port anymore in windows, need to use
* mysql_real_connect() to set the port.
*/
if (host_and_port && (tmp=strchr(host_and_port, ':'))) {
host = estrndup(host_and_port, tmp-host_and_port);
free_host = 1;
tmp++;
if (tmp[0] != '/') {
port = atoi(tmp);
if ((tmp=strchr(tmp, ':'))) {
tmp++;
socket=tmp;
}
} else {
socket = tmp;
}
} else {
host = host_and_port;
port = MySG(default_port);
}
#if MYSQL_VERSION_ID < 32200
mysql_port = port;
#endif
if (!MySG(allow_persistent)) {
persistent=0;
}
if (persistent) {
zend_rsrc_list_entry *le;
/* try to find if we already have this link in our persistent list */
if (zend_hash_find(&EG(persistent_list), hashed_details, hashed_details_length+1, (void **) &le)==FAILURE) { /* we don't */
zend_rsrc_list_entry new_le;
if (MySG(max_links) != -1 && MySG(num_links) >= MySG(max_links)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", MySG(num_links));
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
if (MySG(max_persistent) != -1 && MySG(num_persistent) >= MySG(max_persistent)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open persistent links (%ld)", MySG(num_persistent));
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
/* create the link */
mysql = (php_mysql_conn *) malloc(sizeof(php_mysql_conn));
mysql->active_result_id = 0;
#ifdef CLIENT_MULTI_STATEMENTS
mysql->multi_query = client_flags & CLIENT_MULTI_STATEMENTS? 1:0;
#else
mysql->multi_query = 0;
#endif
#ifndef MYSQL_USE_MYSQLND
mysql->conn = mysql_init(NULL);
#else
mysql->conn = mysql_init(persistent);
#endif
if (connect_timeout != -1) {
mysql_options(mysql->conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout);
}
#ifndef MYSQL_USE_MYSQLND
if (mysql_real_connect(mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL)
#else
if (mysqlnd_connect(mysql->conn, host, user, passwd, 0, NULL, 0, port, socket, client_flags TSRMLS_CC) == NULL)
#endif
{
/* Populate connect error globals so that the error functions can read them */
if (MySG(connect_error) != NULL) {
efree(MySG(connect_error));
}
MySG(connect_error) = estrdup(mysql_error(mysql->conn));
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", MySG(connect_error));
#if defined(HAVE_MYSQL_ERRNO)
MySG(connect_errno) = mysql_errno(mysql->conn);
#endif
free(mysql);
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
mysql_options(mysql->conn, MYSQL_OPT_LOCAL_INFILE, (char *)&MySG(allow_local_infile));
/* hash it up */
Z_TYPE(new_le) = le_plink;
new_le.ptr = mysql;
if (zend_hash_update(&EG(persistent_list), hashed_details, hashed_details_length+1, (void *) &new_le, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
free(mysql);
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
MySG(num_persistent)++;
MySG(num_links)++;
} else { /* The link is in our list of persistent connections */
if (Z_TYPE_P(le) != le_plink) {
MYSQL_DO_CONNECT_RETURN_FALSE();
}
mysql = (php_mysql_conn *) le->ptr;
mysql->active_result_id = 0;
#ifdef CLIENT_MULTI_STATEMENTS
mysql->multi_query = client_flags & CLIENT_MULTI_STATEMENTS? 1:0;
#else
mysql->multi_query = 0;
#endif
/* ensure that the link did not die */
#if defined(MYSQL_USE_MYSQLND)
mysqlnd_end_psession(mysql->conn);
#endif
if (mysql_ping(mysql->conn)) {
if (mysql_errno(mysql->conn) == 2006) {
#ifndef MYSQL_USE_MYSQLND
if (mysql_real_connect(mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL)
#else
if (mysqlnd_connect(mysql->conn, host, user, passwd, 0, NULL, 0, port, socket, client_flags TSRMLS_CC) == NULL)
#endif
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Link to server lost, unable to reconnect");
zend_hash_del(&EG(persistent_list), hashed_details, hashed_details_length+1);
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
mysql_options(mysql->conn, MYSQL_OPT_LOCAL_INFILE, (char *)&MySG(allow_local_infile));
}
} else {
#ifdef MYSQL_USE_MYSQLND
mysqlnd_restart_psession(mysql->conn);
#endif
}
}
ZEND_REGISTER_RESOURCE(return_value, mysql, le_plink);
} else { /* non persistent */
zend_rsrc_list_entry *index_ptr, new_index_ptr;
/* first we check the hash for the hashed_details key. if it exists,
* it should point us to the right offset where the actual mysql link sits.
* if it doesn't, open a new mysql link, add it to the resource list,
* and add a pointer to it with hashed_details as the key.
*/
if (!new_link && zend_hash_find(&EG(regular_list), hashed_details, hashed_details_length+1,(void **) &index_ptr)==SUCCESS) {
int type;
long link;
void *ptr;
if (Z_TYPE_P(index_ptr) != le_index_ptr) {
MYSQL_DO_CONNECT_RETURN_FALSE();
}
link = (long) index_ptr->ptr;
ptr = zend_list_find(link,&type); /* check if the link is still there */
if (ptr && (type==le_link || type==le_plink)) {
zend_list_addref(link);
Z_LVAL_P(return_value) = link;
php_mysql_set_default_link(link TSRMLS_CC);
Z_TYPE_P(return_value) = IS_RESOURCE;
efree(hashed_details);
MYSQL_DO_CONNECT_CLEANUP();
return;
} else {
zend_hash_del(&EG(regular_list), hashed_details, hashed_details_length+1);
}
}
if (MySG(max_links) != -1 && MySG(num_links) >= MySG(max_links)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Too many open links (%ld)", MySG(num_links));
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
mysql = (php_mysql_conn *) emalloc(sizeof(php_mysql_conn));
mysql->active_result_id = 0;
#ifdef CLIENT_MULTI_STATEMENTS
mysql->multi_query = 1;
#endif
#ifndef MYSQL_USE_MYSQLND
mysql->conn = mysql_init(NULL);
#else
mysql->conn = mysql_init(persistent);
#endif
if (!mysql->conn) {
MySG(connect_error) = estrdup("OOM");
php_error_docref(NULL TSRMLS_CC, E_WARNING, "OOM");
efree(hashed_details);
efree(mysql);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
if (connect_timeout != -1) {
mysql_options(mysql->conn, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout);
}
#ifndef MYSQL_USE_MYSQLND
if (mysql_real_connect(mysql->conn, host, user, passwd, NULL, port, socket, client_flags)==NULL)
#else
if (mysqlnd_connect(mysql->conn, host, user, passwd, 0, NULL, 0, port, socket, client_flags TSRMLS_CC) == NULL)
#endif
{
/* Populate connect error globals so that the error functions can read them */
if (MySG(connect_error) != NULL) {
efree(MySG(connect_error));
}
MySG(connect_error) = estrdup(mysql_error(mysql->conn));
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", MySG(connect_error));
#if defined(HAVE_MYSQL_ERRNO)
MySG(connect_errno) = mysql_errno(mysql->conn);
#endif
/* free mysql structure */
#ifdef MYSQL_USE_MYSQLND
mysqlnd_close(mysql->conn, MYSQLND_CLOSE_DISCONNECTED);
#endif
efree(hashed_details);
efree(mysql);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
mysql_options(mysql->conn, MYSQL_OPT_LOCAL_INFILE, (char *)&MySG(allow_local_infile));
/* add it to the list */
ZEND_REGISTER_RESOURCE(return_value, mysql, le_link);
/* add it to the hash */
new_index_ptr.ptr = (void *) Z_LVAL_P(return_value);
Z_TYPE(new_index_ptr) = le_index_ptr;
if (zend_hash_update(&EG(regular_list), hashed_details, hashed_details_length+1,(void *) &new_index_ptr, sizeof(zend_rsrc_list_entry), NULL)==FAILURE) {
efree(hashed_details);
MYSQL_DO_CONNECT_RETURN_FALSE();
}
MySG(num_links)++;
}
efree(hashed_details);
php_mysql_set_default_link(Z_LVAL_P(return_value) TSRMLS_CC);
MYSQL_DO_CONNECT_CLEANUP();
}
/* }}} */
/* {{{ php_mysql_get_default_link
*/
static int php_mysql_get_default_link(INTERNAL_FUNCTION_PARAMETERS)
{
if (MySG(default_link)==-1) { /* no link opened yet, implicitly open one */
ht = 0;
php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
return MySG(default_link);
}
/* }}} */
/* {{{ proto resource mysql_connect([string hostname[:port][:/path/to/socket] [, string username [, string password [, bool new [, int flags]]]]])
Opens a connection to a MySQL Server */
PHP_FUNCTION(mysql_connect)
{
php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ proto resource mysql_pconnect([string hostname[:port][:/path/to/socket] [, string username [, string password [, int flags]]]])
Opens a persistent connection to a MySQL Server */
PHP_FUNCTION(mysql_pconnect)
{
php_mysql_do_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto bool mysql_close([int link_identifier])
Close a MySQL connection */
PHP_FUNCTION(mysql_close)
{
int resource_id;
zval *mysql_link=NULL;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (mysql_link) {
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, -1, "MySQL-Link", le_link, le_plink);
} else {
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, NULL, MySG(default_link), "MySQL-Link", le_link, le_plink);
}
resource_id = mysql_link ? Z_RESVAL_P(mysql_link) : MySG(default_link);
PHPMY_UNBUFFERED_QUERY_CHECK();
#ifdef MYSQL_USE_MYSQLND
{
int tmp;
if ((mysql = zend_list_find(resource_id, &tmp)) && tmp == le_plink) {
mysqlnd_end_psession(mysql->conn);
}
}
#endif
zend_list_delete(resource_id);
if (!mysql_link
|| (mysql_link && Z_RESVAL_P(mysql_link)==MySG(default_link))) {
MySG(default_link) = -1;
if (mysql_link) {
/* on an explicit close of the default connection it had a refcount of 2 so we need one more call */
zend_list_delete(resource_id);
}
}
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool mysql_select_db(string database_name [, int link_identifier])
Selects a MySQL database */
PHP_FUNCTION(mysql_select_db)
{
char *db;
int db_len;
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &db, &db_len, &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
if (php_mysql_select_db(mysql, db TSRMLS_CC)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
#ifdef HAVE_GETINFO_FUNCS
/* {{{ proto string mysql_get_client_info(void)
Returns a string that represents the client library version */
PHP_FUNCTION(mysql_get_client_info)
{
if (zend_parse_parameters_none() == FAILURE) {
return;
}
RETURN_STRING((char *)mysql_get_client_info(),1);
}
/* }}} */
/* {{{ proto string mysql_get_host_info([int link_identifier])
Returns a string describing the type of connection in use, including the server host name */
PHP_FUNCTION(mysql_get_host_info)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_STRING((char *)mysql_get_host_info(mysql->conn),1);
}
/* }}} */
/* {{{ proto int mysql_get_proto_info([int link_identifier])
Returns the protocol version used by current connection */
PHP_FUNCTION(mysql_get_proto_info)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_LONG(mysql_get_proto_info(mysql->conn));
}
/* }}} */
/* {{{ proto string mysql_get_server_info([int link_identifier])
Returns a string that represents the server version number */
PHP_FUNCTION(mysql_get_server_info)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_STRING((char *)mysql_get_server_info(mysql->conn),1);
}
/* }}} */
/* {{{ proto string mysql_info([int link_identifier])
Returns a string containing information about the most recent query */
PHP_FUNCTION(mysql_info)
{
zval *mysql_link = NULL;
int id = -1;
char *str;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() == 0) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
if ((str = (char *)mysql_info(mysql->conn))) {
RETURN_STRING(str,1);
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto int mysql_thread_id([int link_identifier])
Returns the thread id of current connection */
PHP_FUNCTION(mysql_thread_id)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() == 0) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_LONG((long) mysql_thread_id(mysql->conn));
}
/* }}} */
/* {{{ proto string mysql_stat([int link_identifier])
Returns a string containing status information */
PHP_FUNCTION(mysql_stat)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
char *stat;
#ifdef MYSQL_USE_MYSQLND
uint stat_len;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() == 0) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
PHPMY_UNBUFFERED_QUERY_CHECK();
#ifndef MYSQL_USE_MYSQLND
if ((stat = (char *)mysql_stat(mysql->conn))) {
RETURN_STRING(stat, 1);
#else
if (mysqlnd_stat(mysql->conn, &stat, &stat_len) == PASS) {
RETURN_STRINGL(stat, stat_len, 0);
#endif
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string mysql_client_encoding([int link_identifier])
Returns the default character set for the current connection */
PHP_FUNCTION(mysql_client_encoding)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() == 0) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_STRING((char *)mysql_character_set_name(mysql->conn), 1);
}
/* }}} */
#endif
#ifdef MYSQL_HAS_SET_CHARSET
/* {{{ proto bool mysql_set_charset(string csname [, int link_identifier])
sets client character set */
PHP_FUNCTION(mysql_set_charset)
{
zval *mysql_link = NULL;
char *csname;
int id = -1, csname_len;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &csname, &csname_len, &mysql_link) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() == 1) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
if (!mysql_set_character_set(mysql->conn, csname)) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
#endif
#ifndef NETWARE /* The below two functions not supported on NetWare */
#if MYSQL_VERSION_ID < 40000
/* {{{ proto bool mysql_create_db(string database_name [, int link_identifier])
Create a MySQL database */
PHP_FUNCTION(mysql_create_db)
{
char *db;
int db_len;
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &db, &db_len, &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
PHPMY_UNBUFFERED_QUERY_CHECK();
if (mysql_create_db(mysql->conn, db)==0) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto bool mysql_drop_db(string database_name [, int link_identifier])
Drops (delete) a MySQL database */
PHP_FUNCTION(mysql_drop_db)
{
char *db;
int db_len;
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &db, &db_len, &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
if (mysql_drop_db(mysql->conn, db)==0) {
RETURN_TRUE;
} else {
RETURN_FALSE;
}
}
/* }}} */
#endif
#endif /* NETWARE */
/* {{{ php_mysql_do_query_general
*/
static void php_mysql_do_query_general(char *query, int query_len, zval *mysql_link, int link_id, char *db, int use_store, zval *return_value TSRMLS_DC)
{
php_mysql_conn *mysql;
MYSQL_RES *mysql_result;
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, link_id, "MySQL-Link", le_link, le_plink);
if (db) {
if (!php_mysql_select_db(mysql, db TSRMLS_CC)) {
RETURN_FALSE;
}
}
PHPMY_UNBUFFERED_QUERY_CHECK();
MYSQL_DISABLE_MQ;
#ifndef MYSQL_USE_MYSQLND
/* check explain */
if (MySG(trace_mode)) {
if (!strncasecmp("select", query, 6)){
MYSQL_ROW row;
char *newquery;
int newql = spprintf (&newquery, 0, "EXPLAIN %s", query);
mysql_real_query(mysql->conn, newquery, newql);
efree (newquery);
if (mysql_errno(mysql->conn)) {
php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(mysql->conn));
RETURN_FALSE;
}
else {
mysql_result = mysql_use_result(mysql->conn);
while ((row = mysql_fetch_row(mysql_result))) {
if (!strcmp("ALL", row[1])) {
php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "Your query requires a full tablescan (table %s, %s rows affected). Use EXPLAIN to optimize your query.", row[0], row[6]);
} else if (!strcmp("INDEX", row[1])) {
php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "Your query requires a full indexscan (table %s, %s rows affected). Use EXPLAIN to optimize your query.", row[0], row[6]);
}
}
mysql_free_result(mysql_result);
}
}
} /* end explain */
#endif
/* mysql_query is binary unsafe, use mysql_real_query */
#if MYSQL_VERSION_ID > 32199
if (mysql_real_query(mysql->conn, query, query_len)!=0) {
/* check possible error */
if (MySG(trace_mode)){
if (mysql_errno(mysql->conn)){
php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(mysql->conn));
}
}
RETURN_FALSE;
}
#else
if (mysql_query(mysql->conn, query)!=0) {
/* check possible error */
if (MySG(trace_mode)){
if (mysql_errno(mysql->conn)){
php_error_docref("http://www.mysql.com/doc" TSRMLS_CC, E_WARNING, "%s", mysql_error(mysql->conn));
}
}
RETURN_FALSE;
}
#endif
if(use_store == MYSQL_USE_RESULT) {
mysql_result=mysql_use_result(mysql->conn);
} else {
mysql_result=mysql_store_result(mysql->conn);
}
if (!mysql_result) {
if (PHP_MYSQL_VALID_RESULT(mysql->conn)) { /* query should have returned rows */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save result set");
RETURN_FALSE;
} else {
RETURN_TRUE;
}
}
MySG(result_allocated)++;
ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
if (use_store == MYSQL_USE_RESULT) {
mysql->active_result_id = Z_LVAL_P(return_value);
}
}
/* }}} */
/* {{{ php_mysql_do_query
*/
static void php_mysql_do_query(INTERNAL_FUNCTION_PARAMETERS, int use_store)
{
char *query;
int query_len;
zval *mysql_link = NULL;
int id = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &query, &query_len, &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
php_mysql_do_query_general(query, query_len, mysql_link, id, NULL, use_store, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ proto resource mysql_query(string query [, int link_identifier])
Sends an SQL query to MySQL */
PHP_FUNCTION(mysql_query)
{
php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_STORE_RESULT);
}
/* }}} */
/* {{{ proto resource mysql_unbuffered_query(string query [, int link_identifier])
Sends an SQL query to MySQL, without fetching and buffering the result rows */
PHP_FUNCTION(mysql_unbuffered_query)
{
php_mysql_do_query(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_USE_RESULT);
}
/* }}} */
/* {{{ proto resource mysql_db_query(string database_name, string query [, int link_identifier])
Sends an SQL query to MySQL */
PHP_FUNCTION(mysql_db_query)
{
char *db, *query;
int db_len, query_len;
zval *mysql_link = NULL;
int id = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|r", &db, &db_len, &query, &query_len, &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
php_error_docref(NULL TSRMLS_CC, E_DEPRECATED, "This function is deprecated; use mysql_query() instead");
php_mysql_do_query_general(query, query_len, mysql_link, id, db, MYSQL_STORE_RESULT, return_value TSRMLS_CC);
}
/* }}} */
/* {{{ proto resource mysql_list_dbs([int link_identifier])
List databases available on a MySQL server */
PHP_FUNCTION(mysql_list_dbs)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
MYSQL_RES *mysql_result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
PHPMY_UNBUFFERED_QUERY_CHECK();
if ((mysql_result=mysql_list_dbs(mysql->conn, NULL))==NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
}
/* }}} */
/* {{{ proto resource mysql_list_tables(string database_name [, int link_identifier])
List tables in a MySQL database */
PHP_FUNCTION(mysql_list_tables)
{
char *db;
int db_len;
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
MYSQL_RES *mysql_result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &db, &db_len, &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
if (!php_mysql_select_db(mysql, db TSRMLS_CC)) {
RETURN_FALSE;
}
PHPMY_UNBUFFERED_QUERY_CHECK();
if ((mysql_result=mysql_list_tables(mysql->conn, NULL))==NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
}
/* }}} */
/* {{{ proto resource mysql_list_fields(string database_name, string table_name [, int link_identifier])
List MySQL result fields */
PHP_FUNCTION(mysql_list_fields)
{
char *db, *table;
int db_len, table_len;
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
MYSQL_RES *mysql_result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|r", &db, &db_len, &table, &table_len, &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
if (!php_mysql_select_db(mysql, db TSRMLS_CC)) {
RETURN_FALSE;
}
PHPMY_UNBUFFERED_QUERY_CHECK();
if ((mysql_result=mysql_list_fields(mysql->conn, table, NULL))==NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
}
/* }}} */
/* {{{ proto resource mysql_list_processes([int link_identifier])
Returns a result set describing the current server threads */
PHP_FUNCTION(mysql_list_processes)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
MYSQL_RES *mysql_result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() == 0) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
PHPMY_UNBUFFERED_QUERY_CHECK();
mysql_result = mysql_list_processes(mysql->conn);
if (mysql_result == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to save MySQL query result");
RETURN_FALSE;
}
ZEND_REGISTER_RESOURCE(return_value, mysql_result, le_result);
}
/* }}} */
/* {{{ proto string mysql_error([int link_identifier])
Returns the text of the error message from previous MySQL operation */
PHP_FUNCTION(mysql_error)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = MySG(default_link);
if (id==-1) {
if (MySG(connect_error)!=NULL){
RETURN_STRING(MySG(connect_error),1);
} else {
RETURN_FALSE;
}
}
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_STRING((char *)mysql_error(mysql->conn), 1);
}
/* }}} */
/* {{{ proto int mysql_errno([int link_identifier])
Returns the number of the error message from previous MySQL operation */
#ifdef HAVE_MYSQL_ERRNO
PHP_FUNCTION(mysql_errno)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = MySG(default_link);
if (id==-1) {
if (MySG(connect_errno)!=0){
RETURN_LONG(MySG(connect_errno));
} else {
RETURN_FALSE;
}
}
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
RETURN_LONG(mysql_errno(mysql->conn));
}
#endif
/* }}} */
/* {{{ proto int mysql_affected_rows([int link_identifier])
Gets number of affected rows in previous MySQL operation */
PHP_FUNCTION(mysql_affected_rows)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
/* conversion from int64 to long happing here */
Z_LVAL_P(return_value) = (long) mysql_affected_rows(mysql->conn);
Z_TYPE_P(return_value) = IS_LONG;
}
/* }}} */
/* {{{ proto string mysql_escape_string(string to_be_escaped)
Escape string for mysql query */
PHP_FUNCTION(mysql_escape_string)
{
char *str;
int str_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
return;
}
/* assume worst case situation, which is 2x of the original string.
* we don't realloc() down to the real size since it'd most probably not
* be worth it
*/
Z_STRVAL_P(return_value) = (char *) safe_emalloc(str_len, 2, 1);
Z_STRLEN_P(return_value) = mysql_escape_string(Z_STRVAL_P(return_value), str, str_len);
Z_TYPE_P(return_value) = IS_STRING;
if (MySG(trace_mode)){
php_error_docref("function.mysql-real-escape-string" TSRMLS_CC, E_DEPRECATED, "This function is deprecated; use mysql_real_escape_string() instead.");
}
}
/* }}} */
/* {{{ proto string mysql_real_escape_string(string to_be_escaped [, int link_identifier])
Escape special characters in a string for use in a SQL statement, taking into account the current charset of the connection */
PHP_FUNCTION(mysql_real_escape_string)
{
zval *mysql_link = NULL;
char *str;
char *new_str;
int id = -1, str_len, new_str_len;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|r", &str, &str_len, &mysql_link) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() == 1) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
new_str = safe_emalloc(str_len, 2, 1);
new_str_len = mysql_real_escape_string(mysql->conn, new_str, str, str_len);
new_str = erealloc(new_str, new_str_len + 1);
RETURN_STRINGL(new_str, new_str_len, 0);
}
/* }}} */
/* {{{ proto int mysql_insert_id([int link_identifier])
Gets the ID generated from the previous INSERT operation */
PHP_FUNCTION(mysql_insert_id)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &mysql_link) == FAILURE) {
return;
}
if (!mysql_link) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
/* conversion from int64 to long happing here */
Z_LVAL_P(return_value) = (long) mysql_insert_id(mysql->conn);
Z_TYPE_P(return_value) = IS_LONG;
}
/* }}} */
/* {{{ proto mixed mysql_result(resource result, int row [, mixed field])
Gets result data */
PHP_FUNCTION(mysql_result)
{
zval *result, *field=NULL;
long row;
MYSQL_RES *mysql_result;
#ifndef MYSQL_USE_MYSQLND
MYSQL_ROW sql_row;
mysql_row_length_type *sql_row_lengths;
#endif
int field_offset=0;
/*
johannes TODO:
Do 2 zend_parse_paramters calls instead of type "z" and switch below
Q: String or long first?
*/
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl|z", &result, &row, &field) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &result, -1, "MySQL result", le_result);
if (row<0 || row>=(int)mysql_num_rows(mysql_result)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unable to jump to row %ld on MySQL result index %ld", row, Z_LVAL_P(result));
RETURN_FALSE;
}
mysql_data_seek(mysql_result, row);
if (field) {
switch(Z_TYPE_P(field)) {
case IS_STRING: {
int i=0;
const MYSQL_FIELD *tmp_field;
char *table_name, *field_name, *tmp;
if ((tmp=strchr(Z_STRVAL_P(field), '.'))) {
table_name = estrndup(Z_STRVAL_P(field), tmp-Z_STRVAL_P(field));
field_name = estrdup(tmp+1);
} else {
table_name = NULL;
field_name = estrndup(Z_STRVAL_P(field),Z_STRLEN_P(field));
}
mysql_field_seek(mysql_result, 0);
while ((tmp_field=mysql_fetch_field(mysql_result))) {
if ((!table_name || !strcasecmp(tmp_field->table, table_name)) && !strcasecmp(tmp_field->name, field_name)) {
field_offset = i;
break;
}
i++;
}
if (!tmp_field) { /* no match found */
php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s%s%s not found in MySQL result index %ld",
(table_name?table_name:""), (table_name?".":""), field_name, Z_LVAL_P(result));
efree(field_name);
if (table_name) {
efree(table_name);
}
RETURN_FALSE;
}
efree(field_name);
if (table_name) {
efree(table_name);
}
}
break;
default:
convert_to_long_ex(&field);
field_offset = Z_LVAL_P(field);
if (field_offset<0 || field_offset>=(int)mysql_num_fields(mysql_result)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad column offset specified");
RETURN_FALSE;
}
break;
}
}
#ifndef MYSQL_USE_MYSQLND
if ((sql_row=mysql_fetch_row(mysql_result))==NULL
|| (sql_row_lengths=mysql_fetch_lengths(mysql_result))==NULL) { /* shouldn't happen? */
RETURN_FALSE;
}
if (sql_row[field_offset]) {
Z_TYPE_P(return_value) = IS_STRING;
if (PG(magic_quotes_runtime)) {
Z_STRVAL_P(return_value) = php_addslashes(sql_row[field_offset], sql_row_lengths[field_offset],&Z_STRLEN_P(return_value), 0 TSRMLS_CC);
} else {
Z_STRLEN_P(return_value) = sql_row_lengths[field_offset];
Z_STRVAL_P(return_value) = (char *) safe_estrndup(sql_row[field_offset], Z_STRLEN_P(return_value));
}
} else {
Z_TYPE_P(return_value) = IS_NULL;
}
#else
mysqlnd_result_fetch_field_data(mysql_result, field_offset, return_value);
#endif
}
/* }}} */
/* {{{ proto int mysql_num_rows(resource result)
Gets number of rows in a result */
PHP_FUNCTION(mysql_num_rows)
{
zval *result;
MYSQL_RES *mysql_result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &result, -1, "MySQL result", le_result);
/* conversion from int64 to long happing here */
Z_LVAL_P(return_value) = (long) mysql_num_rows(mysql_result);
Z_TYPE_P(return_value) = IS_LONG;
}
/* }}} */
/* {{{ proto int mysql_num_fields(resource result)
Gets number of fields in a result */
PHP_FUNCTION(mysql_num_fields)
{
zval *result;
MYSQL_RES *mysql_result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &result, -1, "MySQL result", le_result);
Z_LVAL_P(return_value) = mysql_num_fields(mysql_result);
Z_TYPE_P(return_value) = IS_LONG;
}
/* }}} */
/* {{{ php_mysql_fetch_hash
*/
static void php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAMETERS, int result_type, int expected_args, int into_object)
{
MYSQL_RES *mysql_result;
zval *res, *ctor_params = NULL;
zend_class_entry *ce = NULL;
#ifndef MYSQL_USE_MYSQLND
int i;
MYSQL_FIELD *mysql_field;
MYSQL_ROW mysql_row;
mysql_row_length_type *mysql_row_lengths;
#endif
#ifdef ZEND_ENGINE_2
if (into_object) {
char *class_name = NULL;
int class_name_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|sz", &res, &class_name, &class_name_len, &ctor_params) == FAILURE) {
return;
}
if (ZEND_NUM_ARGS() < 2) {
ce = zend_standard_class_def;
} else {
ce = zend_fetch_class(class_name, class_name_len, ZEND_FETCH_CLASS_AUTO TSRMLS_CC);
}
if (!ce) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not find class '%s'", class_name);
return;
}
result_type = MYSQL_ASSOC;
} else
#endif
{
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &res, &result_type) == FAILURE) {
return;
}
if (!result_type) {
/* result_type might have been set outside, so only overwrite when not set */
result_type = MYSQL_BOTH;
}
}
if (result_type & ~MYSQL_BOTH) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "The result type should be either MYSQL_NUM, MYSQL_ASSOC or MYSQL_BOTH");
result_type = MYSQL_BOTH;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &res, -1, "MySQL result", le_result);
#ifndef MYSQL_USE_MYSQLND
if ((mysql_row = mysql_fetch_row(mysql_result)) == NULL ||
(mysql_row_lengths = mysql_fetch_lengths(mysql_result)) == NULL) {
RETURN_FALSE;
}
array_init(return_value);
mysql_field_seek(mysql_result, 0);
for (mysql_field = mysql_fetch_field(mysql_result), i = 0;
mysql_field;
mysql_field = mysql_fetch_field(mysql_result), i++)
{
if (mysql_row[i]) {
zval *data;
MAKE_STD_ZVAL(data);
if (PG(magic_quotes_runtime)) {
Z_TYPE_P(data) = IS_STRING;
Z_STRVAL_P(data) = php_addslashes(mysql_row[i], mysql_row_lengths[i], &Z_STRLEN_P(data), 0 TSRMLS_CC);
} else {
ZVAL_STRINGL(data, mysql_row[i], mysql_row_lengths[i], 1);
}
if (result_type & MYSQL_NUM) {
add_index_zval(return_value, i, data);
}
if (result_type & MYSQL_ASSOC) {
if (result_type & MYSQL_NUM) {
Z_ADDREF_P(data);
}
add_assoc_zval(return_value, mysql_field->name, data);
}
} else {
/* NULL value. */
if (result_type & MYSQL_NUM) {
add_index_null(return_value, i);
}
if (result_type & MYSQL_ASSOC) {
add_assoc_null(return_value, mysql_field->name);
}
}
}
#else
mysqlnd_fetch_into(mysql_result, ((result_type & MYSQL_NUM)? MYSQLND_FETCH_NUM:0) | ((result_type & MYSQL_ASSOC)? MYSQLND_FETCH_ASSOC:0), return_value, MYSQLND_MYSQL);
#endif
#ifdef ZEND_ENGINE_2
/* mysqlnd might return FALSE if no more rows */
if (into_object && Z_TYPE_P(return_value) != IS_BOOL) {
zval dataset = *return_value;
zend_fcall_info fci;
zend_fcall_info_cache fcc;
zval *retval_ptr;
object_and_properties_init(return_value, ce, NULL);
zend_merge_properties(return_value, Z_ARRVAL(dataset), 1 TSRMLS_CC);
if (ce->constructor) {
fci.size = sizeof(fci);
fci.function_table = &ce->function_table;
fci.function_name = NULL;
fci.symbol_table = NULL;
fci.object_ptr = return_value;
fci.retval_ptr_ptr = &retval_ptr;
if (ctor_params && Z_TYPE_P(ctor_params) != IS_NULL) {
if (Z_TYPE_P(ctor_params) == IS_ARRAY) {
HashTable *ht = Z_ARRVAL_P(ctor_params);
Bucket *p;
fci.param_count = 0;
fci.params = safe_emalloc(sizeof(zval*), ht->nNumOfElements, 0);
p = ht->pListHead;
while (p != NULL) {
fci.params[fci.param_count++] = (zval**)p->pData;
p = p->pListNext;
}
} else {
/* Two problems why we throw exceptions here: PHP is typeless
* and hence passing one argument that's not an array could be
* by mistake and the other way round is possible, too. The
* single value is an array. Also we'd have to make that one
* argument passed by reference.
*/
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Parameter ctor_params must be an array", 0 TSRMLS_CC);
return;
}
} else {
fci.param_count = 0;
fci.params = NULL;
}
fci.no_separation = 1;
fcc.initialized = 1;
fcc.function_handler = ce->constructor;
fcc.calling_scope = EG(scope);
fcc.called_scope = Z_OBJCE_P(return_value);
fcc.object_ptr = return_value;
if (zend_call_function(&fci, &fcc TSRMLS_CC) == FAILURE) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Could not execute %s::%s()", ce->name, ce->constructor->common.function_name);
} else {
if (retval_ptr) {
zval_ptr_dtor(&retval_ptr);
}
}
if (fci.params) {
efree(fci.params);
}
} else if (ctor_params) {
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0 TSRMLS_CC, "Class %s does not have a constructor hence you cannot use ctor_params", ce->name);
}
}
#endif
}
/* }}} */
/* {{{ proto array mysql_fetch_row(resource result)
Gets a result row as an enumerated array */
PHP_FUNCTION(mysql_fetch_row)
{
php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_NUM, 1, 0);
}
/* }}} */
/* {{{ proto object mysql_fetch_object(resource result [, string class_name [, NULL|array ctor_params]])
Fetch a result row as an object */
PHP_FUNCTION(mysql_fetch_object)
{
php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_ASSOC, 2, 1);
if (Z_TYPE_P(return_value) == IS_ARRAY) {
object_and_properties_init(return_value, ZEND_STANDARD_CLASS_DEF_PTR, Z_ARRVAL_P(return_value));
}
}
/* }}} */
/* {{{ proto array mysql_fetch_array(resource result [, int result_type])
Fetch a result row as an array (associative, numeric or both) */
PHP_FUNCTION(mysql_fetch_array)
{
php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 2, 0);
}
/* }}} */
/* {{{ proto array mysql_fetch_assoc(resource result)
Fetch a result row as an associative array */
PHP_FUNCTION(mysql_fetch_assoc)
{
php_mysql_fetch_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQL_ASSOC, 1, 0);
}
/* }}} */
/* {{{ proto bool mysql_data_seek(resource result, int row_number)
Move internal result pointer */
PHP_FUNCTION(mysql_data_seek)
{
zval *result;
long offset;
MYSQL_RES *mysql_result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &offset)) {
return;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &result, -1, "MySQL result", le_result);
if (offset<0 || offset>=(int)mysql_num_rows(mysql_result)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Offset %ld is invalid for MySQL result index %ld (or the query data is unbuffered)", offset, Z_LVAL_P(result));
RETURN_FALSE;
}
mysql_data_seek(mysql_result, offset);
RETURN_TRUE;
}
/* }}} */
/* {{{ proto array mysql_fetch_lengths(resource result)
Gets max data size of each column in a result */
PHP_FUNCTION(mysql_fetch_lengths)
{
zval *result;
MYSQL_RES *mysql_result;
mysql_row_length_type *lengths;
int num_fields;
int i;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &result, -1, "MySQL result", le_result);
if ((lengths=mysql_fetch_lengths(mysql_result))==NULL) {
RETURN_FALSE;
}
array_init(return_value);
num_fields = mysql_num_fields(mysql_result);
for (i=0; i<num_fields; i++) {
add_index_long(return_value, i, lengths[i]);
}
}
/* }}} */
/* {{{ php_mysql_get_field_name
*/
static char *php_mysql_get_field_name(int field_type)
{
switch(field_type) {
case FIELD_TYPE_STRING:
case FIELD_TYPE_VAR_STRING:
return "string";
break;
#if MYSQL_VERSION_ID > 50002 || defined(MYSQL_USE_MYSQLND)
case MYSQL_TYPE_BIT:
#endif
#ifdef MYSQL_HAS_TINY
case FIELD_TYPE_TINY:
#endif
case FIELD_TYPE_SHORT:
case FIELD_TYPE_LONG:
case FIELD_TYPE_LONGLONG:
case FIELD_TYPE_INT24:
return "int";
break;
case FIELD_TYPE_FLOAT:
case FIELD_TYPE_DOUBLE:
case FIELD_TYPE_DECIMAL:
#ifdef FIELD_TYPE_NEWDECIMAL
case FIELD_TYPE_NEWDECIMAL:
#endif
return "real";
break;
case FIELD_TYPE_TIMESTAMP:
return "timestamp";
break;
#ifdef MYSQL_HAS_YEAR
case FIELD_TYPE_YEAR:
return "year";
break;
#endif
case FIELD_TYPE_DATE:
#ifdef FIELD_TYPE_NEWDATE
case FIELD_TYPE_NEWDATE:
#endif
return "date";
break;
case FIELD_TYPE_TIME:
return "time";
break;
case FIELD_TYPE_SET:
return "set";
break;
case FIELD_TYPE_ENUM:
return "enum";
break;
#ifdef FIELD_TYPE_GEOMETRY
case FIELD_TYPE_GEOMETRY:
return "geometry";
break;
#endif
case FIELD_TYPE_DATETIME:
return "datetime";
break;
case FIELD_TYPE_TINY_BLOB:
case FIELD_TYPE_MEDIUM_BLOB:
case FIELD_TYPE_LONG_BLOB:
case FIELD_TYPE_BLOB:
return "blob";
break;
case FIELD_TYPE_NULL:
return "null";
break;
default:
return "unknown";
break;
}
}
/* }}} */
/* {{{ proto object mysql_fetch_field(resource result [, int field_offset])
Gets column information from a result and return as an object */
PHP_FUNCTION(mysql_fetch_field)
{
zval *result;
long field=0;
MYSQL_RES *mysql_result;
const MYSQL_FIELD *mysql_field;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|l", &result, &field) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &result, -1, "MySQL result", le_result);
if (ZEND_NUM_ARGS() > 1) {
if (field<0 || field>=(int)mysql_num_fields(mysql_result)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Bad field offset");
RETURN_FALSE;
}
mysql_field_seek(mysql_result, field);
}
if ((mysql_field=mysql_fetch_field(mysql_result))==NULL) {
RETURN_FALSE;
}
object_init(return_value);
add_property_string(return_value, "name",(mysql_field->name?mysql_field->name:""), 1);
add_property_string(return_value, "table",(mysql_field->table?mysql_field->table:""), 1);
add_property_string(return_value, "def",(mysql_field->def?mysql_field->def:""), 1);
add_property_long(return_value, "max_length", mysql_field->max_length);
add_property_long(return_value, "not_null", IS_NOT_NULL(mysql_field->flags)?1:0);
add_property_long(return_value, "primary_key", IS_PRI_KEY(mysql_field->flags)?1:0);
add_property_long(return_value, "multiple_key",(mysql_field->flags&MULTIPLE_KEY_FLAG?1:0));
add_property_long(return_value, "unique_key",(mysql_field->flags&UNIQUE_KEY_FLAG?1:0));
add_property_long(return_value, "numeric", IS_NUM(Z_TYPE_P(mysql_field))?1:0);
add_property_long(return_value, "blob", IS_BLOB(mysql_field->flags)?1:0);
add_property_string(return_value, "type", php_mysql_get_field_name(Z_TYPE_P(mysql_field)), 1);
add_property_long(return_value, "unsigned",(mysql_field->flags&UNSIGNED_FLAG?1:0));
add_property_long(return_value, "zerofill",(mysql_field->flags&ZEROFILL_FLAG?1:0));
}
/* }}} */
/* {{{ proto bool mysql_field_seek(resource result, int field_offset)
Sets result pointer to a specific field offset */
PHP_FUNCTION(mysql_field_seek)
{
zval *result;
long offset;
MYSQL_RES *mysql_result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &offset) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &result, -1, "MySQL result", le_result);
if (offset<0 || offset>=(int)mysql_num_fields(mysql_result)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field %ld is invalid for MySQL result index %ld", offset, Z_LVAL_P(result));
RETURN_FALSE;
}
mysql_field_seek(mysql_result, offset);
RETURN_TRUE;
}
/* }}} */
#define PHP_MYSQL_FIELD_NAME 1
#define PHP_MYSQL_FIELD_TABLE 2
#define PHP_MYSQL_FIELD_LEN 3
#define PHP_MYSQL_FIELD_TYPE 4
#define PHP_MYSQL_FIELD_FLAGS 5
/* {{{ php_mysql_field_info
*/
static void php_mysql_field_info(INTERNAL_FUNCTION_PARAMETERS, int entry_type)
{
zval *result;
long field;
MYSQL_RES *mysql_result;
const MYSQL_FIELD *mysql_field = {0};
char buf[512];
int len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &result, &field) == FAILURE) {
return;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &result, -1, "MySQL result", le_result);
if (field<0 || field>=(int)mysql_num_fields(mysql_result)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Field %ld is invalid for MySQL result index %ld", field, Z_LVAL_P(result));
RETURN_FALSE;
}
mysql_field_seek(mysql_result, field);
if ((mysql_field=mysql_fetch_field(mysql_result))==NULL) {
RETURN_FALSE;
}
switch (entry_type) {
case PHP_MYSQL_FIELD_NAME:
Z_STRLEN_P(return_value) = strlen(mysql_field->name);
Z_STRVAL_P(return_value) = estrndup(mysql_field->name, Z_STRLEN_P(return_value));
Z_TYPE_P(return_value) = IS_STRING;
break;
case PHP_MYSQL_FIELD_TABLE:
Z_STRLEN_P(return_value) = strlen(mysql_field->table);
Z_STRVAL_P(return_value) = estrndup(mysql_field->table, Z_STRLEN_P(return_value));
Z_TYPE_P(return_value) = IS_STRING;
break;
case PHP_MYSQL_FIELD_LEN:
Z_LVAL_P(return_value) = mysql_field->length;
Z_TYPE_P(return_value) = IS_LONG;
break;
case PHP_MYSQL_FIELD_TYPE:
Z_STRVAL_P(return_value) = php_mysql_get_field_name(Z_TYPE_P(mysql_field));
Z_STRLEN_P(return_value) = strlen(Z_STRVAL_P(return_value));
Z_STRVAL_P(return_value) = estrndup(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value));
Z_TYPE_P(return_value) = IS_STRING;
break;
case PHP_MYSQL_FIELD_FLAGS:
memcpy(buf, "", sizeof(""));
#ifdef IS_NOT_NULL
if (IS_NOT_NULL(mysql_field->flags)) {
strcat(buf, "not_null ");
}
#endif
#ifdef IS_PRI_KEY
if (IS_PRI_KEY(mysql_field->flags)) {
strcat(buf, "primary_key ");
}
#endif
#ifdef UNIQUE_KEY_FLAG
if (mysql_field->flags&UNIQUE_KEY_FLAG) {
strcat(buf, "unique_key ");
}
#endif
#ifdef MULTIPLE_KEY_FLAG
if (mysql_field->flags&MULTIPLE_KEY_FLAG) {
strcat(buf, "multiple_key ");
}
#endif
#ifdef IS_BLOB
if (IS_BLOB(mysql_field->flags)) {
strcat(buf, "blob ");
}
#endif
#ifdef UNSIGNED_FLAG
if (mysql_field->flags&UNSIGNED_FLAG) {
strcat(buf, "unsigned ");
}
#endif
#ifdef ZEROFILL_FLAG
if (mysql_field->flags&ZEROFILL_FLAG) {
strcat(buf, "zerofill ");
}
#endif
#ifdef BINARY_FLAG
if (mysql_field->flags&BINARY_FLAG) {
strcat(buf, "binary ");
}
#endif
#ifdef ENUM_FLAG
if (mysql_field->flags&ENUM_FLAG) {
strcat(buf, "enum ");
}
#endif
#ifdef SET_FLAG
if (mysql_field->flags&SET_FLAG) {
strcat(buf, "set ");
}
#endif
#ifdef AUTO_INCREMENT_FLAG
if (mysql_field->flags&AUTO_INCREMENT_FLAG) {
strcat(buf, "auto_increment ");
}
#endif
#ifdef TIMESTAMP_FLAG
if (mysql_field->flags&TIMESTAMP_FLAG) {
strcat(buf, "timestamp ");
}
#endif
len = strlen(buf);
/* remove trailing space, if present */
if (len && buf[len-1] == ' ') {
buf[len-1] = 0;
len--;
}
Z_STRLEN_P(return_value) = len;
Z_STRVAL_P(return_value) = estrndup(buf, len);
Z_TYPE_P(return_value) = IS_STRING;
break;
default:
RETURN_FALSE;
}
}
/* }}} */
/* {{{ proto string mysql_field_name(resource result, int field_index)
Gets the name of the specified field in a result */
PHP_FUNCTION(mysql_field_name)
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_NAME);
}
/* }}} */
/* {{{ proto string mysql_field_table(resource result, int field_offset)
Gets name of the table the specified field is in */
PHP_FUNCTION(mysql_field_table)
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_TABLE);
}
/* }}} */
/* {{{ proto int mysql_field_len(resource result, int field_offset)
Returns the length of the specified field */
PHP_FUNCTION(mysql_field_len)
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_LEN);
}
/* }}} */
/* {{{ proto string mysql_field_type(resource result, int field_offset)
Gets the type of the specified field in a result */
PHP_FUNCTION(mysql_field_type)
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_TYPE);
}
/* }}} */
/* {{{ proto string mysql_field_flags(resource result, int field_offset)
Gets the flags associated with the specified field in a result */
PHP_FUNCTION(mysql_field_flags)
{
php_mysql_field_info(INTERNAL_FUNCTION_PARAM_PASSTHRU, PHP_MYSQL_FIELD_FLAGS);
}
/* }}} */
/* {{{ proto bool mysql_free_result(resource result)
Free result memory */
PHP_FUNCTION(mysql_free_result)
{
zval *result;
MYSQL_RES *mysql_result;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &result) == FAILURE) {
return;
}
if (Z_LVAL_P(result)==0) {
RETURN_FALSE;
}
ZEND_FETCH_RESOURCE(mysql_result, MYSQL_RES *, &result, -1, "MySQL result", le_result);
zend_list_delete(Z_LVAL_P(result));
RETURN_TRUE;
}
/* }}} */
/* {{{ proto bool mysql_ping([int link_identifier])
Ping a server connection. If no connection then reconnect. */
PHP_FUNCTION(mysql_ping)
{
zval *mysql_link = NULL;
int id = -1;
php_mysql_conn *mysql;
if (0 == ZEND_NUM_ARGS()) {
id = php_mysql_get_default_link(INTERNAL_FUNCTION_PARAM_PASSTHRU);
CHECK_LINK(id);
} else if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &mysql_link)==FAILURE) {
return;
}
ZEND_FETCH_RESOURCE2(mysql, php_mysql_conn *, &mysql_link, id, "MySQL-Link", le_link, le_plink);
PHPMY_UNBUFFERED_QUERY_CHECK();
RETURN_BOOL(! mysql_ping(mysql->conn));
}
/* }}} */
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
|