Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

Query Select within a function

Codes here !

Moderators: macek, egami, gesf

Query Select within a function

Postby ycans » Tue Sep 11, 2012 5:03 pm

Hi,
I'm very new to php and I'm trying to create a string within a function, but every time I try to output only the text, the text just shows up as "Array". Can you point me to where I'm going wrong?
Many thanks

Code: Select all
function delete_user($_POST, $delete) {
   
   if(isset($delete)) {
   
      $check = $_POST['check'];
      $id = $_POST['id'];
      
      $username = $this->query("SELECT username FROM ".DBTBLE." WHERE id = '$id'");
      
   if ($check == "yes") {

      return  $username." was deleted.";
   } } }
   
ycans
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Sep 11, 2012 4:50 pm

Re: Query Select within a function

Postby NigelRen » Tue Sep 11, 2012 11:53 pm

In short - you can't do it that way.

You will have to use something like...

Code: Select all
$query = $this->query("SELECT username FROM ".DBTBLE." WHERE id = '$id'");
foreach ($query as $row) {
        return $row['username'] ." was deleted.";
}


Although I'm not sure how selecting a row and finding it will indicate it's deleted - but that's or you to decide.
NigelRen
php-forum Active User
php-forum Active User
 
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am

Re: Query Select within a function

Postby ycans » Wed Sep 12, 2012 4:10 am

Thanks for your response, sorry I didn't explain. I wanted to find the users email address so that they could be notified via email. I have no problem deleting the user. The email works fine, but I' still unable to retrieve the email address of the users. Do I need to set up a separate function to request this?

Code: Select all
function delete_user($_POST, $delete) {
   
   if(isset($delete)) {
   
      $check = $_POST['check'];
      $id = $_POST['id'];
      $email_address = $this->query("SELECT email_address FROM ".DBTBLE." WHERE id = '$id'");
      
      Delete_Account($email_address, $id);
      }}
ycans
New php-forum User
New php-forum User
 
Posts: 2
Joined: Tue Sep 11, 2012 4:50 pm

Re: Query Select within a function

Postby NigelRen » Wed Sep 12, 2012 5:07 am

The code you posted shows exactly the same problems as before - so if you follow the same suggestion as before, changing the appropriate parts, then you can get at the e-mail address.

If you look at the manual, you should see that query returns some form of result set - not an individual columns value.
NigelRen
php-forum Active User
php-forum Active User
 
Posts: 450
Joined: Fri Aug 05, 2011 9:53 am


Return to mySQL & php coding

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron