tec-4 Posted October 27, 2011 Share Posted October 27, 2011 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; } else $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! Quote Link to comment https://forums.phpfreaks.com/topic/249920-passing-string-through-a-function-using-return/ Share on other sites More sharing options...
AyKay47 Posted October 27, 2011 Share Posted October 27, 2011 1. you are forgetting your variable symbol $ $result = mysql_query($query, $this->connection); 2. the return should work.. other than above, i don't see any other issues Quote Link to comment https://forums.phpfreaks.com/topic/249920-passing-string-through-a-function-using-return/#findComment-1282728 Share on other sites More sharing options...
tec-4 Posted October 27, 2011 Author Share Posted October 27, 2011 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: <?php //connect to database class, process query and return message $db->addNewBookmark($hash,$url,$title,$current_user); ?> /* JavaScript Code */ function displayMessage(str) { // Using pure JavaScript to create and style a div element var d = document.createElement('div'); with(d.style) { // Applying styles: position='fixed'; 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'; } d.setAttribute('onclick','document.body.removeChild(this)'); // Adding the message passed to the function as text: d.appendChild(document.createTextNode(str)); // Appending the div to document document.body.appendChild(d); // The message will auto-hide in 3 seconds: setTimeout(function(){ try{ document.body.removeChild(d); } catch(error){} },3000); } <?php // 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) VALUES ( '".md5($_GET['url'])."', '".$_GET['url']."', '".$_GET['title']."', '".$current_user."' )"); $message = ''; if(mysql_affected_rows($link)!=1) { $message = 'This URL already exists in the database!'; } else $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. Quote Link to comment https://forums.phpfreaks.com/topic/249920-passing-string-through-a-function-using-return/#findComment-1282779 Share on other sites More sharing options...
AyKay47 Posted October 27, 2011 Share Posted October 27, 2011 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.. Quote Link to comment https://forums.phpfreaks.com/topic/249920-passing-string-through-a-function-using-return/#findComment-1282781 Share on other sites More sharing options...
tec-4 Posted October 27, 2011 Author Share Posted October 27, 2011 Ah! Yes, that makes a lot of sense...it is working now. Still trying to wrap my head around quite a bit of this stuff lol Thanks for the help, AyKay47! Quote Link to comment https://forums.phpfreaks.com/topic/249920-passing-string-through-a-function-using-return/#findComment-1282791 Share on other sites More sharing options...
AyKay47 Posted October 27, 2011 Share Posted October 27, 2011 not a problem.. please mark this as solved.. and let us know if you have any other issues in the future.. Quote Link to comment https://forums.phpfreaks.com/topic/249920-passing-string-through-a-function-using-return/#findComment-1282797 Share on other sites More sharing options...
tec-4 Posted October 27, 2011 Author Share Posted October 27, 2011 Sounds good. Thanks again. Apparently been a long day...took me like 10 minutes to find that darn Topic Solved button! lol Quote Link to comment https://forums.phpfreaks.com/topic/249920-passing-string-through-a-function-using-return/#findComment-1282801 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.