Jump to content

need help in echoing data from database


doforumda

Recommended Posts

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>';
}


?>

Link to comment
https://forums.phpfreaks.com/topic/173294-need-help-in-echoing-data-from-database/
Share on other sites

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

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.

Archived

This topic is now archived and is closed to further replies.

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