V Posted June 17, 2010 Share Posted June 17, 2010 I created 1 function that connects to the db, function dbConnect() { $connection = new mysqli('localhost', 'user', 'password', 'gisttest') or die ('Cannot open database'); return $connection; }//end function and another that queries some categories, function listQuery($table) { // prepare the SQL query $sql = 'SELECT * FROM $table'; $result = $connection->query($sql) or die(mysqli_error()); // find out how many records were retrieved $numRows = $result->num_rows; while ($row = $result->fetch_assoc()) { echo strtoupper("<li>{$row["cat_name"]}</li>"); } }//end function and I'm using require_once("/functions/db_fns.php"); $connection = dbConnect(); echo listQuery("categories"); to show the results but I get Notice: Undefined variable: connection in C:\wamp\www\gisttest\functions\db_fns.php on line 17 Fatal error: Call to a member function query() on a non-object in C:\wamp\www\gisttest\functions\db_fns.php on line 17 It works however when I establish the db connection within the listQuery fucntion and just echo it alone or if I add the entire connection code above echo listQuery("categories"); and remove the $connection = dbConnect(); on top. I have been trying different methods to combine both functions but I keep getting errors. Please someone help me figure this out :'( Quote Link to comment https://forums.phpfreaks.com/topic/205088-why-isnt-this-working/ Share on other sites More sharing options...
Alex Posted June 17, 2010 Share Posted June 17, 2010 You should really look into variable scopes. Your problem is that $connection is not defined within your function, listQuery(). One solution is to pass $connection as a parameter like so: require_once("/functions/db_fns.php"); $connection = dbConnect(); echo listQuery("categories", $connection); function listQuery($table, $connection) { // prepare the SQL query $sql = 'SELECT * FROM $table'; $result = $connection->query($sql) or die(mysqli_error()); // find out how many records were retrieved $numRows = $result->num_rows; while ($row = $result->fetch_assoc()) { echo strtoupper("<li>{$row["cat_name"]}</li>"); } }//end function Quote Link to comment https://forums.phpfreaks.com/topic/205088-why-isnt-this-working/#findComment-1073554 Share on other sites More sharing options...
V Posted June 17, 2010 Author Share Posted June 17, 2010 Ohh right, variable scopes! I read about them prior to starting to code but I guess I forgot. I tried the code you provided but there is still some sort of error. Not sure how to fix it.. Warning: mysqli_error() expects exactly 1 parameter, 0 given in C:\wamp\www\gisttest\functions\db_fns.php on line.. $result = $connection->query($sql) or die(mysqli_error()); Quote Link to comment https://forums.phpfreaks.com/topic/205088-why-isnt-this-working/#findComment-1073556 Share on other sites More sharing options...
Alex Posted June 17, 2010 Share Posted June 17, 2010 You need to pass the connection, $connection in your case, to mysqli_error(). Quote Link to comment https://forums.phpfreaks.com/topic/205088-why-isnt-this-working/#findComment-1073559 Share on other sites More sharing options...
V Posted June 17, 2010 Author Share Posted June 17, 2010 Ok sorry for being such a noob.. Now I'm not getting some scary errors.. First, Table 'gisttest.$table' doesn't exist I changed $table to the actual table name in the functions like so function listQuery(categories, $connection) { // prepare the SQL query $sql = 'SELECT * FROM categories'; $result = $connection->query($sql) or die(mysqli_error($connection)); // find out how many records were retrieved $numRows = $result->num_rows; while ($row = $result->fetch_assoc()) { echo $numRows; echo strtoupper("<li>{$row["cat_name"]}</li>"); } }//end function Then I got Parse error: parse error, expecting `'&'' or `T_VARIABLE' in C:\wamp\www\gisttest\functions\db_fns.php on line 12 so put a & in function listQuery(categories, $connection) { like this function listQuery(categories&$connection) { and I got Fatal error: Cannot pass parameter 1 by reference in C:\wamp\www\gisttest\functions_test.php on line 17 so I tried removing the "categories" argument and just used $connection and got Fatal error: Call to a member function query() on a non-object Lastly, I decided that I suck at php.. lol update: I tried putting back the $table arguments and vars but put $table in double quotes $sql = 'SELECT * FROM "$table"'; and I get You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"$table"' at line 1 I think I'm getting close.. Quote Link to comment https://forums.phpfreaks.com/topic/205088-why-isnt-this-working/#findComment-1073564 Share on other sites More sharing options...
Alex Posted June 17, 2010 Share Posted June 17, 2010 Your first error is because single quotes don't have variable interpolation. Meaning that in this statement: $sql = 'SELECT * FROM "$table"'; $table is being taken as a literal string, not a variable. So you need to do this: $sql = "SELECT * FROM '$table'"; You're also forgetting a $ here: function listQuery(categories, $connection) { It should be: function listQuery($categories, $connection) { Quote Link to comment https://forums.phpfreaks.com/topic/205088-why-isnt-this-working/#findComment-1073569 Share on other sites More sharing options...
V Posted June 17, 2010 Author Share Posted June 17, 2010 Thanks! I initially had the $ but somehow it got lost when I pasted the code I used the double quotes and now it gives You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''categories'' at line 1 I'm echoing this echo listQuery("categories", $connection); This is weird, not sure what I keep doing wrong Quote Link to comment https://forums.phpfreaks.com/topic/205088-why-isnt-this-working/#findComment-1073578 Share on other sites More sharing options...
Alex Posted June 17, 2010 Share Posted June 17, 2010 Well that's what I get for blindly looking at the code I guess. You don't need the single quotes there regardless. $sql = "SELECT * FROM $table"; Quote Link to comment https://forums.phpfreaks.com/topic/205088-why-isnt-this-working/#findComment-1073579 Share on other sites More sharing options...
V Posted June 17, 2010 Author Share Posted June 17, 2010 Thanks!! It's finally working! I will use your code as reference in many future projects ..and I should really learn more about functions before I get banished lol. Quote Link to comment https://forums.phpfreaks.com/topic/205088-why-isnt-this-working/#findComment-1073581 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.