Jump to content

Passing string through a function using return


Recommended Posts

Hello everyone,


Pretty much what I am trying to do is run a query and based of the queries result, return a string - as of now it is turning up blank and cant seem to pass any strings through the function.


Current function:


function addNewBookmark($hash,$url,$title,$username){



$query = "INSERT INTO bookmarks (hash,url,title,username) VALUES '".md5($hash)."', '".$url."', '".$title."', '".$username."')";
      $result = mysql_query(query, $this->connection);
      $message = '';
      if(mysql_affected_rows($this->connection)!=1) {

        	$message = 'This URL already exists in the database!';
                return $message;
        $message = 'The URL was shared!';
        return $message;


The above code seems to run fine, just cant seem to pass a string...read that you can possibly use a __toString function?  but that keeps throwing errors when I try to work with it.


Thanks all!

Link to comment
Share on other sites

Oh, okay - my bad for the typo...thanks for the catch, though.


Maybe it is returning but perhaps I just may not be handling it correctly?


Essentially what I am trying to do is process that query and return the correct message to a script that will in turn take the $message and hand it off to a JavaScript function and echo it like so:


//connect to database class, process query and return message


/* JavaScript Code */

function displayMessage(str)
// Using pure JavaScript to create and style a div element
var d = document.createElement('div');

    	// Applying styles:
	width = '350px';
	height = '20px';
	top = '50%';
	left = '50%';
	margin = '-30px 0 0 -195px';
	backgroundColor = '#f7f7f7';
	border = '1px solid #ccc';
	color = '#777';
	padding = '20px';
	fontSize = '18px';
	fontFamily = '"Myriad Pro",Arial,Helvetica,sans-serif';
	textAlign = 'center';
	zIndex = 100000;
	textShadow = '1px 1px 0 white';

	MozBorderRadius = "12px";
	webkitBorderRadius = "12px";
	borderRadius = "12px";

	MozBoxShadow = '0 0 6px #ccc';
	webkitBoxShadow = '0 0 6px #ccc';
	boxShadow = '0 0 6px #ccc';

    // Adding the message passed to the function as text:

    // Appending the div to document

    // The message will auto-hide in 3 seconds:
	}	catch(error){}


// Adding a line that will call the JavaScript function:
echo 'displayMessage("'.$message.'");';


The query runs and seems to process correctly but the $message in the last couple lines  (echo 'displayMessage("'.$message.'");';) does not seem to echo the message but the JS pop-up will still occur....however, if I just put the straight query in this page itself, like so:


mysql_query("	INSERT INTO bookmarks (hash,url,title,username)

$message = '';
$message = 'This URL already exists in the database!';
$message = 'The URL was shared!';


it will show the appropriate message in the JS popup.  Do I need to "hand it over" in a different way or something?


Thanks for the help and hope the above makes sense.

Link to comment
Share on other sites

you are returning $message..but this variable is not available outside of the functions scope.. I think this may be where you are confused.. to get the returned value.. you muse set the function to a variable..


$message = $db->addNewBookmark($hash,$url,$title,$current_user); //arguments filled out appropriately.


now $message (or whatever you name the variable) will contain the value of your return statement..

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


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