Forum Moderators: coopster

Message Too Old, No Replies

a simple query..

well, should be simple!

         

henry0

3:33 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I cannot pinpoint on my error
the following line keeps telling me:
No results found etc.

<<<
$numresults = mysql_query("SELECT category FROM stories where category = ag '%". $query ."%' ");
>>>
category is a field
stories is the table's name
and ag is a category populating category field among other categories
so the idea is to get all "ag"

thanks
Regards

justageek

4:04 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Will find all exact matches for 'ag':

$numresults = mysql_query("SELECT category FROM stories where category = 'ag'");

But it looks like you are doing a like 'ag' so try:

$numresults = mysql_query("SELECT category FROM stories where category = '%ag". $query ."%' ");

But if they all start with 'ag' try this although I have never tried like as a sub:

$numresults = mysql_query("SELECT category FROM stories where category = 'ag%". $query ."%' ");

henry0

5:19 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks
it seems that the closer is somewhere around of your code posted in your answer
<<

$numresults = mysql_query("SELECT category FROM stories where category = '%ag". $query ."%' ");

But if they all start with 'ag' try this although I have never tried like as a sub:

$numresults = mysql_query("SELECT category FROM stories where category = 'ag%". $query ."%' ");
>>

whch show the building of the page but not anything out of the DB at least no more "No results found .."

yes there is a field named category and among many categories one reads " ag "
so that is all the "ag" related I expect to display

here is the rest of amy code that works well under other DB
of course spelling and DB are matching
the requirements

what's not showing is the connect top atop code.

<<<
if (!($limit)){
$limit = 10;} // Default results per-page.
if (!($page)){
$page = 0;} // Default page value.
$numresults = mysql_query("SELECT category FROM stories where category like '%ag". $query ."%' ");

// the query.
$numrows = mysql_num_rows($numresults); // Number of rows returned from above query.
if ($numrows == 0){
echo("No results found matching your query - $query");
exit();}

$pages = intval($numrows/$limit); // Number of results pages.

// $pages now contains int of pages, unless there is a remainder from division.

if ($numrows%$limit) {
$pages++;} // has remainder so add one page

$current = ($page/$limit) + 1; // Current page number.

if (($pages < 1) ¦¦ ($pages == 0)) {
$total = 1;} // If $pages is less than one or equal to 0, total pages is 1.

else {
$total = $pages;} // Else total pages is $pages value.

$first = $page + 1; // The first result.

if (!((($page + $limit) / $limit) >= $pages) && $pages!= 1) {
$last = $page + $limit;} //If not last results page, last result equals $page plus $limit.

else{
$last = $numrows;} // If last results page, last result equals total number of results.

//escape from PHP mode.
?>
<html>
<head>
<title>Search Results for <?=$query?></title>
</head>
<body>

<table width="100%" border="2" bgcolor="#ffffcc">
<tr>
<td align="left" colspan="4">
Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b>

&nbsp;&nbsp;&nbsp;&nbsp;Page <b><?=$current?></b> of <b><?=$total?></b>

&nbsp;&nbsp;&nbsp;&nbsp;Results per-page: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5"><b>5</b></a> ¦ <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10"><b>10</b></a>
</td>
</tr>
<!-- </table> -->
<?
//Go back into PHP mode.

// Now we can display results.
$numresults = mysql_query("SELECT category FROM stories where category like '%ag". $query ."%' ORDER BY bussiness_name ASC LIMIT $page, $limit");

while ($data = mysql_fetch_array($results))
{
?>

<tr>
<td width="100% align="top" valign="left" colspan="1"><P><font color="#cccccc" size="4"><I>AGRICULTURE & FOOD</I></font>
</td></tr>
<tr>
<td width="100%" align="left" valign="top"><font color="#990000" size="3">Business Name</font>:&nbsp;<?=$data["business_name"]?></font></td>
<tr>
<td width="100%" align="left" valign="top"><font color="#990000" size="3">Newsletter Name</font>:&nbsp;<?=$data["newsletter_name"]?></font></td>
<tr>
<td width="33%" align="left" valign="top"><font color="#990000" size="3">Newsletter</font>:&nbsp;<br>
<?=$data["newsletter_text"]?></font></td>

<tr>
<td width="100%" colspan="1">
<?
{ // If last page don't give next link.
$next_page = $page + $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\"><align=left><font size=3>Next: Click here to jump to next page</a></font></hr>\n");}

?>
</tr>
</td>

<?
}
?>
<align="left">
<?
if ($page!= 0) { // Don't show back link if current page is first page.
$back_page = $page - $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a> \n");}

for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it.
{
$ppage = $limit*($i - 1);
if ($ppage == $page){
echo("<b><hr size=5 align=left width=400>$i</b></hr> \n");} // If current page don't give link, just text.
else{
echo("<font size=4><a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a></font> \n");}
}

if (!((($page+$limit) / $limit) >= $pages) && $pages!= 1) { // If last page don't give next link.
$next_page = $page + $limit;
echo("<a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\"><hr size=5 align=left width=400><font size=5>next</a></font></hr>\n");}
?>

justageek

6:18 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are only selecting the category. Either select the other fields you want or select * them.

Change it here:

// Now we can display results.
$numresults = mysql_query("SELECT * FROM stories where category like '%ag". $query ."%' ORDER BY bussiness_name ASC LIMIT $page, $limit");

You really should only pull the results you want so maybe this would be better:

// Now we can display results.
$numresults = mysql_query("SELECT business_name, newsletter_name, newsletter_text FROM stories where category like '%ag". $query ."%' ORDER BY bussiness_name ASC LIMIT $page, $limit");

JAG

henry0

6:35 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JAG thanks a lot
I will soon report

henry0

7:01 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jag sorry
not there yet
this is as it looks, note: No% with " ag " since this is the real full name.
<<
$numresults = mysql_query("SELECT business_name, newsletter_name, newsletter_text FROM stories where category like 'ag". $query ."%' ");

>>
it displays the page ordering system but no data
and no DB error

I doubled check all DB nields anme and array names
I cannot find a reason
the code is pretty straight forward
and makes sense so?

daisho

7:15 pm on Sep 14, 2003 (gmt 0)

10+ Year Member



In sql % is a wildcard. This really don't have a meaning other than a literal '%' when using '='. You have to use the keywork 'like'

ie:

"select categories from tblcat where name like 'ag%".$query."%'";

or something close to that depending on what you are looking for.

daisho.

justageek

7:22 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe start checking some things.

Try this:

$numresults = mysql_query("SELECT business_name, newsletter_name, newsletter_text FROM stories");
while($row = mysql_fetch_assoc($numresults)){
echo $row['business_name'];
echo $row['newsletter_name'];
echo $row['newsletter_text'];
}

Just to see if you are getting data.

If you are then do this:

$numresults = mysql_query("SELECT business_name, newsletter_name, newsletter_text FROM stories where category like '%ag%'");
while($row = mysql_fetch_assoc($numresults)){
echo $row['business_name'];
echo $row['newsletter_name'];
echo $row['newsletter_text'];
}

If you are getting data then there is something wrong with the call to the db I'm guessing. Try those first. Just to make sure you can get some data.

JAG

henry0

8:14 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JAG, again thank you for your help

yes I get my data in both cases
but as mentioned earlier I do not get error in DB conn
I am loking at it again but do not see to see the light!

justageek

8:29 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Maybe the quotes are screwing something up. I always assign my query to a variable. Try this:

$query = "SELECT business_name, newsletter_name, newsletter_text FROM stories where category like 'ag". $query ."%'";

$numresults = mysql_query($query);

henry0

8:44 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jag
I has to come from the display script

why well.... I made the script working very well in another section

but here something is blocking the display
I sent you a mail with the URL
thanks

henry0

9:06 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can prove it
I went to the code that works well in another section
and deleted all the array area
only left the first HTML title
well it did show fine in the browser
so it has to be related to the array display area
as you see (from the URL) not even html text shows

henry0

9:21 pm on Sep 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



JAG
it's over

the whole thing forced me to re re re read my code
I had once
$results
and once
$numresults ....

thanks for the help in the query area
hope I will be able to help in return
Henry