doforumda Posted September 6, 2009 Share Posted September 6, 2009 the following code does not echo the result from database. when i pass an argument to find_jobs then it should run the query in find_jobs function which is above the if statement and echo it out by the following little code foreach($jobs as $job) { echo $job['title'].'<br>'; } which you can find the above code at the end of the following code. when i pass nothing to find_jobs function the it should run the query in if statement. but it is not echoing anything. how this problem can be solved? <?php function db_connect() { $connection = mysql_pconnect("localhost","username","pass"); if(!$connection) { return false; } if(!mysql_select_db("database",$connection)) { return false; } return $connection; } function db_result_to_array($result) { $res_array = array(); for($count = 0; $row = mysql_fetch_array($result); $count++) { $res_array[$count] = $row; } return $res_array; } function find_jobs($query_str = null) { db_connect(); $select = "SELECT jobs.id, jobs.location, jobs.title, jobs.company, jobs.description, jobs.url "; $from = "FROM jobs "; $where = "WHERE jobs.id > 0 AND DATE_ADD( created_at, INTERVAL 30 DAY ) > NOW() "; $order = " ORDER BY jobs.id DESC"; if(!empty($query_str)) { $select .= sprintf(", match(title,location,description) against ('%s' IN BOOLEAN MODE) as relevance ", mysql_real_escape_string($query_str) ) or die(mysql_error()); $where .= sprintf(" and match(title,location,description) against ('%s' IN BOOLEAN MODE) ", mysql_real_escape_string($query_str) ) or die(mysql_error()); $order = "ORDER BY relevance DESC "; } $query = $select.$from.$where.$order; $result = mysql_query($query); $result = db_result_to_array($result); return $result; } $jobs = find_jobs(); foreach($jobs as $job) { echo $job['title'].'<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/ Share on other sites More sharing options...
sKunKbad Posted September 6, 2009 Share Posted September 6, 2009 what does var_dump($jobs); output ? var_dump($jobs); Quote Link to comment https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/#findComment-913468 Share on other sites More sharing options...
doforumda Posted September 6, 2009 Author Share Posted September 6, 2009 i dont understand which var_dump($jobs); i cant see any such thing my code Quote Link to comment https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/#findComment-913471 Share on other sites More sharing options...
BloodyMind Posted September 6, 2009 Share Posted September 6, 2009 I suggest you echo $query and copy and paste it in phpMyAdmin to see what it does Quote Link to comment https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/#findComment-913474 Share on other sites More sharing options...
sKunKbad Posted September 6, 2009 Share Posted September 6, 2009 i dont understand which var_dump($jobs); i cant see any such thing my code I was suggesting putting that in your code, right before your foreach statement. If your query is even returning anything at all, you would be able to see it. Quote Link to comment https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/#findComment-913475 Share on other sites More sharing options...
doforumda Posted September 6, 2009 Author Share Posted September 6, 2009 it returns this array(0) { } Quote Link to comment https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/#findComment-913478 Share on other sites More sharing options...
doforumda Posted September 6, 2009 Author Share Posted September 6, 2009 i dont understand which var_dump($jobs); i cant see any such thing my code I was suggesting putting that in your code, right before your foreach statement. If your query is even returning anything at all, you would be able to see it. i echo query and it returns the correct query "SELECT jobs.id, jobs.location, jobs.title, jobs.company, jobs.description, jobs.url FROM jobs WHERE jobs.id > 0 AND DATE_ADD( created_at, INTERVAL 30 DAY ) > NOW() ORDER BY jobs.id DESC" then i paste it in phpmyadmin and it returns MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0006 sec ) but there are 7 rows in db Quote Link to comment https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/#findComment-913482 Share on other sites More sharing options...
sKunKbad Posted September 6, 2009 Share Posted September 6, 2009 So you have a bad query. I really can't help you with your complex query, but when you figure out what is wrong, you will have your problem solved. Maybe head over to the MySQL forum and ask... Quote Link to comment https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/#findComment-913484 Share on other sites More sharing options...
bundyxc Posted September 6, 2009 Share Posted September 6, 2009 What's the created_at supposed to do? I've come to the conclusion that it must be a field... but what does it contain? By the way, your SQL intimidated me (and sKunKbad, haha), so I chopped it up a little: SELECT `jobs.id`, `jobs.location`, `jobs.title`, `jobs.company`, `jobs.description`, `jobs.url` FROM `jobs` WHERE `jobs.id` > 0 AND DATE_ADD(created_at, INTERVAL 30 DAY) > NOW() ORDER BY `jobs.id` DESC I'm assuming that the problem is in the 5th line, since that's the only line that I don't completely understand. Quote Link to comment https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/#findComment-913496 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.