The error is "Object of class JUser could not be converted to string in..."bye-bye :o"> How to show records were login matces a field - Forums

Forums

How to show records were login matces a field

maxtemp 13 Feb, 2010
Hello everybody
i'm tryng with, my poor knowledge, to create an application were a user after login into the joomla site, can view all his owm records. The code i have wrote gives me an erroršŸ˜² . Can you give it a look?
<?php
$db =& JFactory::getDBO();
$user = JFactory::getUser();
 $sql=" 
    SELECT *
      FROM `jos_easytables_table_data_4`
      WHERE 'user'like '$user'";

$db->setQuery($sql);

$results = $db->loadRowList();

foreach ($results as $result ); { 

  echo "Tracking_number: " .$result [1]."<br />";
  echo "Sender: " .$result [2]."<br />";
  echo "Client code: " .$result [3]."<br />";
  echo "Status: " .$result [4]."<br />";
}
?>

The error is "Object of class JUser could not be converted to string in..."
bye-bye :o
GreyHead 13 Feb, 2010
HI maxtemp,

A couple of little glitches:
<?php
$db =& JFactory::getDBO();
$user =& JFactory::getUser();
 $sql=" 
    SELECT *
      FROM `#__easytables_table_data_4`
      WHERE `user` LIKE '".$user->id."'
";
Assuming that 'user' is storing the id.

Bob
maxtemp 13 Feb, 2010
Hello GreyHead
i tried as you suggested, and i have no more errors but i also have no rows echoed?
My database structured like this:
ID|Tracking_number|Sender| Client_code| Status:| USERNAME
1 | 123 | Paul | Z4458 | SHIPPED | [email]Paul@Paul.com[/email]
2 | 111 | Mark | Z5588 | RECIVED | [email]Mark@Mark.com[/email]
3 | 222 | Paul | Z9988 | SHIPPED | [email]Paul@Paul.com[/email]

if Paul logs in with his email "Paul@Paul.com" the application shoul echo :
1 -123-Paul-Z4458-SHIPPED
3 -222-Paul-Z9988-SHIPPED
more or less, but now it echoes only the field names.
GreyHead 13 Feb, 2010
Hi maxtemp,

Looks like you are storing the username not the userid, you'll need to change my code to use $user->username instead of $user->id

Bob
maxtemp 14 Feb, 2010
Thanks for your time Greyhead
following your advices, i have modified the code into:
<?php
$db =& JFactory::getDBO();
$user = JFactory::getUser();
 $sql=" 
    SELECT *
      FROM `#_easytables_table_data_4`
      WHERE `USERNAME` LIKE '".$user->USERNAME."'";

$db->setQuery($sql);

$results = $db->loadRowList();

foreach ($results as $result ); {
  echo "Tracking_number: " .$result [1]."<br />";
  echo "Sender: " .$result [2]."<br />";
  echo "Client code: " .$result [3]."<br />";
  echo "Status: " .$result [4]."<br />";
}
?>

but still it gives me only the fields name but not the rest.
Marco
GreyHead 14 Feb, 2010
Hi maxtemp,

Wwll I can see a few errors in the rest of your PHP, you have a stray semicolon and some spaces in the array names that probably shouldn't be there. Clean that up and it will probably be OK.

Bob
PS And probably that should be $user->username in lower case
maxtemp 14 Feb, 2010
Hi
i've eliminated the semicolon in the foreach construct (should be like that reading the php handbook), used a lowecase for "username" both in the code & database


<?php
$db =& JFactory::getDBO();
$user = JFactory::getUser();
 $sql=" 
    SELECT *
      FROM `#_easytables_table_data_4`
      WHERE `username` LIKE '".$user->username."'";

$db->setQuery($sql);

$results = $db->loadRowList();

foreach ($results as $result ) {
  echo "Tracking_number: " .$result [1]."<br />";
  echo "Sender: " .$result [2]."<br />";
  echo "Client_code: " .$result [3]."<br />";
  echo "Status: " .$result [4]."<br />";
}
?>

but now i get no answer from the compilation, no field names but also no error message.
GreyHead 14 Feb, 2010
Hi maxtemp,

Far be it for me to disagree with your PHP handbook but I don't think there ought to be any space in $result [1] either.

Bob
maxtemp 14 Feb, 2010
Ok
i have reinserted the semicolom and deleted the spaces
foreach ($results as $result );
{
echo "Tracking_number: " .$result[1]."<br />";
echo "Sender: " .$result[2]."<br />";
echo "Client_code: " .$result[3]."<br />";
echo "Status: " .$result[4]."<br />";
}
but still the application doesn't extract any fields, it echoes only the field names.
It's like it can't find the similarity between the user and the username.
GreyHead 15 Feb, 2010
Hi maxtemp,

I'm not quite sure what you are after here. Your code needs debugging. You've fixed all the obvious things that I can see, that doesn't mean to say that there aren't still errors in there. (Clearly there are, other wise it would work.)

The next thing I would so is to adds some debug statements in appropriate places to see what is happening at each step.
echo '<div>$sql: '.print_r($sql, true).'</div>';
. . . 
echo '<div>$results: '.print_r($results, true).'</div>';

Bob
This topic is locked and no more replies can be posted.