Jump to content

Need syntax help


Dustin013

Recommended Posts

Here is what I have so far

// Query database for value of requested row
// Example : $requested_value = (new fetchValue(global_data, id)); 
// This returns the value of the row "id" in the table "global_data"
class fetchValue {
  protected $table_name, $row_name;

  public function __construct($table_name, $row_name) {
    $this->table_id = $table_name;
$this->row_id = $row_name;
$content_query = 'SELECT '.$this->row_id.' FROM '.$this->table_id.' LIMIT 1';
echo $content_query."<br />";
$content_result = mysql_query($content_query);
$content_data = mysql_fetch_array($content_result);
echo "Value: ".($content_data['"$this->row_name"'])."<br />";
$this->variable = $content_data['$this->row_name'];
return $this->variable;
  }
}

 

The code will echo out the correct SQL query, however it does no return any value back.. I think the offending line of code is

$this->variable = $content_data['$this->row_name'];

 

I can't seem to figure it out :( Any suggestions?

 

The warning PHP gives me is the following :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource...

Catchable fatal error: Object of class fetchValue could not be converted to string...

 

I was also trying using the following method with no luck

// Query database for value of requested row
// Example : $requested_value = (new fetchValue(global_data, id)); 
// This returns the value of the row "id" in the table "global_data"
class fetchValue {
  protected $table_name, $row_name;

  public function __construct($table_name, $row_name) {
    $this->table_id = $table_name;
$this->row_id = $row_name;
    $this->queryRow();
  }

  protected function queryRow() {

$content_query = 'SELECT * FROM "$this->table_id"';
$content_result = mysql_query($content_result);
$content_data = mysql_fetch_array($content_result);
$this->variable = $content_data['"$this->row_name"'];

  }

  public function __toString() {
    return $this->variable;
  }
}

Link to comment
https://forums.phpfreaks.com/topic/147019-need-syntax-help/
Share on other sites

You were correct, so I changed that line and it appears that it still breaking...

 

 


$content_query = "SELECT $this->row_id FROM $this->table_id LIMIT 1"; 
echo $content_query;
$content_result = mysql_query($content_query) or die(mysql_error());

 

When I run the code and call the function using the following:

 

echo (new fetchValue(global_header, header_content));

 

I get this output...

 

SELECT header_content FROM global_header LIMIT 1

Catchable fatal error: Object of class fetchValue could not be converted to string in...

Link to comment
https://forums.phpfreaks.com/topic/147019-need-syntax-help/#findComment-771831
Share on other sites

You are trying to print an object like it is a string.

 

$obj = new fetchValue(global_header, header_content));
echo $obj->__toString();

 

Should work. Either way calling that object returns the object, even with the tostring method. You have to call it like a method/function to get the function to return what it is suppose to return.

Link to comment
https://forums.phpfreaks.com/topic/147019-need-syntax-help/#findComment-771883
Share on other sites

Ok so here is where I am at now

 

 

// Query database for value of requested row
// Example : $requested_value = (new fetchValue(global_data, id)); 
// This returns the value of the row "id" in the table "global_data"

class fetchValue {
  protected $table_name, $row_name;

  public function __construct($table_name, $row_name) {
$this->table_id = $table_name;
$this->row_id = $row_name;
     
            echo "The table name passed is named <b>".$this->table_id."</b><br />";
     echo "The row name passed is named <b>".$this->row_id."</b><br />";

$content_query = "SELECT $this->row_id FROM $this->table_id LIMIT 1";

     echo "The query executed was : <b>".$content_query."</b><br />";

$content_result = mysql_query($content_query) or die(mysql_error());
$content_data = mysql_fetch_array($content_result);

echo "Manually printing the value of page_name from query: <b>".$content_data['page_name']."</b><br />";

// page_name is the variable passed, however, I shouldn't have to put in page_name, I would like to use $this->row_id!

$var = $content_data['page_name'];
        echo "var has a value!!! The query is at least working!  : ".$var;

return $var;
  }
  
}

// Retrieve Page Info
///////////////////////
$obj = new fetchValue(page_info, page_name);
echo $obj->__toString();

 

The error generating from running this script...

 

The table name passed is named page_info

The row name passed is named page_name

The query executed was : SELECT page_name FROM page_info LIMIT 1

Manually printing the value of page_name from query: aboutus

var has a value!!! The query is at least working! : aboutus

 

Fatal error: Call to undefined method fetchValue::__toString() in

 

 

Using $var = $content_data['page_name'] then returning $var gives us the content from the table which is currently  :  "aboutus"

What I am trying to do is place the variable passed ($row_name) into content_data['row_name'] so that it doesn't have to be manually defined like I did it above.

The syntax $var = $content_data['$this->rowid'] is incorrect...??

 

I am slowly driving myself insane.. hehe.. thanks for the help so far guys!

Link to comment
https://forums.phpfreaks.com/topic/147019-need-syntax-help/#findComment-771915
Share on other sites

That method has to be defined in your class. It is not:

 

<?php
class myClass {
public function __construct() {
	$this->value = "test";
}

public function __toString() {
	return $this->value;
}
}

$class = new myClass();
echo $class->__toString();

// echos "test";
?>

Link to comment
https://forums.phpfreaks.com/topic/147019-need-syntax-help/#findComment-771917
Share on other sites

Thanks!!! That solved that part! Now, one last syntax issue....

 

$this->var = $content_data['page_name'];  this line, I am manually typing the row name, what I would like to do is use the variable $this->row_id ...

 

When I type it out, $var = $content_data['$this->row']; doesn't get a value because the syntax ['$this->row']; is incorrect...

 

I have tried a few different ways but none have seemed to work. Is it possible to use a variable to determine which row should be assigned when using mysql_fetch_array?

 

class fetchValue {
   public function __construct($table_name, $row_name) {
	$this->table_id = $table_name;
	$this->row_id = $row_name;
	$content_query = "SELECT $this->row_id FROM $this->table_id LIMIT 1";
	$content_result = mysql_query($content_query) or die(mysql_error());
	$content_data = mysql_fetch_array($content_result);
	$this->var = $content_data['page_name'];
   }
   
   public function __toString() {
      return $this->var;
   }
}

$var = new fetchValue(page_info, page_name);
echo $var;

Link to comment
https://forums.phpfreaks.com/topic/147019-need-syntax-help/#findComment-771927
Share on other sites

I have tried

 

var = $content_data[$row_name];

var = $content_data[$this->row_id];

 

Both don't return a value so I get the

 

Catchable fatal error: Method fetchValue::__toString() must return a string value in C:\xampp\htdocs\Fluidcontent\main.php on line 91

Link to comment
https://forums.phpfreaks.com/topic/147019-need-syntax-help/#findComment-771939
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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