I have this code:

function get_setting($setting_name)
{
		$sql = "SELECT * FROM settings WHERE setting_name = '" . $setting_name . "'";

	$result = mysql_query("SELECT * FROM settings WHERE setting_name = '" . $setting_name . "'", $db);		
	$row = mysql_fetch_row($result);

	if (!isset($row))
	{
		echo "row isnt set";   
	}

	// return $row;
}

I know its messy, and it makes no sense, but bear with me. What do you think it prints? Well, Ill tell you. It prints "row isnt set", every time.

Why, is my question. Its ridiculous, and its driving me nuts. Please help!

    try the following

    
    $result = mysql_query("SELECT * FROM settings WHERE setting_name = '" . $setting_name . "'", $db) or die("Error in query: " . mysql_error());         
    $row = mysql_fetch_row($result) or die("Error fetching row: " . mysql_error());

    and see what (if any) errors you get

      i get exactly one instance of "row isnt set", as always.

        Or, since the function returns in an array, use isset($row[0]) or is_array($row)

          Funny, apparently $row[0] is not set, nor is $row even an array!

          Now this is getting ridiculous.

            or just if ($row) should really work... if the fetch fails, it will return false.

            [edit]try var_export($row); after the fetch command...[/edit]

              a few things ive just found:

              $db, which is defined in an external file, needs to be declared global inside the function. In 6 months, I've never had to do that.
              Perhaps my host changed the php settings.

              Second, for some reason or another, it now works. I have made NO CHANGES. it now returns the value like it should. Very strange indeed.Again, maybe my host was messing around with php settings or something.

              thanks for the help 😃

                you shouldn't really need the $db, PHP will just use the last opened connection if none is specified. You may have done that before, and therefor have never run into scope issues.

                  Write a Reply...