loxy1914 Posted November 21, 2011 Share Posted November 21, 2011 Hi everybody. I have 3 years to deal with PHP, and I am facing an issue in this function: function searchByName($carTitle) { $searchQuery = mysql_query("SELECT * FROM car WHERE `title`= '$carTitle'") or die(mysql_error()) ; echo $searchQuery; while($results = mysql_fetch_assoc($searchQuery)){ print($results['title']); }} I am trying to print the executing query,also. Can someone help me with this? Quote Link to comment Share on other sites More sharing options...
Alexv Posted November 21, 2011 Share Posted November 21, 2011 I hope this helps. <?php function searchByName($carTitle) { $query = "SELECT * FROM car WHERE `title`= '$carTitle'"; $searchQuery = mysql_query($query) or die(mysql_error()) ; echo $query; while($results = mysql_fetch_assoc($searchQuery)){ print($results['title']); }} ?> Quote Link to comment Share on other sites More sharing options...
loxy1914 Posted November 21, 2011 Author Share Posted November 21, 2011 This really worked. Cheers! Nonetheless, I cannot understand why this happened... The query remains the same. Quote Link to comment Share on other sites More sharing options...
loxy1914 Posted November 21, 2011 Author Share Posted November 21, 2011 I am sorry for the duplicate post, but only the query worked: the return $results['title'] part, Not. Quote Link to comment Share on other sites More sharing options...
Alexv Posted November 21, 2011 Share Posted November 21, 2011 I am sorry for the duplicate post, but only the query worked: the return $results['t'] part, Not. I only changed 3 lines of code above the while loop. Your select/display logic is still the same. BTW your are printing result['title'] not returning it. Quote Link to comment Share on other sites More sharing options...
loxy1914 Posted November 21, 2011 Author Share Posted November 21, 2011 That`s true. I have decided to return and call it in another script. Should I avoid something,like this? Quote Link to comment Share on other sites More sharing options...
Alexv Posted November 21, 2011 Share Posted November 21, 2011 That`s true. I have decided to return and call it in another script. Should I avoid something,like this? It's ok to use a return. But in your case you will only return the first value from the database not the whole result set from the query. http://php.net/manual/en/function.return.php Quote Link to comment Share on other sites More sharing options...
loxy1914 Posted November 21, 2011 Author Share Posted November 21, 2011 Actually, this is not a problem because there is only one record in the DB. Is it prossible to return $results, and in another script to call : $car = new Car(); $searchResults = $car->searchByName($searchword); $cName=$searchResults['I_TITLE']; Quote Link to comment 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.