runsql
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| runsql [2005/10/05 07:55] – 209.8.22.201 | runsql [2006/11/09 16:36] (current) – 216.254.60.114 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Execute an SQL statement on a MySQL DB ====== | ||
| + | |||
| + | <code php> | ||
| + | |||
| + | |||
| + | function runSQL($sql_string) { | ||
| + | |||
| + | global $config; | ||
| + | $link = mysql_connect ($config[' | ||
| + | $result = mysql_db_query($config[' | ||
| + | // | ||
| + | |||
| + | if ($result != 1) { | ||
| + | for($i=0; $i< mysql_num_rows($result); | ||
| + | $temparray = mysql_fetch_assoc($result); | ||
| + | $resultarray[]=$temparray; | ||
| + | } | ||
| + | mysql_free_result ($result); | ||
| + | } | ||
| + | |||
| + | if (mysql_insert_id($link)) { | ||
| + | $resultarray = mysql_insert_id($link); | ||
| + | } | ||
| + | |||
| + | mysql_close ($link); | ||
| + | return $resultarray; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | == Note a few things about this code == | ||
| + | * The global $config must be set somewhere. | ||
| + | * The $sql_string variable does not have a default value, --it probably should if unsafe users have access(as I rush off to change my own code!) | ||
| + | * mysql has an " | ||
| + | * this is a generic function, --you might want to use full database.table syntax if using this query out of the box | ||
| + | * There is another way to write queries to take advantage of OOP ([[http:// | ||
| + | |||
