V Posted June 17, 2010 Share Posted June 17, 2010 I have been using several functions for each query but now I'm attempting to create just one function that queries from the database by using 2 arguments. 1 argument for the DB table and another for the column. Perhaps I need more experience to do this but I'm really curious if I'm somewhat close. function listQuery($table,$column) { //1. Create DB connection $connection = mysql_connect("localhost", "user", "password"); if (!$connection) { die("Database connection failed: " .mysql_error()); } //2. Select a DB to use $db_select = mysql_select_db("sitetest",$connection); if (!$db_select) { die("Database selection failed: " .mysql_error()); } $table = ''; $column = ''; return mysql_query("SELECT * FROM $table", $connection); if (!$table) { die("Database query failed: " .mysql_error()); } while ($row = mysql_fetch_array($table)) { echo $row['$column']; } } and I'm using this code to echo the loop echo listQuery('categories','cat_name'); My idea was to list all my categories from the DB but instead I get a blank page. No errors tho. What are your thoughts? Link to comment https://forums.phpfreaks.com/topic/205006-my-first-complex-function/ Share on other sites More sharing options...
joel24 Posted June 17, 2010 Share Posted June 17, 2010 you're wiping the content of the column variable in the line: $column = ''; and also you're just executing the MYSQL query but not storing the results in a variable, you need to have @variable = mysql_query("select * from rararara"); and your returning nothing from the function. if you want to return an array of the db content instead of echoing it, you can just have return mysql_fetch_array($sql) instead of the while loop. try function listQuery($table,$column) { //1. Create DB connection $connection = mysql_connect("localhost", "user", "password"); if (!$connection) { die("Database connection failed: " .mysql_error()); } //2. Select a DB to use $db_select = mysql_select_db("sitetest",$connection); if (!$db_select) { die("Database selection failed: " .mysql_error()); } $sql = @mysql_query("SELECT * FROM $table", $connection); if (!$sql) { die("Database query failed: " .mysql_error()); } while ($row = mysql_fetch_array($sql)) { echo $row['$column']; } } Link to comment https://forums.phpfreaks.com/topic/205006-my-first-complex-function/#findComment-1073221 Share on other sites More sharing options...
V Posted June 17, 2010 Author Share Posted June 17, 2010 Joel that's very cool, thank you! I'm able to query using the $table variable but I get Notice: Undefined index: $column in C:\wamp\www\gisttest\functions.php on line 26 Notice: Undefined index: $column in C:\wamp\www\gisttest\functions.php on line 26 Notice: Undefined index: $column in C:\wamp\www\gisttest\functions.php on line 26 Notice: Undefined index: $column in C:\wamp\www\gisttest\functions.php on line 26 for the $column. It works however when I type the column name like this echo $row['cat_name']; instead of $column. Would you happen to know why the error appears? Link to comment https://forums.phpfreaks.com/topic/205006-my-first-complex-function/#findComment-1073466 Share on other sites More sharing options...
KevinM1 Posted June 17, 2010 Share Posted June 17, 2010 Joel that's very cool, thank you! I'm able to query using the $table variable but I get Notice: Undefined index: $column in C:\wamp\www\gisttest\functions.php on line 26 Notice: Undefined index: $column in C:\wamp\www\gisttest\functions.php on line 26 Notice: Undefined index: $column in C:\wamp\www\gisttest\functions.php on line 26 Notice: Undefined index: $column in C:\wamp\www\gisttest\functions.php on line 26 for the $column. It works however when I type the column name like this echo $row['cat_name']; instead of $column. Would you happen to know why the error appears? You assigned '' to $column, so when you use $row['$column'], you're actually attempting to access $row[''], which obviously doesn't exist. In order to access a table row column, you need to either know the column name, or use a foreach to cycle through them all. foreach ($row as $key => $value) { echo "Column name: $key -- Value: $value"; } Link to comment https://forums.phpfreaks.com/topic/205006-my-first-complex-function/#findComment-1073523 Share on other sites More sharing options...
V Posted June 17, 2010 Author Share Posted June 17, 2010 Oh I see. I guess I'll just type the column for now. Link to comment https://forums.phpfreaks.com/topic/205006-my-first-complex-function/#findComment-1073547 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.