Created
April 28, 2016 15:26
-
-
Save mbagattini/c02c7327966fb0c73f96340cddd23be2 to your computer and use it in GitHub Desktop.
Three simple functions that mimic ADO.NET approach to relational database querying
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $mysql_server = "your_host"; | |
| $mysql_database = "your_database_name"; | |
| $mysql_username = "your_database_user"; | |
| $mysql_password = "your_database_password"; | |
| //executes a query and returns the first value or null when nothing's found | |
| //sample: $scalarValue = SQLExecuteScalar("select field from table where id=1"); | |
| //sample: $scalarValue = SQLExecuteScalar("select * from info"); //returns first row, first column field | |
| function SQLExecuteScalar($query, $default = null) | |
| { | |
| //imports global settings | |
| global $mysql_server, $mysql_username, $mysql_password, $mysql_database; | |
| //enstablish connection | |
| mysql_connect($mysql_server, $mysql_username, $mysql_password) | |
| or die("connection failed: " . mysql_error()); | |
| //select database | |
| mysql_select_db($mysql_database) | |
| or die("Database not found: " . mysql_error()); | |
| //get query results | |
| $results = mysql_query($query) | |
| or die("Error executing query: " . mysql_error()); | |
| if (mysql_num_rows($results)) { | |
| $row = mysql_fetch_row($results); | |
| mysql_free_result($results); | |
| return $row[0]; | |
| } | |
| return $default; | |
| } | |
| //executes a query and return the number of affected rows | |
| //sample: $affectedRows = SQLExecuteNonQuery("insert into table(id, field) values (1, 'test')"); | |
| function SQLExecuteNonQuery($query) | |
| { | |
| global $mysql_server, $mysql_username, $mysql_password, $mysql_database; | |
| //enstablish connection | |
| mysql_connect($mysql_server, $mysql_username, $mysql_password) | |
| or die("connection failed: " . mysql_error()); | |
| //select database | |
| mysql_select_db($mysql_database) | |
| or die("Database not found: " . mysql_error()); | |
| //get query results | |
| mysql_query($query) | |
| or die("Error executing query: " . mysql_error()); | |
| //returns affected rows count | |
| return mysql_affected_rows(); | |
| } | |
| //executes a query and returns selected data as an associative array | |
| //$associativeArray = SQLExecuteReader("select * from table"); | |
| function SQLExecuteReader($query) | |
| { | |
| //imports global settings | |
| global $mysql_server, $mysql_username, $mysql_password, $mysql_database; | |
| //enstablish connection | |
| mysql_connect($mysql_server, $mysql_username, $mysql_password) | |
| or die("connection failed: " . mysql_error()); | |
| //select database | |
| mysql_select_db($mysql_database) | |
| or die("Database not found: " . mysql_error()); | |
| //get query results | |
| $results = mysql_query($query) | |
| or die("Error executing query: " . mysql_error()); | |
| //fetch row by row into an associative array and return | |
| $fetched_data = Array(); | |
| while($row = mysql_fetch_assoc($results)) | |
| { | |
| array_push($fetched_data, $row); | |
| } | |
| return $fetched_data; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment