Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: None | Size: 72.27 KB | Hits: 24 | Expires: Never
Copy text to clipboard
  1. /*
  2. ** mIRC MySQL v1.0.0b
  3. **
  4. ** Author: Reko Tiira [ ramirez ]
  5. ** E-mail: reko@tiira.net
  6. ** Date: 18th March 2009
  7. ** IRC: ramirez @ irc.undernet.org [ #mircscripting ]
  8. **
  9. ** Please see mmysql.chm for documentation.
  10. **
  11. ** Any questions, suggestions and such should be
  12. ** e-mailed at the above e-mail.
  13. */
  14.  
  15. /*
  16. ** MySQL Constants
  17. */
  18.  
  19. alias MYSQL_OK                   return 0
  20.  
  21. alias MYSQL_BOTH                 return 1
  22. alias MYSQL_NUM                  return 2
  23. alias MYSQL_ASSOC                return 3
  24.  
  25. alias MYSQL_ALL                  return 1
  26. alias MYSQL_BOUND                return 2
  27.  
  28. alias MYSQL_ERROR_OK             return 0
  29. alias MYSQL_ERROR_INVALIDARG     return 3000
  30. alias MYSQL_ERROR_BIND           return 3001
  31. alias MYSQL_ERROR_NOMOREROWS     return 3002
  32. alias MYSQL_ERROR_FETCH          return 3003
  33. alias MYSQL_ERROR_NOMOREFIELDS   return 3004
  34.  
  35. /*
  36. ** On Load Event
  37. */
  38.  
  39. on *:LOAD:{
  40.   if ($version < 6.2) {
  41.     echo 4 -a Obsoloete mIRC version $version
  42.     echo 4 -a mIRC MySQL requires at least mIRC 6.2
  43.     echo 4 -a Script wasn't loaded.
  44.     unload -rs $+(",$script,")
  45.     halt
  46.   }
  47.  
  48.   echo 3 -a mIRC MySQL loaded successfully.
  49. }
  50.  
  51. /*
  52. ** MySQL DLL Path
  53. */
  54.  
  55. alias -l mysql_dll return $qt($+($scriptdir,mmysql.dll))
  56.  
  57. /*
  58. ** MySQL Internals
  59. */
  60.  
  61. alias -l mysql_param return $qt($replace($1, \, \\, ", \"))
  62.  
  63. /*
  64. ** MySQL API
  65. */
  66.  
  67. ;;; <summary>Opens the help file.</summary>
  68. ;;; <syntax>/mysql_help</syntax>
  69. alias mysql_help {
  70.   run $+($scriptdir,mmysql.chm)
  71. }
  72.  
  73. ;;; <summary>Returns the version of the mIRC MySQL DLL.</summary>
  74. ;;; <syntax>$mysql_version</syntax>
  75. ;;; <returns>The version of the library.</returns>
  76. ;;; <remarks>
  77. ;;; The returned version is delimited by periods and has 3 different numbers indicating the version: a <i>major</i>, a <i>minor</i> and a <i>revision</i> number.
  78. ;;; For example 1.2.3 means that major version is 1, minor 2 and the revision number is 3.
  79. ;;; </remarks>
  80. ;;; <example>
  81. ;;; ; Displays the DLL version to an active window
  82. ;;; //echo -a DLL Version: $mysql_version
  83. ;;;
  84. ;;; ; Example output:
  85. ;;; ; DLL Version: 1.0.0
  86. ;;; </example>
  87. ;;; <seealso>mysql_get_client_info</seealso>
  88. ;;; <seealso>mysql_get_server_info</seealso>
  89. ;;; <seealso>mysql_get_host_info</seealso>
  90. ;;; <seealso>mysql_get_proto_info</seealso>
  91. alias mysql_version {
  92.   return $dll($mysql_dll, mmysql_version,)
  93. }
  94.  
  95. ;;; <summary>Add single quotes around text.</summary>
  96. ;;; <syntax>$mysql_qt ( text )</syntax>
  97. ;;; <param name="text">The text to be single quoted.</param>
  98. ;;; <returns>The single quoted text.</returns>
  99. ;;; <remarks>
  100. ;;; This is an auxiliary identifier that can be used to quote data prior to using them in queries.
  101. ;;; </remarks>
  102. ;;; <example>
  103. ;;; ; Escape data, and then add quotes around it
  104. ;;; %data = $mysql_qt($mysql_real_escape_string(%db, %data))
  105. ;;; ; Execute a query
  106. ;;; mysql_exec %db INSERT INTO table (data) VALUES ( %data )
  107. ;;; </example>
  108. ;;; <seealso>mysql_escape_string</seealso>
  109. ;;; <seealso>mysql_real_escape_string</seealso>
  110. alias mysql_qt {
  111.   return $+(',$1-,')
  112. }
  113.  
  114. ;;; <summary>Opens a MySQL database.</summary>
  115. ;;; <syntax>$mysql_connect ( host, user [, pass [, db ] ] )</syntax>
  116. ;;; <param name="host">The host to connect to. Can be either an IP address or a hostname.</param>
  117. ;;; <param name="user">The username to connect with.</param>
  118. ;;; <param name="pass">Optional. The password to connect with.</param>
  119. ;;; <param name="db">Optional. The database to select after connecting.</param>
  120. ;;; <returns>A positive, numeric connection identifier if successful, or <b>$null</b> if there was an error.</returns>
  121. ;;; <remarks>
  122. ;;; The <i>db</i> argument is optional, if it isn't specified, a transient database is opened in a temporary file. If specified, and the file <i>db</i> doesn't exist, an empty database will be created on that file.
  123. ;;;
  124. ;;; If <i>db</i> is equal to the special keyword <b>:memory:</b> a memory database is opened instead of a file database. If <i>from</i> is specified the memory database will contain a copy of the specified database, otherwise an empty memory database is created. If file <i>from</i> doesn't exist, an empty database will be created on that file.
  125. ;;; <i>from</i> is only valid when <b>:memory:</b> is used, otherwise an error is raised.
  126. ;;;
  127. ;;; If <b>$null</b> is returned you can determine the exact reason for the error by checking the value of <b>%mysql_errstr</b>.
  128. ;;; For more information about error handling, see <a href="errors.html">Handling Errors</a>
  129. ;;; </remarks>
  130. ;;; <example>
  131. ;;; ; Connects to a database and displays the status after. Closes the db if it was opened successfully.
  132. ;;; var %db = $mysql_connect(localhust, user, pass)
  133. ;;; if (%db) {
  134. ;;;   echo -a Connection opened successfully.
  135. ;;;   mysql_close %db
  136. ;;; }
  137. ;;; else {
  138. ;;;   echo -a Error connecting: %mysql_errstr
  139. ;;; }
  140. ;;; </example>
  141. ;;; <seealso>mysql_close</seealso>
  142. alias mysql_connect {
  143.   var %params = $iif($0 >= 1, $mysql_param($1))
  144.   if ($0 >= 2) %params = %params $mysql_param($2)
  145.   if ($0 >= 3) %params = %params $mysql_param($3)
  146.   if ($0 >= 4) %params = %params $mysql_param($4)
  147.   return $dll($mysql_dll, mmysql_connect, %params)
  148. }
  149.  
  150. ;;; <summary>Closes an open MySQL database connection.</summary>
  151. ;;; <syntax>$mysql_close ( conn )</syntax>
  152. ;;; <syntax>/mysql_close conn</syntax>
  153. ;;; <param name="conn">The connection identifier.</param>
  154. ;;; <returns><b>1</b> if connection was closed successfully, or <b>$null</b> if there was an error.</returns>
  155. ;;; <remarks>It is usually ok to ignore the return value of <b>$mysql_close</b> because the only case an error is returned is when an invalid <i>conn</i> is specified.</remarks>
  156. ;;; <example>
  157. ;;; ; Opens a database and displays the status after. Closes the db if it was opened successfully.
  158. ;;; var %db = $mysql_connect(localhost, user, pass)
  159. ;;; if (%db) {
  160. ;;;   echo -a Database opened successfully.
  161. ;;;   mysql_close %db
  162. ;;; }
  163. ;;; else {
  164. ;;;   echo -a Error opening database: %mysql_errstr
  165. ;;; }
  166. ;;; </example>
  167. ;;; <seealso>mysql_open</seealso>
  168. alias mysql_close {
  169.   var %params = $iif($0 >= 1, $mysql_param($1))
  170.   return $dll($mysql_dll, mmysql_close, %params)
  171. }
  172.  
  173. ;;; <summary>Selects a database to use.</summary>
  174. ;;; <syntax>$mysql_select_db ( conn, db )</syntax>
  175. ;;; <syntax>/mysql_select_db conn db</syntax>
  176. ;;; <param name="conn">The connection identifier.</param>
  177. ;;; <param name="db">The database to select.</param>
  178. ;;; <returns><b>1</b> if connection was closed successfully, or <b>$null</b> if there was an error.</returns>
  179. ;;; <remarks>
  180. ;;; You can select the db with <a href="mysql_connect">$mysql_connect</a> as well.
  181. ;;; </remarks>
  182. ;;; <example>
  183. ;;; ; Opens a database and selects database.
  184. ;;; var %db = $mysql_connect(localhost, user, pass)
  185. ;;; mysql_select_db %db test
  186. ;;; </example>
  187. ;;; <seealso>mysql_open</seealso>
  188. alias mysql_select_db {
  189.   var %params = $iif($0 >= 1, $mysql_param($1))
  190.   if ($0 >= 2) %params = %params $mysql_param($1)
  191.   return $dll($mysql_dll, mmysql_select_db, %params)
  192. }
  193.  
  194. ;;; <summary>Pings a connection and reconnects if connection was lost.</summary>
  195. ;;; <syntax>$mysql_ping ( conn )</syntax>
  196. ;;; <syntax>/mysql_ping conn</syntax>
  197. ;;; <param name="conn">The connection identifier.</param>
  198. ;;; <returns><b>1</b> if connection is working, or <b>$null</b> if there was an error.</returns>
  199. ;;; <example>
  200. ;;; ; Pings a database
  201. ;;; mysql_ping %db
  202. ;;; </example>
  203. ;;; <seealso>mysql_open</seealso>
  204. ;;; <seealso>mysql_close</seealso>
  205. alias mysql_ping {
  206.   var %params = $iif($0 >= 1, $mysql_param($1))
  207.   return $dll($mysql_dll, mmysql_ping, %params)
  208. }
  209.  
  210. ;;; <summary>Pings a connection and reconnects if connection was lost.</summary>
  211. ;;; <syntax>$mysql_set_charset ( conn, charset )</syntax>
  212. ;;; <syntax>/mysql_set_charset conn charset</syntax>
  213. ;;; <param name="conn">The connection identifier.</param>
  214. ;;; <param name="charset">The character set to use.</param>
  215. ;;; <returns><b>1</b> if connection is working, or <b>$null</b> if there was an error.</returns>
  216. ;;; <remarks>
  217. ;;; The specified <i>charset</i> must be a valid character set. For list of supported character sets check MySQL documentation.
  218. ;;; </remarks>
  219. ;;; <example>
  220. ;;; ; Selects latin1 as charset
  221. ;;; mysql_set_character_set %db latin1
  222. ;;; </example>
  223. ;;; <seealso>mysql_client_encoding</seealso>
  224. alias mysql_set_charset {
  225.   var %params = $iif($0 >= 1, $mysql_param($1))
  226.   if ($0 >= 2) %params = %params $mysql_param($1)
  227.   return $dll($mysql_dll, mmysql_set_charset, %params)
  228. }
  229.  
  230. ;;; <summary>Turns on or off autocommit mode, or returns its current state.</summary>
  231. ;;; <syntax>$mysql_autocommit ( conn [, mode ] )</syntax>
  232. ;;; <syntax>/mysql_autocommit conn mode</syntax>
  233. ;;; <param name="conn">The connection identifier.</param>
  234. ;;; <param name="mode"><b>1</b>1 to enable autocommit mode, <b>0</b> to disable.</param>
  235. ;;; <returns>If setting the autocommit mode, <b>1</b> on success, or <b>$null</b> if there was an error. If getting the autocommit mode, <b>1</b> if autocommit mode is enabled, otherwise <b>0</b>.</returns>
  236. ;;; <remarks>
  237. ;;; When auto-commit mode is enabled every SQL statement is automatically committed after they're executed, unless <i>$mysql_begin</i> or <i>BEGIN</i> statement is explicitly used to start a transaction. When disabled, changes to the database are deferred and only committed when <i>$mysql_commit</i> or <i>COMMIT</i> statement is used.
  238. ;;;
  239. ;;; Using transactions when doing a batch of updates on database can greatly improve the performance. Disabling auto-commit mode means that you don't have to worry about remembering to start the transaction all the time, all you need to worry about is where you want all the pending changes to be committed.
  240. ;;;
  241. ;;; By default auto-commit is enabled by default for new database connections.
  242. ;;;
  243. ;;; Note: Transactions are only supported for transactional storage engines such as InnoDB. MyISAM is not supported.
  244. ;;; </remarks>
  245. ;;; <example>
  246. ;;; ; Disable auto-commit
  247. ;;; mysql_autocommit %db 0
  248. ;;;
  249. ;;; ; Insert a row
  250. ;;; mysql_exec %db INSERT INTO test VALUES ('First row')
  251. ;;;
  252. ;;; ; The previous statement didn't get inserted yet because it hasn't been committed automatically
  253. ;;; mysql_exec %db INSERT INTO test VALUES ('Second row')
  254. ;;;
  255. ;;; ; Now commit both inserts at the same time
  256. ;;; mysql_commit %db
  257. ;;; </example>
  258. ;;; <seealso>mysql_begin</seealso>
  259. ;;; <seealso>mysql_commit</seealso>
  260. ;;; <seealso>mysql_rollback</seealso>
  261. alias mysql_autocommit {
  262.   var %params = $iif($0 >= 1, $mysql_param($1))
  263.   if ($0 >= 2) %params = %params $mysql_param($1)
  264.   return $dll($mysql_dll, mmysql_autocommit, %params)
  265. }
  266.  
  267. ;;; <summary>Returns client version.</summary>
  268. ;;; <syntax>$mysql_get_client_info</syntax>
  269. ;;; <returns>Client version.</returns>
  270. ;;; <seealso>mysql_version</seealso>
  271. ;;; <seealso>mysql_get_server_info</seealso>
  272. ;;; <seealso>mysql_get_host_info</seealso>
  273. ;;; <seealso>mysql_get_proto_info</seealso>
  274. alias mysql_get_client_info {
  275.   var %params = $iif($0 >= 1, $mysql_param($1))
  276.   return $dll($mysql_dll, mmysql_get_client_info, %params)
  277. }
  278.  
  279. ;;; <summary>Returns host info for connection.</summary>
  280. ;;; <syntax>$mysql_get_host_info ( conn ) </syntax>
  281. ;;; <param name="conn">The connection identifier.</param>
  282. ;;; <returns>Host info.</returns>
  283. ;;; <seealso>mysql_version</seealso>
  284. ;;; <seealso>mysql_get_client_info</seealso>
  285. ;;; <seealso>mysql_get_server_info</seealso>
  286. ;;; <seealso>mysql_get_proto_info</seealso>
  287. alias mysql_get_host_info {
  288.   var %params = $iif($0 >= 1, $mysql_param($1))
  289.   return $dll($mysql_dll, mmysql_get_host_info, %params)
  290. }
  291.  
  292. ;;; <summary>Returns protocol version for connection.</summary>
  293. ;;; <syntax>$mysql_get_proto_info ( conn ) </syntax>
  294. ;;; <param name="conn">The connection identifier.</param>
  295. ;;; <returns>Protocol version.</returns>
  296. ;;; <seealso>mysql_version</seealso>
  297. ;;; <seealso>mysql_get_client_info</seealso>
  298. ;;; <seealso>mysql_get_host_info</seealso>
  299. ;;; <seealso>mysql_get_server_info</seealso>
  300. alias mysql_get_proto_info {
  301.   var %params = $iif($0 >= 1, $mysql_param($1))
  302.   return $dll($mysql_dll, mmysql_get_proto_info, %params)
  303. }
  304.  
  305. ;;; <summary>Returns server version for connection.</summary>
  306. ;;; <syntax>$mysql_get_server_info ( conn ) </syntax>
  307. ;;; <param name="conn">The connection identifier.</param>
  308. ;;; <returns>Server version.</returns>
  309. ;;; <seealso>mysql_version</seealso>
  310. ;;; <seealso>mysql_get_client_info</seealso>
  311. ;;; <seealso>mysql_get_host_info</seealso>
  312. ;;; <seealso>mysql_get_proto_info</seealso>
  313. alias mysql_get_server_info {
  314.   var %params = $iif($0 >= 1, $mysql_param($1))
  315.   return $dll($mysql_dll, mmysql_get_server_info, %params)
  316. }
  317.  
  318. ;;; <summary>Returns the client character set.</summary>
  319. ;;; <syntax>$mysql_client_encoding ( conn)</syntax>
  320. ;;; <syntax>/mysql_client_encoding conn</syntax>
  321. ;;; <param name="conn">The connection identifier.</param>
  322. ;;; <returns>The character set client uses.</returns>
  323. ;;; <example>
  324. ;;; echo -a Client encoding: $mysql_client_encoding(%db)
  325. ;;; </example>
  326. ;;; <seealso>mysql_set_charset</seealso>
  327. alias mysql_client_encoding {
  328.   var %params = $iif($0 >= 1, $mysql_param($1))
  329.   return $dll($mysql_dll, mmysql_client_encoding, %params)
  330. }
  331.  
  332. ;;; <summary>Escapes a string for use as a query parameter.</summary>
  333. ;;; <syntax>$mysql_escape_string ( string )</syntax>
  334. ;;; <param name="string">The string to escape.</param>
  335. ;;; <returns>Escaped string.</returns>
  336. ;;; <remarks>
  337. ;;; $mysql_escape_string escapes the specific <i>string</i> so that it can be used safely in queries.
  338. ;;;
  339. ;;; It is often more desireable to use <a href="mysql_real_escape_string.html">$mysql_real_escape_string</a>, as it takes the connection encoding into accout as well.
  340. ;;; </remarks>
  341. ;;; <example>
  342. ;;; var %str = $?="Input a string:"
  343. ;;; var %sql = INSERT INTO table (value) VALUES (' $+ $mysql_escape_string(%str) $+ ')
  344. ;;; ; %sql can now be safely executed
  345. ;;; </example>
  346. ;;; <seealso>mysql_real_escape_string</seealso>
  347. ;;; <seealso>mysql_query</seealso>
  348. ;;; <seealso>mysql_exec</seealso>
  349. ;;; <seealso>mysql_qt</seealso>
  350. alias mysql_escape_string {
  351.   var %params = $iif($0 >= 1, $mysql_param($1))
  352.   return $dll($mysql_dll, mmysql_escape_string, %params)
  353. }
  354.  
  355. ;;; <summary>Escapes a string for use as a query parameter.</summary>
  356. ;;; <syntax>$mysql_real_escape_string ( conn, string )</syntax>
  357. ;;; <param name="conn">The connection identifier.</param>
  358. ;;; <param name="string">The string to escape.</param>
  359. ;;; <returns>Escaped string or <b>$null</b> on error.</returns>
  360. ;;; <remarks>
  361. ;;; $mysql_real_escape_string escapes the specific <i>string</i> so that it can be used safely in queries.
  362. ;;;
  363. ;;; If <b>$null</b> is returned, it can mean two things: the <i>string</i> parameter was empty so nothing was escaped, or there was an error. To determine which was the case, you can check <b>%mysql_errno</b> variable. If it's <b>$MYSQL_ERROR_OK</b>, there were no errors.
  364. ;;;
  365. ;;; It is usually ok to ignore if the return value is <b>$null</b> because the only case an error is returned is when an invalid <i>conn</i> is specified.
  366. ;;; </remarks>
  367. ;;; <example>
  368. ;;; var %str = $?="Input a string:"
  369. ;;; var %sql = INSERT INTO table (value) VALUES (' $+ $mysql_escape_string(%str) $+ ')
  370. ;;; ; %sql can now be safely executed
  371. ;;; </example>
  372. ;;; <seealso>mysql_real_escape_string</seealso>
  373. ;;; <seealso>mysql_query</seealso>
  374. ;;; <seealso>mysql_exec</seealso>
  375. ;;; <seealso>mysql_qt</seealso>
  376. alias mysql_real_escape_string {
  377.   var %params = $iif($0 >= 1, $mysql_param($1))
  378.   if ($0 >= 2) %params = %params $mysql_param($2)
  379.   return $dll($mysql_dll, mmysql_real_escape_string, %params)
  380. }
  381.  
  382. ;;; <summary>Executes a SQL query and returns data returned by it.</summary>
  383. ;;; <syntax>$mysql_query ( conn, query [, bind_value [, ... ] ] ) [ .file ]</syntax>
  384. ;;; <syntax>/mysql_query conn query</syntax>
  385. ;;; <syntax>$mysql_query ( statement [, bind_value [, ... ] ] )</syntax>
  386. ;;; <syntax>/mysql_query statement [ bind_value [ ... ] ]</syntax>
  387. ;;; <param name="conn">The connection identifier.</param>
  388. ;;; <param name="query">The query to execute.</param>
  389. ;;; <param name="statement">A prepared statement to execute.</param>
  390. ;;; <param name="bind_value">Optional. One or more values to bind to the query.</param>
  391. ;;; <prop name="file">Optional. If specified the query parameter is treated as a filename instead, and that file will be executed as SQL.</prop>
  392. ;;; <returns>A positive, numeric result identifier or <b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  393. ;;; <remarks>
  394. ;;; To execute a prepared statement first prepare it with <a href="mysql_prepare.html">$mysql_prepare</a>.
  395. ;;; To learn about prepared statements and binding values, see <a href="prepared.html">Prepared Statements</a>.
  396. ;;;
  397. ;;; If <b>$mysql_query</b> was used to execute a query that doesn't return any data, such as INSERT or UPDATE it'll return <b>1</b> on success.
  398. ;;; A SELECT query always returns a result identifier on success, even if the query selected no rows. You can use <b>$mysql_num_rows</b> to determine how many rows were returned.
  399. ;;;
  400. ;;; If <b>$null</b> is returned you can determine the exact reason for the error by checking the value of <b>%mysql_errstr</b>.
  401. ;;; For more information about error handling, see <a href="errors.html">Handling Errors</a>
  402. ;;;
  403. ;;; For queries that aren't prepared statements, you can only bind values by using the identifier form of <b>$mysql_query</b>. If you wish to ignore the return value you can use <i>/noop</i> with it.
  404. ;;;
  405. ;;; If you want to bind a text value with more than one word, you must use the identifier form of syntax.
  406. ;;;
  407. ;;; Executing a query also sets a <b>%mysql_sqlstate</b> variable to a value indicaating the SQL state. For a list of SQL states consult to MySQL documentation.
  408. ;;;
  409. ;;; <b>$mysql_query</b> can execute multiple queries seperated by semicolons. The returned result is the data returned by the last SQL query.
  410. ;;; To see guidelines for writing SQL queries with mIRC MySQL, see <a href="queries.html">Writing Queries</a>.
  411. ;;; </remarks>
  412. ;;; <example>
  413. ;;; ; Selects data from a table and fetches it
  414. ;;; var %sql = SELECT col, another FROM table
  415. ;;; var %request = $mysql_query(%db, %sql)
  416. ;;; if (%request) {
  417. ;;;   echo -a Query executed successfully.
  418. ;;;   mysql_free %request
  419. ;;; }
  420. ;;; else {
  421. ;;;   echo -a Error executing query: %mysql_errstr
  422. ;;; }
  423. ;;; </example>
  424. ;;; <seealso href="queries.html">Writing Queries</seealso>
  425. ;;; <seealso href="binary.html">Handling Binary Data</seealso>
  426. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  427. ;;; <seealso>mysql_unbuffered_query</seealso>
  428. ;;; <seealso>mysql_exec</seealso>
  429. ;;; <seealso>mysql_fetch_row</seealso>
  430. ;;; <seealso>mysql_fetch_bound</seealso>
  431. ;;; <seealso>mysql_fetch_single</seealso>
  432. ;;; <seealso>mysql_num_rows</seealso>
  433. ;;; <seealso>mysql_free</seealso>
  434. alias mysql_query {
  435.   var %params
  436.   if ($0 >= 1) {
  437.     %params = $mysql_param($1) 1
  438.     if (!$mysql_is_valid_statement($1)) {
  439.       if ($isid) {
  440.         %params = %params $iif($prop == file, 1, 0)
  441.       }
  442.       else {
  443.         var %query, %i = 2
  444.         while (%i <= $0) {
  445.           %query = %query $ [ $+ [ %i ] ]
  446.           inc %i
  447.         }
  448.         %params = %params $mysql_param(%query)
  449.       }
  450.     }
  451.     if ($isid) {
  452.       var %i = 2
  453.       while (%i <= $0) {
  454.         %params = %params $mysql_param($ [ $+ [ %i ] ])
  455.         inc %i
  456.       }
  457.     }      
  458.   }
  459.   return $dll($mysql_dll, mmysql_query, %params)
  460. }
  461.  
  462. ;;; <summary>Executes a SQL query and returns data returned by it.</summary>
  463. ;;; <syntax>$mysql_unbuffered_query ( conn, query [, bind_value [, ... ] ] ) [ .file ]</syntax>
  464. ;;; <syntax>/mysql_unbuffered_query conn query</syntax>
  465. ;;; <syntax>$mysql_unbuffered_query ( statement [, bind_value [, ... ] ] )</syntax>
  466. ;;; <syntax>/mysql_unbuffered_query statement [ bind_value [ ... ] ]</syntax>
  467. ;;; <param name="conn">The connection identifier.</param>
  468. ;;; <param name="query">The query to execute.</param>
  469. ;;; <param name="statement">A prepared statement to execute.</param>
  470. ;;; <param name="bind_value">Optional. One or more values to bind to the query.</param>
  471. ;;; <prop name="file">Optional. If specified the query parameter is treated as a filename instead, and that file will be executed as SQL.</prop>
  472. ;;; <returns>A positive, numeric result identifier or <b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  473. ;;; <remarks>
  474. ;;; Unbuffered queries work like regular queries, except that they produce a result set that isn't buffered in memory. Since the rows aren't buffered in memory, unbuffered queries are the optimal way to handle large set of sequental data because they're more efficient and the memory footprint is much smaller.
  475. ;;; The trade off is, since there's no way to know the number of rows before they're fetched, that you can't use certain functions such as <a href="mysql_num_rows.html">mysql_num_rows</a>, <a href="mysql_result.html">mysql_result</a> and <a href="mysql_data_seek.html">mysql_data_seek</a>. You also need to fetch all rows from the unbuffered result before sending new SQL statement to the server, or problems can arise.
  476. ;;;
  477. ;;; Other than that <b>$mysql_unbuffered_query</b> acts exactly the same as <a href="mysql_query.html">$mysql_query</a>, which you can check for more information and example.
  478. ;;; </remarks>
  479. ;;; <seealso href="queries.html">Writing Queries</seealso>
  480. ;;; <seealso href="binary.html">Handling Binary Data</seealso>
  481. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  482. ;;; <seealso>mysql_query</seealso>
  483. ;;; <seealso>mysql_exec</seealso>
  484. ;;; <seealso>mysql_fetch_row</seealso>
  485. ;;; <seealso>mysql_fetch_bound</seealso>
  486. ;;; <seealso>mysql_fetch_single</seealso>
  487. ;;; <seealso>mysql_num_rows</seealso>
  488. ;;; <seealso>mysql_free</seealso>
  489. alias mysql_unbuffered_query {
  490.   var %params
  491.   if ($0 >= 1) {
  492.     %params = $mysql_param($1) 2
  493.     if (!$mysql_is_valid_statement($1)) {
  494.       if ($isid) {
  495.         %params = %params $iif($prop == file, 1, 0)
  496.       }
  497.       else {
  498.         var %query, %i = 2
  499.         while (%i <= $0) {
  500.           %query = %query $ [ $+ [ %i ] ]
  501.           inc %i
  502.         }
  503.         %params = %params $mysql_param(%query)
  504.       }
  505.     }
  506.     if ($isid) {
  507.       var %i = 2
  508.       while (%i <= $0) {
  509.         %params = %params $mysql_param($ [ $+ [ %i ] ])
  510.         inc %i
  511.       }
  512.     }      
  513.   }
  514.   return $dll($mysql_dll, mmysql_query, %params)
  515. }
  516.  
  517. ;;; <summary>Executes a result-less SQL query.</summary>
  518. ;;; <syntax>$mysql_exec ( conn, query [, bind_value [, ... ] ] ) [ .file ]</syntax>
  519. ;;; <syntax>/mysql_exec conn query</syntax>
  520. ;;; <syntax>$mysql_exec ( statement [, bind_value [, ... ] ] )</syntax>
  521. ;;; <syntax>/mysql_exec statement [ bind_value [ ... ] ]</syntax>
  522. ;;; <param name="conn">The connection identifier.</param>
  523. ;;; <param name="query">The query to execute.</param>
  524. ;;; <param name="statement">A prepared statement to execute.</param>
  525. ;;; <param name="bind_value">Optional. One or more values to bind to the query.</param>
  526. ;;; <prop name="file">Optional. If specified the query parameter is treated as a filename instead, and that file will be executed as SQL.</prop>
  527. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  528. ;;; <remarks>
  529. ;;; <b>$mysql_exec</b> acts exactly the same as <a href="mysql_query.html">$mysql_query</a>, except it never returns a result, even for SELECT statements. Check it for more information
  530. ;;; </remarks>
  531. ;;; <example>
  532. ;;; ; Inserts data to a table
  533. ;;; var %sql = INSERT INTO table (key, value) VALUES ('version', '1.0.0')
  534. ;;; if ($mysql_exec(%db, %sql)) {
  535. ;;;   echo -a Query executed succesfully.
  536. ;;; }
  537. ;;; else {
  538. ;;;   echo -a Error executing query: %mysql_errstr
  539. ;;; }
  540. ;;; </example>
  541. ;;; <seealso href="queries.html">Writing Queries</seealso>
  542. ;;; <seealso href="binary.html">Handling Binary Data</seealso>
  543. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  544. ;;; <seealso>mysql_query</seealso>
  545. ;;; <seealso>mysql_unbuffered_query</seealso>
  546. alias mysql_exec {
  547.   var %params
  548.   if ($0 >= 1) {
  549.     %params = $mysql_param($1) 3
  550.     if (!$mysql_is_valid_statement($1)) {
  551.       if ($isid) {
  552.         %params = %params $iif($prop == file, 1, 0)
  553.       }
  554.       else {
  555.         var %query, %i = 2
  556.         while (%i <= $0) {
  557.           %query = %query $ [ $+ [ %i ] ]
  558.           inc %i
  559.         }
  560.         %params = %params $mysql_param(%query)
  561.       }
  562.     }
  563.     if ($isid) {
  564.       var %i = 2
  565.       while (%i <= $0) {
  566.         %params = %params $mysql_param($ [ $+ [ %i ] ])
  567.         inc %i
  568.       }
  569.     }      
  570.   }
  571.   return $dll($mysql_dll, mmysql_query, %params)
  572. }
  573.  
  574. ;;; <summary>Executes a result-less SQL query from a file.</summary>
  575. ;;; <syntax>$mysql_exec_file ( conn, file [, bind_value [, ... ] ] )</syntax>
  576. ;;; <syntax>/mysql_exec_file conn file</syntax>
  577. ;;; <param name="conn">The connection identifier.</param>
  578. ;;; <param name="file">The file to execute.</param>
  579. ;;; <param name="bind_value">Optional. One or more values to bind to the query.</param>
  580. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  581. ;;; <remarks>
  582. ;;; This is an alias for <a href="mysql_exec.html">$mysql_exec(...).file</a>
  583. ;;;
  584. ;;; This command is useful for executing a long query or multiple queries.
  585. ;;; One common use is executing an initialization query file after loading a script.
  586. ;;; </remarks>
  587. ;;; <example>
  588. ;;; ; A possible LOAD event for a script
  589. ;;; on *:LOAD:{
  590. ;;;   var %db = $mysql_connect(localhost, user, pass)
  591. ;;;   mysql_exec_file %db init.sql
  592. ;;;   mysql_close %db
  593. ;;; }
  594. ;;; </example>
  595. ;;; <seealso href="queries.html">Writing Queries</seealso>
  596. ;;; <seealso href="binary.html">Handling Binary Data</seealso>
  597. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  598. ;;; <seealso>mysql_exec</seealso>
  599. alias mysql_exec_file {
  600.   if ($isid) {
  601.     var %params, %i = 1
  602.     while (%i <= $0) {
  603.       %params = $+(%params,$iif(%params,$chr(44)),$ $+ %i)
  604.       inc %i
  605.     }
  606.     var %cmd = $!mysql_exec( $+ %params $+ ).file
  607.     return [ [ %cmd ] ]
  608.   }
  609.   return $mysql_exec($1, $2).file
  610. }
  611.  
  612. ;;; <summary>Frees a query result or prepared statement.</summary>
  613. ;;; <syntax>$mysql_free ( result )</syntax>
  614. ;;; <syntax>/mysql_free result</syntax>
  615. ;;; <syntax>$mysql_free ( statement )</syntax>
  616. ;;; <syntax>/mysql_free statement</syntax>
  617. ;;; <param name="result">The result identifier.</param>
  618. ;;; <param name="statement">The statement identifier.</param>
  619. ;;; <returns><b>1</b> if the result was freed successfully, or <b>$null</b> if there was an error.</returns>
  620. ;;; <remarks>It is usually ok to ignore the return value of <b>$mysql_free </b>because the only case an error is returned is when an invalid <i>result</i> is specified.</remarks>
  621. ;;; <example>
  622. ;;; ; Selects data from a table and then frees it (unpractical, only shows usage)
  623. ;;; var %sql = SELECT * FROM table
  624. ;;; var %request = $mysql_query(%db, %sql)
  625. ;;; if (%request) {
  626. ;;;   echo -a Query executed succesfully. Freeing data.
  627. ;;;   mysql_free %request
  628. ;;; }
  629. ;;; else {
  630. ;;;   echo -a Error executing query: %mysql_errstr
  631. ;;; }
  632. ;;; </example>
  633. ;;; <seealso>mysql_query</seealso>
  634. ;;; <seealso>mysql_free_result</seealso>
  635. alias mysql_free {
  636.   var %params = $iif($0 >= 1, $mysql_param($1))
  637.   return $dll($mysql_dll, mmysql_free, %params)
  638. }
  639.  
  640. ;;; <summary>Frees a query result or prepared statement.</summary>
  641. ;;; <syntax>$mysql_free_result ( result )</syntax>
  642. ;;; <syntax>/mysql_free_result result</syntax>
  643. ;;; <syntax>$mysql_free_result ( statement )</syntax>
  644. ;;; <syntax>/mysql_free_result statement</syntax>
  645. ;;; <param name="result">The result identifier.</param>
  646. ;;; <param name="statement">The statement identifier.</param>
  647. ;;; <returns><b>1</b> if the result was freed successfully, or <b>$null</b> if there was an error.</returns>
  648. ;;; <remarks>This is an alias for <a href="mysql_free.html">$mysql_free</a> provided for convience for those, who prefer to use finalize when freeing prepared statements.</remarks>
  649. ;;; <seealso>mysql_query</seealso>
  650. ;;; <seealso>mysql_free</seealso>
  651. alias mysql_free_result {
  652.   return $mysql_free($1)
  653. }
  654.  
  655. ;;; <summary>Returns a number of rows in a result.</summary>
  656. ;;; <syntax>$mysql_num_rows ( result )</syntax>
  657. ;;; <param name="result">The result identifier.</param>
  658. ;;; <returns>The number of rows in the result on success, or <b>$null</b> if there was an error.</returns>
  659. ;;; <example>
  660. ;;; ; This is not a practical example. In real application it'd be much better to SELECT COUNT(*) to get number of rows
  661. ;;; var %sql = SELECT * FROM table
  662. ;;; var %res = $mysql_query(%db, %sql)
  663. ;;; if (%res) {
  664. ;;;   echo -a Number of rows returned: $mysql_num_rows(%res)
  665. ;;;   mysql_free %res
  666. ;;; }
  667. ;;; else {
  668. ;;;   echo -a Error executing query: %mysql_errstr
  669. ;;; }
  670. ;;; </example>
  671. ;;; <seealso>mysql_query</seealso>
  672. ;;; <seealso>mysql_num_fields</seealso>
  673. alias mysql_num_rows {
  674.   var %params = $iif($0 >= 1, $mysql_param($1))
  675.   return $dll($mysql_dll, mmysql_num_rows, %params)
  676. }
  677.  
  678. ;;; <summary>Returns a number of fields in a result.</summary>
  679. ;;; <syntax>$mysql_num_fields ( result )</syntax>
  680. ;;; <param name="result">The result identifier.</param>
  681. ;;; <returns>The number of fields in the result on success, or <b>$null</b> if there was an error.</returns>
  682. ;;; <example>
  683. ;;; var %sql = SELECT * FROM table
  684. ;;; var %res = $mysql_query(%db, %sql)
  685. ;;; if (%res) {
  686. ;;;   echo -a Number of fields returned: $mysql_num_fields(%res)
  687. ;;;   mysql_free %res
  688. ;;; }
  689. ;;; else {
  690. ;;;   echo -a Error executing query: %mysql_errstr
  691. ;;; }
  692. ;;; </example>
  693. ;;; <seealso>mysql_query</seealso>
  694. ;;; <seealso>mysql_num_rows</seealso>
  695. alias mysql_num_fields {
  696.   var %params = $iif($0 >= 1, $mysql_param($1))
  697.   return $dll($mysql_dll, mmysql_num_fields , %params)
  698. }
  699.  
  700. ;;; <summary>Returns a number of affected rows of the last INSERT, UPDATE or DELETE query.</summary>
  701. ;;; <syntax>$mysql_affected_rows ( conn )</syntax>
  702. ;;; <param name="conn">The connection identifier.</param>
  703. ;;; <returns>The number of affected rows on success, or <b>$null</b> if there was an error.</returns>
  704. ;;; <example>
  705. ;;; var %sql = UPDATE publishers SET publisher = 'Square Enix' WHERE publisher = 'Squaresoft'
  706. ;;; if ($mysql_exec(%db, %sql)) {
  707. ;;;   echo -a Number of rows affected: $mysql_affected_rows(%db)
  708. ;;;   mysql_free %res
  709. ;;; }
  710. ;;; else {
  711. ;;;   echo -a Error executing query: %mysql_errstr
  712. ;;; }
  713. ;;; </example>
  714. ;;; <seealso>mysql_exec</seealso>
  715. ;;; <seealso>mysql_query</seealso>
  716. alias mysql_affected_rows {
  717.   var %params = $iif($0 >= 1, $mysql_param($1))
  718.   return $dll($mysql_dll, mmysql_affected_rows, %params)
  719. }
  720.  
  721. ;;; <summary>Returns the row id of the most recently inserted row.</summary>
  722. ;;; <syntax>$mysql_insert_id ( conn )</syntax>
  723. ;;; <param name="conn">The connection identifier.</param>
  724. ;;; <returns>The row id on success, or <b>$null</b> if there was an error.</returns>
  725. ;;; <example>
  726. ;;; var %sql = INSERT INTO publishers (publisher) VALUES ('Square Enix')
  727. ;;; if ($mysql_exec(%db, %sql)) {
  728. ;;;   echo -a Inserted row id: $mysql_insert_id(%db)
  729. ;;;   mysql_free %res
  730. ;;; }
  731. ;;; else {
  732. ;;;   echo -a Error executing query: %mysql_errstr
  733. ;;; }
  734. ;;; </example>
  735. ;;; <seealso>mysql_exec</seealso>
  736. ;;; <seealso>mysql_query</seealso>
  737. alias mysql_insert_id {
  738.   var %params = $iif($0 >= 1, $mysql_param($1))
  739.   return $dll($mysql_dll, mmysql_insert_id, %params)
  740. }
  741.  
  742. ;;; <summary>Fetches the current row from a result and then advances to the next row.</summary>
  743. ;;; <syntax>$mysql_fetch_row ( result, hash_table [, result_type ] )</syntax>
  744. ;;; <param name="result">The result identifier.</param>
  745. ;;; <param name="hash_table">The name of the hash table to where to store the row data.</param>
  746. ;;; <param name="result_type">The type of the result. Optional, see remarks for more info.</param>
  747. ;;; <returns><b>1</b> on success; Otherwise <b>0</b> if there are no more rows available, or <b>$null</b> if there was an error.</returns>
  748. ;;; <remarks>
  749. ;;; <b>$mysql_fetch_row</b> fetches the next row from the <i>result</i> and stores the data in <i>hash_table</i>.
  750. ;;; If the hash table doesn't exist, it will be created; Otherwise it will be cleared before new data is stored.
  751. ;;;
  752. ;;; <i>result_type</i> specifies how the hash table is created, it can be one of the following: <b>$MYSQL_NUM</b>, <b>$MYSQL_ASSOC</b> or <b>$MYSQL_BOTH</b>. $MYSQL_BOTH is default.
  753. ;;; If $MYSQL_NUM is used, the hash table items will be field indexes, starting from index 1. If $MYSQL_ASSOC is used, the items will be field names. If $MYSQL_BOTH is used, both column indexes and names are used.
  754. ;;; In case of $MYSQL_BOTH, if some of the column names are identical to another columns' index, the index has priority and will be used as an item.
  755. ;;; </remarks>
  756. ;;; <example>
  757. ;;; var %sql = SELECT first_name, last_name FROM contacts
  758. ;;; var %res = $mysql_query(%db, %sql)
  759. ;;; if (%res) {
  760. ;;;   echo -a Fetching results...
  761. ;;;   echo -a -
  762. ;;;   while ($mysql_fetch_row(%res, row, $MYSQL_ASSOC)) {
  763. ;;;     ; If you used $MYSQL_FETCH_NUM or $MYSQL_FETCH_BOTH you could use 1 instead of first_name and 2 instead of last_name
  764. ;;;     echo -a First name: $hget(row, first_name)
  765. ;;;     echo -a Last name: $hget(row, last_name)
  766. ;;;     echo -a -
  767. ;;;   }
  768. ;;;   mysql_free %res
  769. ;;; }
  770. ;;; else {
  771. ;;;   echo -a Error executing query: %mysql_errstr
  772. ;;; }
  773. ;;; </example>
  774. ;;; <seealso>mysql_query</seealso>
  775. ;;; <seealso>mysql_fetch_single</seealso>
  776. alias mysql_fetch_row {
  777.   var %params = $iif($0 >= 1, $mysql_param($1))
  778.   if ($0 >= 2) %params = %params $mysql_param($gettok($2,1,32))
  779.   if ($0 >= 3) %params = %params $mysql_param($3)
  780.   return $dll($mysql_dll, mmysql_fetch_row, %params)
  781. }
  782.  
  783. ;;; <summary>Fetches the current row from a result and then advances to the next row.</summary>
  784. ;;; <syntax>$mysql_fetch_num ( result, hash_table )</syntax>
  785. ;;; <param name="result">The result identifier.</param>
  786. ;;; <param name="hash_table">The name of the hash table to where to store the row data.</param>
  787. ;;; <returns><b>1</b> on success; Otherwise <b>0</b> if there are no more rows available, or <b>$null</b> if there was an error.</returns>
  788. ;;; <remarks>
  789. ;;; <b>$mysql_fetch_num</b> is provided for convenience. All it does is call <a href="mysql_fetch_row.html">$mysql_fetch_row</a> with <i>result_type</i> set to <b>$MYSQL_NUM</b>
  790. ;;; </remarks>
  791. ;;; <seealso>mysql_fetch_row</seealso>
  792. ;;; <seealso>mysql_fetch_assoc</seealso>
  793. alias mysql_fetch_num {
  794.   return $mysql_fetch_row($1, $2, $MYSQL_NUM)
  795. }
  796.  
  797. ;;; <summary>Fetches the current row from a result and then advances to the next row.</summary>
  798. ;;; <syntax>$mysql_fetch_assoc ( result, hash_table )</syntax>
  799. ;;; <param name="result">The result identifier.</param>
  800. ;;; <param name="hash_table">The name of the hash table to where to store the row data.</param>
  801. ;;; <returns><b>1</b> on success; Otherwise <b>0</b> if there are no more rows available, or <b>$null</b> if there was an error.</returns>
  802. ;;; <remarks>
  803. ;;; <b>$mysql_fetch_assoc</b> is provided for convenience. All it does is call <a href="mysql_fetch_row.html">$mysql_fetch_row</a> with <i>result_type</i> set to <b>$MYSQL_ASSOC</b>
  804. ;;; </remarks>
  805. ;;; <seealso>mysql_fetch_row</seealso>
  806. ;;; <seealso>mysql_fetch_num</seealso>
  807. alias mysql_fetch_assoc {
  808.   return $mysql_fetch_row($1, $2, $MYSQL_ASSOC)
  809. }
  810.  
  811. ;;; <summary>Fetches the current row from a result and assigns the column values in variables and then advances to the next row.</summary>
  812. ;;; <syntax>$mysql_fetch_bound ( result [, bind_type ] )</syntax>
  813. ;;; <param name="result">The result identifier.</param>
  814. ;;; <param name="bind_type">The type of the bind. Optional, see remarks for more info.</param>
  815. ;;; <returns><b>1</b> on success; Otherwise <b>0</b> if there are no more rows available, or <b>$null</b> if there was an error.</returns>
  816. ;;; <remarks>
  817. ;;; <b>$mysql_fetch_bound</b> fetches the next row from the <i>result</i> and assigns the column data in variables specified by <a href="mysql_fetch_field.html">$mysql_fetch_field</a>.
  818. ;;;
  819. ;;; <i>bind_type</i> specifies how the values are bound, it can be one of the following: <b>$MYSQL_ALL</b> or <b>$MYSQL_BOUND</b>. $MYSQL_BOUND is default.
  820. ;;; If $MYSQL_BOUND is specified, only columns that have been bound with <a href="mysql_fetch_field.html">$mysql_fetch_field</a> are fetched in variables. If $MYSQL_ALL is specified all rows are fetched, even ones that haven't been bound explicitly with <a href="mysql_fetch_field.html">$mysql_fetch_field</a>. In this case the column names are used as variable names. Depending on whether the column type is binary or not, a regular variable or a binary variable will be used.
  821. ;;;
  822. ;;; The bound variables are set as global variables when fetched, because mIRC MySQL has no access to local variables. You should be very careful that you don't override any existing global variables, especially when $MYSQL_ALL is used!
  823. ;;; </remarks>
  824. ;;; <example>
  825. ;;; var %sql = SELECT first_name, last_name, address FROM contacts
  826. ;;; var %res = $mysql_query(%db, %sql)
  827. ;;; if (%res) {
  828. ;;;   ; The first column will be bound to %name
  829. ;;;   mysql_bind_field %result 1 name
  830. ;;;
  831. ;;;   ; The third column will be bound to %postal_address
  832. ;;;   mysql_bind_field %result address first_name
  833. ;;;
  834. ;;;   ; The second column will be bound automatically to %last_name with $MYSQL_ALL
  835. ;;;
  836. ;;;   echo -a Fetching results...
  837. ;;;   echo -a -
  838. ;;;   while ($mysql_fetch_bound(%res, $MYSQL_ALL)) {
  839. ;;;     ; If you used $MYSQL_BOUND, %last_name would not exist because it wasn't bound explicitly with mysql_bind_field
  840. ;;;     echo -a First name: %name
  841. ;;;     echo -a Last name: %last_name
  842. ;;;     echo -a Address: %postal_address
  843. ;;;     echo -a -
  844. ;;;   }
  845. ;;;   mysql_free %res
  846. ;;; }
  847. ;;; else {
  848. ;;;   echo -a Error executing query: %mysql_errstr
  849. ;;; }
  850. ;;; </example>
  851. ;;; <seealso>mysql_query</seealso>
  852. ;;; <seealso>mysql_bind_field</seealso>
  853. ;;; <seealso>mysql_fetch_row</seealso>
  854. ;;; <seealso>mysql_fetch_single</seealso>
  855. alias mysql_fetch_bound {
  856.   var %params = $iif($0 >= 1, $mysql_param($1))
  857.   if ($0 >= 2) %params = %params $mysql_param($2)
  858.   tokenize 32 $dll($mysql_dll, mmysql_fetch_bound, %params)
  859.   if ($0 == 1) {
  860.     return $1
  861.   }
  862.   if ($0 == 3) {
  863.     var %file = $1, %i = 1, %total = $numtok($2, 124), %offset = 0
  864.     while (%i <= %total) {
  865.       var %size = $gettok($2, %i, 124), %bvar = $gettok($3, %i, 124)
  866.       bread %file %offset %size %bvar
  867.       inc %offset %size
  868.       inc %i
  869.     }
  870.     return 1
  871.   }
  872.   return $null
  873. }
  874.  
  875. ;;; <summary>Fetches and returns the first column of the current row from a result and then advances to the next row.</summary>
  876. ;;; <syntax>$mysql_fetch_single ( result [, &binvar ] )</syntax>
  877. ;;; <param name="result">The result identifier.</param>
  878. ;;; <param name="binvar">The name of the binary variable to assign binary data to. Optional.</param>
  879. ;;; <returns>The value of the first column of the fetched row if <i>&binvar</i> isn't specified, otherwise the size of the binary variable on success, <b>$null</b> if there are no more rows, or if there was an error.</returns>
  880. ;;; <remarks>
  881. ;;; If <i>&binvar</i> is specified the behaviour of <b>$mysql_fetch_single</b> changes slightly. Instead of returning the first column's value, it will assign it to a binvar and return the binvar's size on success.
  882. ;;; In case the first column is not blob type, its text representation will be stored in the <i>&binvar</i> as sequential ascii values. If <i>&binvar</i> isn't set, but the first column is a blob, it will be converted to text.
  883. ;;; For more information about handling binary data in mIRC MySQL, see <a href="binary.html">Handling Binary Data</a>
  884. ;;;
  885. ;;; In case of <b>$null</b> is returned it can mean three different things:
  886. ;;; <b>1.</b> The returned value from MySQL database is NULL or an empty string.
  887. ;;; <b>2.</b> There are no more rows available.
  888. ;;; <b>3.</b> There was an error.
  889. ;;;
  890. ;;; To determine the cause of <b>$null</b>, examine the <b>%mysql_errno</b> variable after calling <b>$mysql_fetch_single</b>. The returned value can be one of the following:
  891. ;;; <b>1.</b> <b>$MYSQL_OK</b> if there was no error.
  892. ;;; <b>2.</b> <b>$MYSQL_NOMOREROWS</b> if there are no more rows available.
  893. ;;; <b>3.</b> Some other error code if there was an error.
  894. ;;; </remarks>
  895. ;;; <example>
  896. ;;; var %sql = SELECT COUNT(*) FROM contacts
  897. ;;; var %res = $mysql_query(%db, %sql)
  898. ;;; if (%res) {
  899. ;;;   echo -a Number of rows in contacts: $mysql_fetch_single(%res)
  900. ;;;   mysql_free %res
  901. ;;; }
  902. ;;; else {
  903. ;;;   echo -a Error executing query: %mysql_errstr
  904. ;;; }
  905. ;;; </example>
  906. ;;; <seealso>mysql_query</seealso>
  907. ;;; <seealso>mysql_fetch_row</seealso>
  908. ;;; <seealso>mysql_fetch_field</seealso>
  909. alias mysql_fetch_single {
  910.   if ($0 < 2) {
  911.     return $dll($mysql_dll, mmysql_fetch_single, $iif($0 >= 1, $mysql_param($1)))
  912.   }
  913.   else {
  914.     tokenize 32 $dll($mysql_dll, mmysql_fetch_single, $mysql_param($1) $mysql_param($2))
  915.     if ($0 == 3) {
  916.       bread $1 0 $2 $3
  917.       return $bvar($3, 0)
  918.     }
  919.     return $null
  920.   }
  921. }
  922.  
  923. ;;; <summary>Fetches and returns the specified column of the current row from a result and then advances to the next row.</summary>
  924. ;;; <syntax>$mysql_fetch_field ( result, field [, &binvar ] ) [ .name ]</syntax>
  925. ;;; <param name="result">The result identifier.</param>
  926. ;;; <param name="field">The field index or name. See remarks for details.</param>
  927. ;;; <param name="binvar">The name of the binary variable to assign binary data to. Optional.</param>
  928. ;;; <prop name="name">Forces field to be treated as name.</prop>
  929. ;;; <returns>The value of the specified column of the fetched row if <i>&binvar</i> isn't specified, otherwise the size of the binary variable on success, <b>$null</b> if there are no more rows, or if there was an error.</returns>
  930. ;;; <remarks>
  931. ;;; <b>$mysql_fetch_field</b> is identical to <a href="mysql_fetch_single.html">$mysql_fetch_single</a> with the only difference being that <b>$mysql_fetch_field</b> returns a value of specified column, instead of the first column.
  932. ;;;
  933. ;;; If <i>field</i> is numeric it is treated as an ordinal index for the column, first column being 1, otherwise it is treated as the column's name. You can use the <b>.name</b> property to force the field to be treated as column name even if it's a number.
  934. ;;;
  935. ;;; See <a href="mysql_fetch_single.html">$mysql_fetch_single</a> for more details.
  936. ;;; </remarks>
  937. ;;; <seealso>mysql_query</seealso>
  938. ;;; <seealso>mysql_fetch_row</seealso>
  939. ;;; <seealso>mysql_fetch_single</seealso>
  940. ;;; <seealso>mysql_result</seealso>
  941. alias mysql_fetch_field {
  942.   var %params = $iif($0 >= 1, $mysql_param($1))
  943.   if ($0 >= 2) {
  944.     var %name = $iif($2 !isnum || $prop == name, 1, 0)
  945.     %params = %params %name $mysql_param($2) 0
  946.   }
  947.   if ($0 < 3) {
  948.     return $dll($mysql_dll, mmysql_fetch_field, %params)
  949.   }
  950.   else {
  951.     %params = %params $mysql_param($3)
  952.     tokenize 32 $dll($mysql_dll, mmysql_fetch_field, %params)
  953.     if ($0 == 3) {
  954.       bread $1 0 $2 $3
  955.       return $bvar($3, 0)
  956.     }
  957.     return $null
  958.   }
  959. }
  960.  
  961. ;;; <summary>Fetches and returns the specified column of the current row from a result.</summary>
  962. ;;; <syntax>$mysql_result ( result, field [, &binvar ] ) [ .name ]</syntax>
  963. ;;; <param name="result">The result identifier.</param>
  964. ;;; <param name="field">The field index or name. See remarks for details.</param>
  965. ;;; <param name="binvar">The name of the binary variable to assign binary data to. Optional.</param>
  966. ;;; <prop name="name">Forces field to be treated as name.</prop>
  967. ;;; <returns>The value of the specified column of the fetched row if <i>&binvar</i> isn't specified, otherwise the size of the binary variable on success, <b>$null</b> if there are no more rows, or if there was an error.</returns>
  968. ;;; <remarks>
  969. ;;; <b>$mysql_result</b> is identical to <a href="mysql_fetch_field.html">$mysql_fetch_field</a> except it doesn't advance to the next row.
  970. ;;; </remarks>
  971. ;;; <seealso>mysql_fetch_field</seealso>
  972. alias mysql_result {
  973.   var %params = $iif($0 >= 1, $mysql_param($1))
  974.   if ($0 >= 2) {
  975.     var %name = $iif($2 !isnum || $prop == name, 1, 0)
  976.     %params = %params %name $mysql_param($2) 1
  977.   }
  978.   if ($0 < 3) {
  979.     return $dll($mysql_dll, mmysql_fetch_field, %params)
  980.   }
  981.   else {
  982.     %params = %params $mysql_param($3)
  983.     tokenize 32 $dll($mysql_dll, mmysql_fetch_field, %params)
  984.     if ($0 == 3) {
  985.       bread $1 0 $2 $3
  986.       return $bvar($3, 0)
  987.     }
  988.     return $null
  989.   }
  990. }
  991.  
  992. ;;; <summary>Seek to a particular row.</summary>
  993. ;;; <syntax>$mysql_data_seek ( result, row_index )</syntax>
  994. ;;; <syntax>/mysql_data_seek result row_index</syntax>
  995. ;;; <param name="result">The result identifier.</param>
  996. ;;; <param name="row_index">The row to seek to.</param>
  997. ;;; <returns><b>1</b> on success; Otherwise <b>0</b> if the row isn't seekable, or <b>$null</b> if there was an error.</returns>
  998. ;;; <seealso>mysql_query</seealso>
  999. ;;; <seealso>mysql_key</seealso>
  1000. alias mysql_data_seek {
  1001.   var %params = $iif($0 >= 1, $mysql_param($1))
  1002.   if ($0 >= 2) %params = %params $mysql_param($2)
  1003.   return $dll($mysql_dll, mmysql_data_seek, %params)
  1004. }
  1005.  
  1006. ;;; <summary>Checks if a connection is valid.</summary>
  1007. ;;; <syntax>$mysql_is_valid_connection ( conn )</syntax>
  1008. ;;; <param name="conn">The connection identifier.</param>
  1009. ;;; <returns><b>1</b> if <i>conn</i> is a valid connection, <b>0</b> if it's invalid, or <b>$null</b> if there was an error.</returns>
  1010. ;;; <remarks>
  1011. ;;; It is usually ok to ignore if <b>$mysql_is_valid_connection</b> returns <b>$null</b> because the only case an error is returned is when <i>conn</i> isn't specified.
  1012. ;;; </remarks>
  1013. ;;; <seealso>mysql_open</seealso>
  1014. ;;; <seealso>mysql_is_valid_result</seealso>
  1015. ;;; <seealso>mysql_is_valid_statement</seealso>
  1016. alias mysql_is_valid_connection {
  1017.   var %params = $iif($0 >= 1, $mysql_param($1))
  1018.   return $dll($mysql_dll, mmysql_is_valid_connection, %params)
  1019. }
  1020.  
  1021. ;;; <summary>Checks if a result is valid.</summary>
  1022. ;;; <syntax>$mysql_is_valid_result ( result )</syntax>
  1023. ;;; <param name="result">The result identifier.</param>
  1024. ;;; <returns><b>1</b> if <i>conn</i> is a valid connection, <b>0</b> if it's invalid, or <b>$null</b> if there was an error.</returns>
  1025. ;;; <remarks>
  1026. ;;; It is usually ok to ignore if <b>$mysql_is_valid_result</b> returns <b>$null</b> because the only case an error is returned is when <i>conn</i> isn't specified.
  1027. ;;; </remarks>
  1028. ;;; <seealso>mysql_query</seealso>
  1029. ;;; <seealso>mysql_unbuffered_query</seealso>
  1030. ;;; <seealso>mysql_is_valid_connection</seealso>
  1031. ;;; <seealso>mysql_is_valid_statement</seealso>
  1032. alias mysql_is_valid_result {
  1033.   var %params = $iif($0 >= 1, $mysql_param($1))
  1034.   return $dll($mysql_dll, mmysql_is_valid_result, %params)
  1035. }
  1036.  
  1037. ;;; <summary>Checks if a statement is valid.</summary>
  1038. ;;; <syntax>$mysql_is_valid_statement ( stmt )</syntax>
  1039. ;;; <param name="stmt">The statement identifier.</param>
  1040. ;;; <returns><b>1</b> if <i>conn</i> is a valid connection, <b>0</b> if it's invalid, or <b>$null</b> if there was an error.</returns>
  1041. ;;; <remarks>
  1042. ;;; It is usually ok to ignore if <b>$mysql_is_valid_statement</b> returns <b>$null</b> because the only case an error is returned is when <i>conn</i> isn't specified.
  1043. ;;; </remarks>
  1044. ;;; <seealso>mysql_prepare</seealso>
  1045. ;;; <seealso>mysql_is_valid_connection</seealso>
  1046. ;;; <seealso>mysql_is_valid_result</seealso>
  1047. alias mysql_is_valid_statement {
  1048.   var %params = $iif($0 >= 1, $mysql_param($1))
  1049.   return $dll($mysql_dll, mmysql_is_valid_statement, %params)
  1050. }
  1051.  
  1052. ;;; <summary>Begins a transaction.</summary>
  1053. ;;; <syntax>$mysql_begin ( conn )</syntax>
  1054. ;;; <syntax>/mysql_begin conn</syntax>
  1055. ;;; <param name="conn">The connection identifier.</param>
  1056. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  1057. ;;; <remarks>
  1058. ;;; <b>$mysql_begin</b> is a shorthand function provided for convience for executing <i>BEGIN</i> on <i>conn</i>.
  1059. ;;;
  1060. ;;; Transactions should be used whenever a batch of queries that modify a database are executed. Transactions are much more efficient in such cases, because otherwise every individual query would create a transaction of their own, which is an expensive operation.
  1061. ;;;
  1062. ;;; Note: Transactions are only supported for transactional storage engines such as InnoDB. MyISAM is not supported.
  1063. ;;; </remarks>
  1064. ;;; <seealso>mysql_commit</seealso>
  1065. ;;; <seealso>mysql_rollback</seealso>
  1066. ;;; <seealso>mysql_autocommit</seealso>
  1067. alias mysql_begin {
  1068.   return $mysql_exec($mysql_param($1), BEGIN)
  1069. }
  1070.  
  1071. ;;; <summary>Commits a transaction.</summary>
  1072. ;;; <syntax>$mysql_commit ( conn )</syntax>
  1073. ;;; <syntax>/mysql_commit conn</syntax>
  1074. ;;; <param name="conn">The connection identifier.</param>
  1075. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  1076. ;;; <remarks>
  1077. ;;; <b>$mysql_commit</b> is a shorthand function provided for convience for executing <i>COMMIT</i> on <i>conn</i>.
  1078. ;;;
  1079. ;;; Committing a transaction will save all the changes that were done during the transaction to the database.
  1080. ;;;
  1081. ;;; Note: Transactions are only supported for transactional storage engines such as InnoDB. MyISAM is not supported.
  1082. ;;; </remarks>
  1083. ;;; <seealso>mysql_begin</seealso>
  1084. ;;; <seealso>mysql_rollback</seealso>
  1085. ;;; <seealso>mysql_autocommit</seealso>
  1086. alias mysql_commit {
  1087.   return $mysql_exec($mysql_param($1), COMMIT)
  1088. }
  1089.  
  1090. ;;; <summary>Rolls back a transaction.</summary>
  1091. ;;; <syntax>$mysql_rollback ( conn )</syntax>
  1092. ;;; <syntax>/mysql_rollback conn</syntax>
  1093. ;;; <param name="conn">The connection identifier.</param>
  1094. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  1095. ;;; <remarks>
  1096. ;;; <b>$mysql_rollback</b> is a shorthand function provided for convience for executing <i>ROLLBACK TRANSACTION</i> on <i>conn</i>.
  1097. ;;;
  1098. ;;; Rolling back a transaction will discard all the changes that were done during the transaction.
  1099. ;;;
  1100. ;;; Note: Transactions are only supported for transactional storage engines such as InnoDB. MyISAM is not supported.
  1101. ;;; </remarks>
  1102. ;;; <seealso>mysql_begin</seealso>
  1103. ;;; <seealso>mysql_commit</seealso>
  1104. ;;; <seealso>mysql_autocommit</seealso>
  1105. alias mysql_rollback {
  1106.   return $mysql_exec($mysql_param($1), ROLLBACK)
  1107. }
  1108.  
  1109. ;;; <summary>Prepares a SQL query to be executed later.</summary>
  1110. ;;; <syntax>$mysql_prepare ( conn, query ) [ .file ]</syntax>
  1111. ;;; <param name="conn">The connection identifier.</param>
  1112. ;;; <param name="query">The query to execute.</param>
  1113. ;;; <prop name="file">Optional. If specified the query parameter is treated as a filename instead, and that file will be treated as SQL.</prop>
  1114. ;;; <returns>A positive, numeric statement identifier on success, or <b>$null</b> if there was an error.</returns>
  1115. ;;; <remarks>
  1116. ;;; Prepared queries are efficient when you need to execute the same query many times with different parameters, as you can just bind new parameters to the statement after each execution. For more information about prepared statements and parameter binding, see <a href="prepared.html">Prepared Statements</a>.
  1117. ;;; Just like ordinary queries, prepared queries are executed with <a href="mysql_exec.html">$mysql_exec</a>, <a href="mysql_query.html">$mysql_query</a> or <a href="mysql_unbuffered_query.html">$mysql_unbuffered_query</a>, see example below.
  1118. ;;;
  1119. ;;; If <b>$null</b> is returned you can determine the exact reason for the error by checking the value of <b>%mysql_errstr</b>.
  1120. ;;; For more information about error handling, see <a href="errors.html">Handling Errors</a>.
  1121. ;;;
  1122. ;;; <b>$mysql_prepare</b> can only prepare a single query. Extra queries seperated by a semi-colon are ignored, only the first one is prepared.
  1123. ;;; To see guidelines for writing SQL queries with mIRC MySQL, see <a href="queries.html">Writing Queries</a>.
  1124. ;;; </remarks>
  1125. ;;; <example>
  1126. ;;; ; Inserts data into a table two times with different parameters
  1127. ;;; var %sql = INSERT INTO table VALUES (?, :test)
  1128. ;;; var %stmt = $mysql_prepare(%db, %sql)
  1129. ;;; if (%stmt) {
  1130. ;;;   echo -a Query prepared successfully.
  1131. ;;;
  1132. ;;;   ; Binds Hello as first parameter, and World as second parameter and inserts the row
  1133. ;;;   mysql_bind_value %stmt 1 Hello
  1134. ;;;   mysql_bind_value %stmt :test World
  1135. ;;;   mysql_exec %stmt
  1136. ;;;
  1137. ;;;   ; Binds NULL as first parameter, and 100 as second parameter and inserts the row
  1138. ;;;   mysql_bind_null %stmt 1
  1139. ;;;   mysql_bind_value %stmt :test 100
  1140. ;;;   mysql_exec %stmt
  1141. ;;;
  1142. ;;;   ; Binds 'This is a test' as first parameter, and uses the previously bound parameter for second parameter
  1143. ;;;   noop $mysql_exec(%stmt, This is a test)
  1144. ;;;
  1145. ;;;   mysql_free %stmt
  1146. ;;; }
  1147. ;;; else {
  1148. ;;;   echo -a Error preparing query: %mysql_errstr
  1149. ;;; }
  1150. ;;; </example>
  1151. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  1152. ;;; <seealso>mysql_bind_field</seealso>
  1153. ;;; <seealso>mysql_bind_param</seealso>
  1154. ;;; <seealso>mysql_bind_value</seealso>
  1155. ;;; <seealso>mysql_bind_null</seealso>
  1156. ;;; <seealso>mysql_exec</seealso>
  1157. ;;; <seealso>mysql_query</seealso>
  1158. ;;; <seealso>mysql_free</seealso>
  1159. alias mysql_prepare {
  1160.   var %params = $iif($0 >= 1, $mysql_param($1))
  1161.   if ($0 >= 2) %params = %params $iif($isid && $prop == file, 1, 0) $mysql_param($2)
  1162.   return $dll($mysql_dll, mmysql_prepare, %params)
  1163. }
  1164.  
  1165. ;;; <summary>Binds a column to a variable.</summary>
  1166. ;;; <syntax>$mysql_bind_field ( result, column, var ) [ .name ]</syntax>
  1167. ;;; <syntax>/mysql_bind_field result column var</syntax>
  1168. ;;; <param name="result">The result identifier.</param>
  1169. ;;; <param name="column">The column number of name to bind for. Must exist in the result set.</param>
  1170. ;;; <param name="var">The variable or binary variable to bind the column for.</param>
  1171. ;;; <prop name="name">Forces column to be treated as name.</prop>
  1172. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  1173. ;;; <remarks>
  1174. ;;; The columns bound to variables with <b>$mysql_bind_field</b> are used when fetching rows with <a href="mysql_fetch_bound.html">$mysql_fetch_bound</a>.
  1175. ;;;
  1176. ;;; If <i>column</i> is numeric it is treated as an ordinal index for the column, first column being 1, otherwise it is treated as the column's name. You can use the <b>.name</b> property to force the field to be treated as column name even if it's a number.
  1177. ;;;
  1178. ;;; The <i>var</i> parameter is considered as a binary variable if it starts with a <b>&</b>. Otherwise it's considered as a regular variable. You should <b>not</b> prefix the var with a <b>%</b>; otherwise mIRC will evaluate the variable right away.
  1179. ;;;
  1180. ;;; The bound variables are set as global variables when fetched, because mIRC MySQL has no access to local variables. You should be very careful that you don't override any existing global variables.
  1181. ;;;
  1182. ;;; For more information about parameter binding, see <a href="prepared.html">Prepared Statements</a>.
  1183. ;;;
  1184. ;;; If you want to use the <b>.name</b> property to force the <i>column</i> to act as a column name, you must use the first form of the syntax. If you don't care about the return value, you can use the mIRC's built-in command <b>/noop</b>
  1185. ;;; </remarks>
  1186. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  1187. ;;; <seealso>mysql_bind_column</seealso>
  1188. ;;; <seealso>mysql_prepare</seealso>
  1189. ;;; <seealso>mysql_fetch_bound</seealso>
  1190. ;;; <seealso>mysql_clear_bindings</seealso>
  1191. alias mysql_bind_field {
  1192.   var %params = $iif($0 >= 1, $mysql_param($1))
  1193.   if ($0 >= 2) {
  1194.     var %name = $iif($2 !isnum || $prop == name, 1, 0)
  1195.     %params = %params %name $mysql_param($2)
  1196.   }
  1197.   if ($0 >= 3) {
  1198.     %params = %params $mysql_param($3)
  1199.   }
  1200.   return $dll($mysql_dll, mmysql_bind_field, %params)
  1201. }
  1202.  
  1203. ;;; <summary>Binds a column to a variable.</summary>
  1204. ;;; <syntax>$mysql_bind_column ( result, column, var ) [ .name ]</syntax>
  1205. ;;; <syntax>/mysql_bind_column result column var</syntax>
  1206. ;;; <param name="result">The result identifier.</param>
  1207. ;;; <param name="column">The column number of name to bind for. Must exist in the result set.</param>
  1208. ;;; <param name="var">The variable or binary variable to bind the column for.</param>
  1209. ;;; <param name="datatype">Optional. Tells what datatype column is. See remarks.</param>
  1210. ;;; <prop name="name">Forces column to be treated as name.</prop>
  1211. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  1212. ;;; <remarks>This is an alias for <a href="mysql_bind_field.html">$mysql_bind_field</a> provided for convience.</remarks>
  1213. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  1214. ;;; <seealso>mysql_bind_field</seealso>
  1215. ;;; <seealso>mysql_prepare</seealso>
  1216. ;;; <seealso>mysql_fetch_bound</seealso>
  1217. alias mysql_bind_column {
  1218.   return $mysql_bind_field($1, $2, $3)
  1219. }
  1220.  
  1221. ;;; <summary>Binds a variable as a parameter for prepared statement.</summary>
  1222. ;;; <syntax>$mysql_bind_param ( statement, param, var )</syntax>
  1223. ;;; <syntax>/mysql_bind_param statement param var</syntax>
  1224. ;;; <param name="statement">The prepared statement identifier.</param>
  1225. ;;; <param name="param">The parameter to bind to. Must exist in the prepared query.</param>
  1226. ;;; <param name="var">The variable or binary variable to bind to.</param>
  1227. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  1228. ;;; <remarks>
  1229. ;;; <b>$mysql_bind_param</b> can be used to bind a variable to a parameter. The variable is bound as a reference and is evaluated at the time of execution. This means that by changing the variable in mIRC, you're effectively changing the bound value as well.
  1230. ;;;
  1231. ;;; The <i>param</i> parameter can either be a numerical index, specified with a <b>?</b> in the query, or a named parameter specified with a <b>:name</b> in the query. If binding a named parameter, you should also include the colon in the name.
  1232. ;;;
  1233. ;;; The <i>var</i> parameter is considered as a binary variable if it starts with a <b>&</b>. Otherwise it's considered as a regular variable. You should <b>not</b> prefix the var with a <b>%</b>; otherwise mIRC will evaluate the variable right away. If not a binary variable, the specified variable must be a global variable because local variables only exist in scope of the alias they're declared in; mIRC MySQL has no access to them. See example below.
  1234. ;;;
  1235. ;;; For more information about parameter binding, see <a href="prepared.html">Prepared Statements</a>.
  1236. ;;; </remarks>
  1237. ;;; <example>
  1238. ;;; ; Binds one numerical and one named parameter two times
  1239. ;;; var %sql = SELECT ?, :test
  1240. ;;; var %stmt = $mysql_prepare(%db, %sql)
  1241. ;;; if (%stmt) {
  1242. ;;;   ; Binds %first as first parameter, and &second as second parameter.
  1243. ;;;   ; Do not prefix the variable with a % or mIRC will evaluate the variable beforehand.
  1244. ;;;   mysql_bind_param %stmt 1 first
  1245. ;;;   mysql_bind_param %stmt :test &second
  1246. ;;;
  1247. ;;;   ; We can define the variables after they're bound because they aren't evaluated before the query is executed.
  1248. ;;;   set %first Hello
  1249. ;;;   bset -t &second 1 World
  1250. ;;;
  1251. ;;;   ; Execute the query and show the results
  1252. ;;;   var %result = $mysql_query(%stmt)
  1253. ;;;   if ($mysql_fetch_row(%result, row, $MYSQL_NUM)) {
  1254. ;;;     echo -a First execution:
  1255. ;;;     echo -a 1st: $hget(row, 1)
  1256. ;;;     echo -a 2nd: $hget(row, 2)
  1257. ;;;   }
  1258. ;;;   mysql_free %result
  1259. ;;;
  1260. ;;;   ; Change the first parameter to something else, you don't need to call mysql_bind_param again!
  1261. ;;;   set %first Another
  1262. ;;;
  1263. ;;;   ; Execute the query again and show the new results
  1264. ;;;   var %result = $mysql_query(%stmt)
  1265. ;;;   if ($mysql_fetch_row(%result, row, $MYSQL_NUM)) {
  1266. ;;;     echo -a Second execution:
  1267. ;;;     echo -a 1st: $hget(row, 1)
  1268. ;;;     echo -a 2nd: $hget(row, 2)
  1269. ;;;   }
  1270. ;;;   mysql_free %result
  1271. ;;;   mysql_free %stmt
  1272. ;;; }
  1273. ;;; else {
  1274. ;;;   echo -a Error preparing query: %mysql_errstr
  1275. ;;; }
  1276. ;;;
  1277. ;;; ; Output:
  1278. ;;; ; First execution:
  1279. ;;; ; 1st: Hello
  1280. ;;; ; 2nd: World
  1281. ;;; ; Second execution:
  1282. ;;; ; 1st: Another
  1283. ;;; ; 2nd: World
  1284. ;;; </example>
  1285. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  1286. ;;; <seealso>mysql_prepare</seealso>
  1287. ;;; <seealso>mysql_bind_field</seealso>
  1288. ;;; <seealso>mysql_bind_value</seealso>
  1289. ;;; <seealso>mysql_bind_null</seealso>
  1290. ;;; <seealso>mysql_clear_bindings</seealso>
  1291. alias mysql_bind_param {
  1292.   var %params = $iif($0 >= 1, $mysql_param($1))
  1293.   if ($0 >= 2) %params = %params $mysql_param($2)
  1294.   if ($0 >= 3) %params = %params $mysql_param($3)
  1295.   return $dll($mysql_dll, mmysql_bind_param, %params)
  1296. }
  1297.  
  1298. ;;; <summary>Binds a value as a parameter for prepared statement.</summary>
  1299. ;;; <syntax>$mysql_bind_value ( statement, param, value )</syntax>
  1300. ;;; <syntax>/mysql_bind_value statement param value</syntax>
  1301. ;;; <param name="statement">The prepared statement identifier.</param>
  1302. ;;; <param name="param">The parameter to bind to. Must exist in the prepared query.</param>
  1303. ;;; <param name="value">The value to bind to.</param>
  1304. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  1305. ;;; <remarks>
  1306. ;;; <b>$mysql_bind_value</b> can be used to bind a value to a parameter.
  1307. ;;;
  1308. ;;; The <i>param</i> parameter can either be a numerical index, specified with a <b>?</b> in the query, or a named parameter specified with a <b>:name</b> in the query. If binding a named parameter, you should also include the colon in the name.
  1309. ;;;
  1310. ;;; Note that if you want to bind a text value with more than one word, you must use the first form of syntax. If you don't care about the return value, you can use the built-in mIRC command <b>/noop</b>
  1311. ;;;
  1312. ;;; For more information about parameter binding, see <a href="prepared.html">Prepared Statements</a>.
  1313. ;;; </remarks>
  1314. ;;; <example>
  1315. ;;; ; Binds one numerical and one named parameter
  1316. ;;; var %sql = SELECT ?, :test
  1317. ;;; var %stmt = $mysql_prepare(%db, %sql)
  1318. ;;; if (%stmt) {
  1319. ;;;   ; Binds 'Hello world' as first parameter and 100 as second parameter as float.
  1320. ;;;   ; We must use the $mysql_bind_param syntax here, because the value contains more than one word.
  1321. ;;;   noop $mysql_bind_value(%stmt, 1, Hello world)
  1322. ;;;   mysql_bind_value %stmt :test 100
  1323. ;;;
  1324. ;;;   ; Execute the query and show the results
  1325. ;;;   var %result = $mysql_query(%stmt)
  1326. ;;;   if ($mysql_fetch_row(%result, row, $MYSQL_NUM)) {
  1327. ;;;     echo -a 1st: $hget(row, 1)
  1328. ;;;     echo -a 2nd: $hget(row, 2)
  1329. ;;;   }
  1330. ;;;
  1331. ;;;   mysql_free %result
  1332. ;;;   mysql_free %stmt
  1333. ;;; }
  1334. ;;; else {
  1335. ;;;   echo -a Error preparing query: %mysql_errstr
  1336. ;;; }
  1337. ;;;
  1338. ;;; ; Output:
  1339. ;;; ; 1st: Hello world
  1340. ;;; ; 2nd: 100.0
  1341. ;;; </example>
  1342. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  1343. ;;; <seealso>mysql_prepare</seealso>
  1344. ;;; <seealso>mysql_bind_field</seealso>
  1345. ;;; <seealso>mysql_bind_param</seealso>
  1346. ;;; <seealso>mysql_bind_null</seealso>
  1347. ;;; <seealso>mysql_clear_bindings</seealso>
  1348. alias mysql_bind_value {
  1349.   var %params = $iif($0 >= 1, $mysql_param($1))
  1350.   if ($0 >= 2) %params = %params $mysql_param($2)
  1351.   if ($0 >= 3) %params = %params $mysql_param($3)
  1352.   return $dll($mysql_dll, mmysql_bind_value, %params)
  1353. }
  1354.  
  1355. ;;; <summary>Binds null as a parameter for prepared statement.</summary>
  1356. ;;; <syntax>$mysql_bind_null ( statement, param )</syntax>
  1357. ;;; <syntax>/mysql_bind_null statement param</syntax>
  1358. ;;; <param name="statement">The prepared statement identifier.</param>
  1359. ;;; <param name="param">The parameter to bind to. Must exist in the prepared query.</param>
  1360. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  1361. ;;; <remarks>
  1362. ;;; The <i>param</i> parameter can either be a numerical index, specified with a <b>?</b> in the query, or a named parameter specified with a <b>:name</b> in the query. If binding a named parameter, you should also include the colon in the name.
  1363. ;;;
  1364. ;;; For more information about parameter binding, see <a href="prepared.html">Prepared Statements</a>.
  1365. ;;; </remarks>
  1366. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  1367. ;;; <seealso>mysql_prepare</seealso>
  1368. ;;; <seealso>mysql_bind_field</seealso>
  1369. ;;; <seealso>mysql_bind_param</seealso>
  1370. ;;; <seealso>mysql_bind_value</seealso>
  1371. ;;; <seealso>mysql_clear_bindings</seealso>
  1372. alias mysql_bind_null {
  1373.   var %params = $iif($0 >= 1, $mysql_param($1))
  1374.   if ($0 >= 2) %params = %params $mysql_param($2)
  1375.   return $dll($mysql_dll, mmysql_bind_null, %params)
  1376. }
  1377.  
  1378. ;;; <summary>Clears all bindings from a result or a statement.</summary>
  1379. ;;; <syntax>$mysql_clear_bindings ( result )</syntax>
  1380. ;;; <syntax>/mysql_clear_bindings result</syntax>
  1381. ;;; <syntax>$mysql_clear_bindings ( statement )</syntax>
  1382. ;;; <syntax>/mysql_clear_bindings statement</syntax>
  1383. ;;; <param name="result">The result identifier.</param>
  1384. ;;; <param name="statement">The prepared statement identifier.</param>
  1385. ;;; <returns><b>1</b> on success, or <b>$null</b> if there was an error.</returns>
  1386. ;;; <remarks>
  1387. ;;; <b>$mysql_clear_bindings</b> clear all parameter bindings from a specified result or prepared statement. That is, unless they're re-bound, they will default to NULL.
  1388. ;;;
  1389. ;;; If used to clear bindings in a result set, clears all bindings specified with <a href="mysql_bind_field.html">$mysql_bind_field</a>. If used to clear bindings in a prepared statement, clears all bindings specified with <a href="mysql_bind_param.html">$mysql_bind_param</a> or <a href="mysql_bind_value.html">$mysql_bind_value</a>.
  1390. ;;;
  1391. ;;; It is usually ok to ignore the return value of <b>$mysql_clear_bindings</b> because the only case an error is returned is when an invalid <i>statement</i> is specified.
  1392. ;;; </remarks>
  1393. ;;; <seealso href="prepared.html">Prepared Statements</seealso>
  1394. ;;; <seealso>mysql_prepare</seealso>
  1395. ;;; <seealso>mysql_bind_param</seealso>
  1396. ;;; <seealso>mysql_bind_value</seealso>
  1397. ;;; <seealso>mysql_bind_null</seealso>
  1398. alias mysql_clear_bindings {
  1399.   var %params = $iif($0 >= 1, $mysql_param($1))
  1400.   return $dll($mysql_dll, mmysql_bind_null, %params)
  1401. }
  1402.  
  1403. ;;; <summary>Fetches field info from a result.</summary>
  1404. ;;; <syntax>$mysql_fetch_field_info ( result, hash_table [, field_offset ] )</syntax>
  1405. ;;; <param name="result">The result identifier.</param>
  1406. ;;; <param name="hash_table">The name of the hash table to where to store the field data.</param>
  1407. ;;; <param name="field_offset">Optional. The field offset which to fetch info for.</param>
  1408. ;;; <returns><b>1</b> on success; Otherwise <b>0</b> if there are no more fields available, or <b>$null</b> if there was an error.</returns>
  1409. ;;; <remarks>
  1410. ;;; <b>$mysql_fetch_field</b> fetches the next field from the <i>result</i> and stores the data in <i>hash_table</i>.
  1411. ;;; If the hash table doesn't exist, it will be created; Otherwise it will be cleared before new data is stored.
  1412. ;;;
  1413. ;;; If <i>field_offset</i> isn't specified, <b>$mysql_fetch_field</b> fetches the first unfetched field info. Otherwise it'll fetch the info for the specified field.
  1414. ;;; The first field has an offset of 1.
  1415. ;;;
  1416. ;;; The resulting hash table will have the following items:
  1417. ;;; <pre><b>name</b>            Name of the field, can be an alias.
  1418. ;;; <b>org_name</b>        Original name of the field, aliases are ignored.
  1419. ;;; <b>table</b>           Name of the table the field belongs in, can be an alias.
  1420. ;;; <b>org_table</b>       Original name of the table the field belongs in, aliases are ignored.
  1421. ;;; <b>db</b>              Database the field belongs in.
  1422. ;;; <b>length</b>          Length of the field.
  1423. ;;; <b>max_length</b>      The length of the longest value in the result set. 0 for unbuffered results.
  1424. ;;; <b>not_null</b>        1 if the field is not null, otherwise 0.
  1425. ;;; <b>primary_key</b>     1 if the field is a primary key, otherwise 0.
  1426. ;;; <b>unique_key</b>      1 if the field is an unique key, otherwise 0.
  1427. ;;; <b>multiple_key</b>    1 if the field is part of a non-unique key, otherwise 0.
  1428. ;;; <b>unsigned</b>        1 if the field is has unsigned attribute, otherwise 0.
  1429. ;;; <b>zerofill</b>        1 if the field is has zerofill attribute, otherwise 0.
  1430. ;;; <b>binary</b>          1 if the field is binary, otherwise 0.
  1431. ;;; <b>numeric</b>         1 if the field is numeric, otherwise 0.
  1432. ;;; <b>auto_increment</b>  1 if the field is auto incrementing, otherwise 0.
  1433. ;;; <b>no_default</b>      1 if the field doesn't have a default value, otherwise 0.
  1434. ;;; <b>decimals</b>        Number of decimals for the field.
  1435. ;;; <b>type</b>            The type of the field.</pre>
  1436. ;;; </remarks>
  1437. ;;; <seealso>mysql_field_info_seek</seealso>
  1438. ;;; <seealso>mysql_field_name</seealso>
  1439. ;;; <seealso>mysql_field_type</seealso>
  1440. ;;; <seealso>mysql_field_len</seealso>
  1441. ;;; <seealso>mysql_field_table</seealso>
  1442. ;;; <seealso>mysql_field_flags</seealso>
  1443. alias mysql_fetch_field_info {
  1444.   var %params = $iif($0 >= 1, $mysql_param($1))
  1445.   if ($0 >= 2) %params = %params $mysql_param($gettok($2,1,32))
  1446.   if ($0 >= 3) %params = %params $mysql_param($3)
  1447.   return $dll($mysql_dll, mmysql_fetch_field_info, %params)
  1448. }
  1449.  
  1450. ;;; <summary>Seeks to a field info offset.</summary>
  1451. ;;; <syntax>$mysql_fetch_field_info ( result, field_offset )</syntax>
  1452. ;;; <param name="result">The result identifier.</param>
  1453. ;;; <param name="field_offset">The field offset to seek to.</param>
  1454. ;;; <returns><b>1</b> on success or <b>$null</b> if there was an error.</returns>
  1455. ;;; <remarks>
  1456. ;;; The first field has an offset of 1.
  1457. ;;; </remarks>
  1458. ;;; <seealso>mysql_fetch_field_info</seealso>
  1459. ;;; <seealso>mysql_field_name</seealso>
  1460. ;;; <seealso>mysql_field_type</seealso>
  1461. ;;; <seealso>mysql_field_len</seealso>
  1462. ;;; <seealso>mysql_field_table</seealso>
  1463. ;;; <seealso>mysql_field_flags</seealso>
  1464. alias mysql_field_info_seek {
  1465.   var %params = $iif($0 >= 1, $mysql_param($1))
  1466.   if ($0 >= 2) %params = %params $mysql_param($2)
  1467.   return $dll($mysql_dll, mmysql_field_info_seek, %params)
  1468. }
  1469.  
  1470. ;;; <summary>Returns field name.</summary>
  1471. ;;; <syntax>$mysql_field_name( result, field_offset )</syntax>
  1472. ;;; <param name="result">The result identifier.</param>
  1473. ;;; <param name="field_offset">The field offset.</param>
  1474. ;;; <returns>Field name on success or <b>$null</b> if there was an error.</returns>
  1475. ;;; <seealso>mysql_fetch_field_info</seealso>
  1476. ;;; <seealso>mysql_field_name</seealso>
  1477. ;;; <seealso>mysql_field_type</seealso>
  1478. ;;; <seealso>mysql_field_len</seealso>
  1479. ;;; <seealso>mysql_field_table</seealso>
  1480. ;;; <seealso>mysql_field_flags</seealso>
  1481. alias mysql_field_name {
  1482.   var %params = $iif($0 >= 1, $mysql_param($1))
  1483.   if ($0 >= 2) %params = %params $mysql_param($2)
  1484.   return $dll($mysql_dll, mmysql_field_name, %params)
  1485. }
  1486.  
  1487. ;;; <summary>Returns field type.</summary>
  1488. ;;; <syntax>$mysql_field_type( result, field_offset )</syntax>
  1489. ;;; <param name="result">The result identifier.</param>
  1490. ;;; <param name="field_offset">The field offset.</param>
  1491. ;;; <returns>Field type on success or <b>$null</b> if there was an error.</returns>
  1492. ;;; <seealso>mysql_fetch_field_info</seealso>
  1493. ;;; <seealso>mysql_field_name</seealso>
  1494. ;;; <seealso>mysql_field_len</seealso>
  1495. ;;; <seealso>mysql_field_table</seealso>
  1496. ;;; <seealso>mysql_field_flags</seealso>
  1497. alias mysql_field_type {
  1498.   var %params = $iif($0 >= 1, $mysql_param($1))
  1499.   if ($0 >= 2) %params = %params $mysql_param($2)
  1500.   return $dll($mysql_dll, mmysql_field_type, %params)
  1501. }
  1502.  
  1503. ;;; <summary>Returns field length.</summary>
  1504. ;;; <syntax>$mysql_field_len( result, field_offset )</syntax>
  1505. ;;; <param name="result">The result identifier.</param>
  1506. ;;; <param name="field_offset">The field offset.</param>
  1507. ;;; <returns>Field length on success or <b>$null</b> if there was an error.</returns>
  1508. ;;; <seealso>mysql_fetch_field_info</seealso>
  1509. ;;; <seealso>mysql_field_name</seealso>
  1510. ;;; <seealso>mysql_field_type</seealso>
  1511. ;;; <seealso>mysql_field_table</seealso>
  1512. ;;; <seealso>mysql_field_flags</seealso>
  1513. alias mysql_field_len {
  1514.   var %params = $iif($0 >= 1, $mysql_param($1))
  1515.   if ($0 >= 2) %params = %params $mysql_param($2)
  1516.   return $dll($mysql_dll, mmysql_field_len, %params)
  1517. }
  1518.  
  1519. ;;; <summary>Returns name of the table the field belongs to.</summary>
  1520. ;;; <syntax>$mysql_field_table( result, field_offset )</syntax>
  1521. ;;; <param name="result">The result identifier.</param>
  1522. ;;; <param name="field_offset">The field offset.</param>
  1523. ;;; <returns>Table name on success or <b>$null</b> if there was an error.</returns>
  1524. ;;; <seealso>mysql_fetch_field_info</seealso>
  1525. ;;; <seealso>mysql_field_name</seealso>
  1526. ;;; <seealso>mysql_field_type</seealso>
  1527. ;;; <seealso>mysql_field_len</seealso>
  1528. ;;; <seealso>mysql_field_flags</seealso>
  1529. alias mysql_field_table {
  1530.   var %params = $iif($0 >= 1, $mysql_param($1))
  1531.   if ($0 >= 2) %params = %params $mysql_param($2)
  1532.   return $dll($mysql_dll, mmysql_field_table, %params)
  1533. }
  1534.  
  1535. ;;; <summary>Returns field flags.</summary>
  1536. ;;; <syntax>$mysql_field_flags( result, field_offset )</syntax>
  1537. ;;; <param name="result">The result identifier.</param>
  1538. ;;; <param name="field_offset">The field offset.</param>
  1539. ;;; <returns>Field flags on success or <b>$null</b> if there was an error.</returns>
  1540. ;;; <seealso>mysql_fetch_field_info</seealso>
  1541. ;;; <seealso>mysql_field_name</seealso>
  1542. ;;; <seealso>mysql_field_type</seealso>
  1543. ;;; <seealso>mysql_field_len</seealso>
  1544. ;;; <seealso>mysql_field_table</seealso>
  1545. alias mysql_field_flags {
  1546.   var %params = $iif($0 >= 1, $mysql_param($1))
  1547.   if ($0 >= 2) %params = %params $mysql_param($2)
  1548.   return $dll($mysql_dll, mmysql_field_flags, %params)
  1549. }