I've created this function and I wonder if ever I COULD USE VARIABLES IN reading
column values on database table.

function vote($column, $imgid)//the $column stands for a variable handling a string I've passed from my previous call.  For now I am setting it as a string named "likes" which is equal to the name of 1 of my column in the table.
				{
					$query="SELECT $column FROM images WHERE imgid=$imgid";
					$result=mysql_query($query);
					if($column=mysql_fetch_assoc($result))
					{	
						$column=$column[$column]; // problem starts here. It prompts an error saying "Warning: Illegal offset type in C:\xampp\htdocs\wakiki\index.php";
						$column+=1;
					}
					$query="UPDATE images SET $column=$column WHERE imgid=$imgid";
					$result=mysql_query($query);
				}

Any help would be appreciated. Thanks

Perhaps it is better if you provide a clear picture of what you are trying to achieve. Can you give a detailed example of what should happen with the values (without code).

change line 8 to
$column = $column + 1;

i gather that you are wanting to update column by one. can you post more info please bud

@gotboots: His line 8, and your change, do exactly the same thing.

I think the problem is you are using $column for internal variables in your function. Change them to unique names like:

function vote($column, $imgid) {

    $query="SELECT $column FROM images WHERE imgid=$imgid";

    $result=mysql_query($query);

    if($row=mysql_fetch_assoc($result)) {
	
        $value=$row[$column];

        $value += 1; // or $value++ which is the same
    }

    // add this check to avoid an error if $row is false
    if($value and $value > 0) {

        $query="UPDATE images SET $column=$value WHERE imgid=$imgid";

        $result=mysql_query($query);
    }
}

yes broj1...you are right...the problem is because of variable value....because of using same variable name $column.....

Just change this:-

if($column=mysql_fetch_assoc($result))

to

if($row=mysql_fetch_assoc($result))

and

$value=$column[$column];

to

$value=$row[$column];

as directed by broj1

This may help. I sometimes use this in a WHILE loop, processed for each row of a result set. It's perhaps slightly less efficient, but can make code easier to read when I use the variables. With this code $row becomes $name, and so on for each attribute in the row.

// create a variable for each attribute
foreach($row as $var => $value){
  $$var = $value;
}

i think you need to make different var names, you are overwriting and using the same var for completely different things?

function vote($column, $imgid){
//the $column stands for a variable handling a string I've passed from my previous call.  For now I am setting it as a string named "likes" which is equal to the name of 1 of my column in the table.
	$query="SELECT $column FROM images WHERE imgid=$imgid";
	$result=mysql_query($query);
	if($row=mysql_fetch_assoc($result)){
		$column2=$row[$column];
		//$intcolumn+=1;//whats this supposed to do/be?
	}
	$query2="UPDATE images SET $column=$column WHERE imgid=$imgid";
	//$column = $column?? which is $column, $column2 or $intcolumn
	//$result=mysql_query($query);
}

Nope, it works just like that and creates a different variable for each attribute in the record being read out of the dataset. That snippet was taken from working code from one of my websites.

Here it is more in context:

while ($row = mysql_fetch_array($dataset)) {
  // create a variable for each attribute in row
  foreach($row as $var => $temp){
    $$var = $temp;
  }

  echo "<p>$lname, fname</p>";

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.