Help, with query to return user info to pass to url and hidden fields

I am ok with codeing but NOT half-as good as some of your GUYS here.
I’m trying to write a simple sql query based on returning certain values from a table for that user.

I would like to just beable to include this as a page user.info.php and render it on any page as include to return need values .

See My CODE. PLESE HELP ME.


<?php 

		// Set cookie
		$userid = JRequest::getVar('userid');		
		
		$data       = new stdClass();
		$model      =& $this->getModel('profile');
		$my         = CFactory::getUser();
		
		// Test if userid is 0, check if the user is viewing its own profile.
		$db =& JFactory::getDBO();
		$user =& JFactory::getUser();
        $userId = $user->id;

		// Return with empty data
		if($userId == null || $userId == '')
		{
			//return false;
		}

		$user		=& JFactory::getUser($userId);
		
		if($user->id == null){
			//return false;
		}
		
		$id = & JFactory::getUser($userId);
		$query = 'SELECT user_id, id, format_id, year, name FROM #__muscol_albums WHERE user_id = ' . $id;
		//$query = 'SELECT user_id FROM #__muscol_albums WHERE id = ' . $album_id ;
		$result = mysql_query($query) or die('Error, No Album Search failed');
		list($name, $user_id, $id, $year) = mysql_fetch_array($result);
	
	 
		echo $id;
		echo $user_id;
		echo $year;
			
			 
	 		
    // preform id return check and redirecto to correct url
    if ($user->get('id') == 0 || $userid == 0 || $userid <> $user->get('id')){
	 $url=JURI::root().'index.php?'.$component.'&id='.$id.'&amp;tmpl=component&amp;print=1';
	} else {
    $url=JURI::root().'index.php?option=com_community&amp;view=profile&amp;id=1&amp;tmpl=component&amp;print=1'; //redirect      is a function 
	 
	}

?>

From a quick look your SQL query seems wrong to me, in your list you define variables to you but your SQL query does not match those variables.

$query = 'SELECT user_id, id, format_id, year, name FROM #__muscol_albums WHERE user_id = ' . $id;

list($name, $user_id, $id, $year) = mysql_fetch_array($result);

Also your code to me is a little messy as you use JFactory::[B]getUser/B multiple times when it really only needs to be called once. Below is a cleaned up version which i think should work fine.

// Set cookie
$userid = JRequest::getVar('userid');        

$data  = new stdClass();
$model =& $this->getModel('profile');
$my    = CFactory::getUser();

// Test if userid is 0, check if the user is viewing its own profile.
$db =& JFactory::getDBO();
$user =& JFactory::getUser();

// Return with empty data
if ($user->id == null || empty($user->id)) {
    //return false;
}

// Query the database for the user
$sql = "SELECT id, year, name FROM #__muscol_albums WHERE user_id = '" . $user->id . "'";
//$query = 'SELECT user_id FROM #__muscol_albums WHERE id = ' . $album_id ;

$result = mysql_query($query) or die('Error, No Album Search failed');

if (mysql_num_rows($result) > 0) {
    list($id, $year, $name) = mysql_fetch_array($result);

    // Display the results
    echo $id . '<br />' . $user->id . '<br />' . $year;

    // Preform id return check and redirecto to correct url
    if ($user->get('id') == 0 || $userid == 0 || $userid <> $user->get('id')){
        $url = JURI::root() . 'index.php?' . $component . '&id=' . $id . '&amp;tmpl=component&amp;print=1';
    } else {
        $url = JURI::root() . 'index.php?option=com_community&amp;view=profile&amp;id=1&amp;tmpl=component&amp;print=1'; 
    }
} else {
    echo 'No results exist!';
}

One last thing, call_time_pass_reference shouldn’t be used anymore. If your confused im simply talking about =& which is deprecated in PHP 5.3. To stop errors from occurring the most simple fix is to change it to a straight = sign but that may not always be the best solution.

SgtLegend, you are right my coing is little messy. Trying and get there, with better reading of code layouts.

i implemented the alternative you sent, it does not eror out, but t stops and procuded the call execut on this line is where it goes than
jsut does the die
$result = mysql_query($sql) or die(‘Error, No Album Search failed’);

It seems like even is I haev the $userid=xx in teh browser it still does that, what I am I missing.

It is not running the query adn returing the valeus needed.

Instead of using

$result = mysql_query($sql) or die('Error, No Album Search failed');

use

$result = mysql_query($sql) or die('Error, No Album Search failed<br />' . mysql_error());

and let me know what the error is.

SGTLegend, please help me

Hope you around to respond.
Here is Error Message i am getting when SQL returns


	Error, No Album Search failed
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 '' at line 1

WHAT DEOS THAT MEAN?
I added this echo to shwo me what it is not colelctiong, see results

CODE ADDED echo


		// Query the database for the user
		$sql = 'SELECT id, year, name FROM #__muscol_albums WHERE user_id = ' . $user->id;
		//$query = 'SELECT user_id FROM #__muscol_albums WHERE id = ' . $album_id ;
		
		//$result = mysql_query($sql) or die('Error, No Album Search failed');
		echo $sql;
		echo '<br/>';
		echo $id;
		echo '<br/>';
		echo $user->id;
		echo '<br/>';
		echo $year;
		echo '<br/>';
		echo $name;
		echo '<br/>';
		$result = mysql_query($sql) or die('Error, No Album Search failed<br />' . mysql_error());   
		

RESULTS


SELECT id, year, name FROM #__muscol_albums WHERE user_id = 83
25
83

mosConfig_frontend_userparams
Error, No Album Search failed
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 '' at line 1

NO name, year, and it only returns the id ‘25’ when I put it in the browser as &id=25, otehrwise it does not return formt eh sql statement in echo, that appears to be issue the sql statement.

PLEASE HELP

SGTLegend, followed all what you wrote and added my own littel tweaks to fit my app, got it to work perfectly, man thank you soooo much.
YOU guys on DEVSHED rock, good and bad…