Jump to content

Recommended Posts

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

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'];
}
}

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?

 

 

 

 

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";
}

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.