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? Link to comment https://forums.phpfreaks.com/topic/251576-resource-id7/ 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']); }} ?> Link to comment https://forums.phpfreaks.com/topic/251576-resource-id7/#findComment-1290195 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. Link to comment https://forums.phpfreaks.com/topic/251576-resource-id7/#findComment-1290199 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. Link to comment https://forums.phpfreaks.com/topic/251576-resource-id7/#findComment-1290201 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. Link to comment https://forums.phpfreaks.com/topic/251576-resource-id7/#findComment-1290204 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? Link to comment https://forums.phpfreaks.com/topic/251576-resource-id7/#findComment-1290205 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 Link to comment https://forums.phpfreaks.com/topic/251576-resource-id7/#findComment-1290209 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']; Link to comment https://forums.phpfreaks.com/topic/251576-resource-id7/#findComment-1290213 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.