Jump to content

Why isn't this working?


V

Recommended Posts

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  :'(

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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());

 

 

Link to comment
Share on other sites

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..

 

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.