U get the following error on my one page

Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /usr/www/users/stealtf/components/com_aclassf/top.php on line 111

This is my coding

<?php
/**
*  This file is part of Almond Classifieds Component for Joomla! (site:[url]http://www.almondsoft.com/j[/url] )
*  Copyright (C) 2008-2009 AlmondSoft.Com. All rights reserved.
*  [url]http://www.gnu.org/licenses/gpl-2.0.html[/url] GNU/GPL  
*/

include('funcs2.php');
 
function print_categories()
{ 
global $html_header, $html_footer, $categories,$ad_ind_width,$schopt,$ads_fields,$subcnmb,$javastl,$hghltcat,
$catl_width, $top_page_info,  $top_page_width, $topsearchfields, $top_leftcol,$catcols,$_REQUEST,$hdhctgrs,
$tbclr_1, $tbclr_2, $tbclr_3, $tbclr_4, $fntclr_1, $msg, $top_rightcol,$btmtophtml, $templ,
$top_bottom,$getcityval, $popctgr, $top_rightcol, $tmltads, $slctcntr, $indx_url, $use_ajax, $locations;

$top_rightcol="";

global $rmmbrloc;
if ($_REQUEST[$schopt]!=''){  

$cityhtml="&nbsp;<p class='pst1'>
<font class=stfntb> 
&nbsp; &nbsp; <b>".$ads_fields[$schopt][0].": ".$_REQUEST[$schopt]." </b></font>
&nbsp; &nbsp;&nbsp; &nbsp;  <font class='stfnt'>( <a href='{$indx_url}allc=1'>".$msg['all_cities']."</a> $rmmbrloc )</font> </font>
 
<br>
";

$citgtv1=$_REQUEST[$schopt];
$getcityval="$schopt=".checkgetfld($citgtv1)."&";
 
}

if ($_REQUEST[$schopt]!=""){$ltstcityv="&city=".checkgetfld($_REQUEST[$schopt]);}
else{$ltstcityv="";}


$grp_cnt=get_grpcnt();

$popctgr=""; $divkey1="0";  
foreach ($categories as $key => $value)
{
 
$aa1=split("_",$key);

if ($aa1[0]=='newcolumn'){$divkey1="0";   $categ[$key]="</td><td valign='top' align=left>";}
else {
if($aa1[0] == 'title')
{   

$p_val1=""; if ($divkey1=="1"){$p_val1="<p class='pst1'>";}
$categ[$key]= " 
$p_val1<p class='pst1'>
<TABLE BORDER=0 WIDTH='90%' cellspacing=0 cellpadding=0  >
<TR><td class='ct_group'><a href='{$indx_url}".$getcityval."md=browse&mds=search&gmct=".$aa1[1]."'>
<font class='ttl_grp'>".$categories[$key].":</a></font> &nbsp; (".$grp_cnt[$aa1[1]].") 
</TD></TR></table>
";

}else 
{

if (($categories[$key][2] != 'h') and ($key != 'evntcl'))
{$divkey1="1";$categ[$key]=print_cat_name($key);}

}
}

}

include ($templ['top']); 
return;
}

function get_grpcnt()
{ 
global $categories, $sql_mct1, $table_ads, $grpcnt, $gkey2, $HTTP_GET_VARS;
 
foreach ($categories as $key => $value)
{
$aa1=split("_",$key);
if($aa1[0]== 'title'){$kk1=0; 
if ($sql_mct1!=""){

get_gcnt2();
 
}
$sql_mct1="";
}
if($kk1==1 and $key!="evntcl" and  $key!="evcl_rpl" and 
$categories[$key][2]!="h" and $aa1[0]!='newcolumn'){$sql_mct1=$sql_mct1." catname='$key' or";}
if($aa1[0]== 'title'){$kk1=1; $gkey2=$aa1[1];}
}

get_gcnt2();
 
return $grpcnt;
}

function get_gcnt2()
{ 
global $sql_mct1, $table_ads, $grpcnt, $gkey2, $HTTP_GET_VARS;

if ($HTTP_GET_VARS['city']!=""){ $city_query=" and city='".$HTTP_GET_VARS['city']."' ";};
 
$sql_mct1=preg_replace ("/or$/", "", $sql_mct1);
$sql_mct1="( $sql_mct1 ) and ";
$sql_query="select count(idnum) from $table_ads where $sql_mct1 visible=1 $city_query";
$sql_res=mysql_query("$sql_query"); 
$row=mysql_fetch_row($sql_res);
$grpcnt[$gkey2]=$row[0];

}

 
function print_cat_name($key)
{
global $categories, $fntclr_1, $msg, $subcnmb, $getcityval,$popctgr, $indx_url;

# $dtupd=get_date_update($key); if ($dtupd!=''){$dtupd=$msg['updated_t'].": ".$dtupd;}

$vhtml= "
<nobr><a href='".sturl_ct_top($key,$getcityval, "1")."' >
".$categories[$key][0]."</a> <font class='ct_count'>(".get_cat_count($key).")</font></nobr> <br>
";
return $vhtml;
}




function checkgetfld($value)
{
$value=ereg_replace(' ', '+', $value);
$value=ereg_replace('@', '%40', $value);
$value=ereg_replace('!', '%21', $value);
$value=ereg_replace("'", '%27', $value);
$value=ereg_replace("\"", '%27', $value);

return $value;
}


function get_total_count()
{
global $cat_fields, $table_ads, $ct, $page, $adsonpage, $html_header, 
$html_footer; 
 
global $sqlaflcl;

$sql_query="select count(idnum) from $table_ads where $sqlaflcl
visible=1 ";
if( !($sql_res=@mysql_query("$sql_query")))
{echo $html_header;
echo "
<center>
<font FACE='ARIAL, HELVETICA'  COLOR='#bb0000' size=-1><b>
Error in connecting to ads MySQL table <font color='#000099'>'$table_ads'</font>.
<br> Seems, this table is not created, 
<a href='createtb.php'>click here </a> to create this table.
</b></font></center>
";
echo $html_footer;
return;
}

$row=mysql_fetch_row($sql_res);
$count=$row[0];
return $count;
} 



?>

Recommended Answers

All 12 Replies

Hard to see which line is 111. Mark it.

Also, did you read the sticky post at the top of this forum on this topic ?

Sorry about that, I highlighted it, i did put the {code} deal but it still icoded it! thank you

Sorry for posting, I have actaully searched before I joined and nothing is that similar and parts that did look i was so lost cos I really am a super noob to this, hope you all forgive me

do this:

$sql_res=mysql_query("$sql_query") or die(mysql_error());

There's probably an error in your query.

Replace this:

$sql_res=mysql_query("$sql_query"); 
$row=mysql_fetch_row($sql_res);

With this:

$sql_res=mysql_query($sql_query);
if (!$sql_res) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row=mysql_fetch_row($sql_res);

The die method(above post) could be used instead of the if statement, its a matter of personal preference, tho the die may be milliseconds faster
Your issue could be as simple as a SQL error, when $sql_res is failing you will receive an error on mysql_fetch_row.

Perhaps add a LIMIT 1 to the end of the query. Are you sure that there aren't more than 1 result being returned by the query?

Replace this:

$sql_res=mysql_query("$sql_query"); 
$row=mysql_fetch_row($sql_res);

With this:

$sql_res=mysql_query($sql_query);
if (!$sql_res) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$row=mysql_fetch_row($sql_res);

The die method(above post) could be used instead of the if statement, its a matter of personal preference, tho the die may be milliseconds faster
Your issue could be as simple as a SQL error, when $sql_res is failing you will receive an error on mysql_fetch_row.

Perhaps add a LIMIT 1 to the end of the query. Are you sure that there aren't more than 1 result being returned by the query?

Thank you very much, I have copied the coding but am getting this error now Could not run query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') and visible=1' at line 1 when I use the die method I get this You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') and visible=1' at line 1 Very new to php and I didnt make the coding, its a app. line one doesnt make sense to be incorrect since I know thats normal coding. <?php

also I know the syntax issue is with this line

$sql_query="select count(idnum) from $table_ads where $sql_mct1 visible=1 $city_query";

and I tried to add AND there but nothing

Line 1 is referring to the first line of the query, not php. This is a MySQL error, not a php error. There is something wrong with the SQL query.

It seems that the issue is in the variable, $sql_mct1. Would you mind posting that code up?

Line 1 is referring to the first line of the query, not php. This is a MySQL error, not a php error. There is something wrong with the SQL query.

It seems that the issue is in the variable, $sql_mct1. Would you mind posting that code up?

Thank you so much, i guess I am posting on the wrong forum, but I appreciate all the help, if any of this works I will post it on the mysql pages to help others. code is below

function get_gcnt2()
{ 
global $sql_mct1, $table_ads, $grpcnt, $gkey2, $HTTP_GET_VARS;

if ($HTTP_GET_VARS['city']!=""){ $city_query=" and city='".$HTTP_GET_VARS['city']."' ";};
 
$sql_mct1=preg_replace ("/or$/", "", $sql_mct1);
$sql_mct1="( $sql_mct1 ) and ";
$sql_query="select count(idnum) from $table_ads where $sql_mct1 visible=1 $city_query";
$sql_res=mysql_query("$sql_query"); 
$row=mysql_fetch_row($sql_res);
$grpcnt[$gkey2]=$row[0];

}

You should not use $HTTP_GET_VARS, use $_GET instead. You also don't need to include that in the globals, $_GET is a superglobal so you don't need to use global to use it.

The issue is still with the $sql_mct1
Perhaps echo out the $sql_mct1 before the query to see what it is. When i see the value of $sql_mct1 I can of more help.

Possibility that the preg_replace is causing your problem, not sure tho without knowing the value of $sql_mct1.

I cleaned the code a little and added in some debug for you.

function get_gcnt2()
{
	global $sql_mct1, $table_ads, $grpcnt, $gkey2;

	$city_query='';
	if (isset($_GET['city']) && $_GET['city']) { 
		$city_query = " AND city='".$_GET['city']."' ";
	}

	$sql_mct1 = preg_replace("/or$/", "", $sql_mct1);
	$sql_query = "SELECT COUNT(idnum) FROM $table_ads WHERE $sql_mct1 AND visible = 1 $city_query";
	$sql_res = mysql_query($sql_query);
	if (!$sql_res) {
		$errID = mysql_errno();
		$errEr = mysql_error();
		die('There was an error with SQL: '.(isset($errID) ? $errID.': '.$errEr : $errEr));
	}
	$row=mysql_fetch_row($sql_res);
	
	if ($row) {
		$grpcnt[$gkey2]=$row[0];
	}
}

Wow that was a hectic clean up thank you. I am getting a new error

Parse error: syntax error, unexpected $end in /usr/www/users/stealtf/components/com_aclassf/top.php on line 164

that line being ?> So at least at this point the error has been more defined and from what I understand this has gone back to being a good ol PHP error and somewhere on the coding I am missing a closed Bracket. Gosh I have to go through all the coding :( not going to be fun.

Make sure there is no white space after the ?>

hi,
you write below one

if($sql_res!="")
{
$row=mysql_fetch_row($sql_res);
$grpcnt[$gkey2]=$row[0];
}

instead of

$row=mysql_fetch_row($sql_res);
$grpcnt[$gkey2]=$row[0];


then it won't give u any error

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.