FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Need help..... somthing wrong here??





salman_500
hey..

i have this code...

Code:
( <b>";
   
$tutcountcat = $tutcat3['name'];
$tutcountsql = mysql_query("SELECT COUNT (*) FROM tutorials WHERE category = '$tutcountcat'");
$tutcount = mysql_result($tutcountsql, 0);
if ($tutcount == 0){
echo "0";
}
else {
echo $tutcount;
}   
   echo "</b> )


pretty simple..... but when it is run.......

i get this...

Quote:
(
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\tutorials\tutorials.php on line 32
0 )


...well....... what am i doing rong??

thnx !
hexkid
salman_500 wrote:
what am i doing rong??

You're not validating the result of mysql_* functions.

Try this
Code:
$tutcountcat = $tutcat3['name'];
$tutcountsql = mysql_query("SELECT COUNT (*) FROM tutorials WHERE category = '$tutcountcat'");

########################
### validate the result of mysql_query()
if ($tutcountsql === false) {
  echo "There was an error in mysql_query().<br>\n";
  echo "The error was: ", mysql_error(), "<br>\n";
  echo "Script terminated.";
  die(1);
}

$tutcount = mysql_result($tutcountsql, 0);
if ($tutcount == 0){
echo "0";
}
else {
echo $tutcount;
}
salman_500
now i get this...

Quote:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\tutorials\tutorials.php on line 32
There was an error in mysql_query().
The error was: 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 'FROM 'tutorials' WHERE 'category' = 'test'' at line 1
Script terminated.
hexkid
salman_500 wrote:
Quote:
right syntax to use near 'FROM 'tutorials' WHERE 'category' = 'test''

Where did all the single quotes come from?
Perhaps you want to use backticks?

Why do you show a script but run another? Smile
JBotAlan
salman_500 wrote:
Code:
$tutcountsql = mysql_query("SELECT COUNT (*) FROM tutorials WHERE category = '$tutcountcat'");


I did this to myself, too. mysql_query needs both the query itself and the connection resource id that you got when you connected.

Example:
Code:

$conn = mysql_connect("localhost","root","password");
mysql_select_db("testdb", $conn);
$tutcountsql = mysql_query("SELECT COUNT (*) FROM tutorials WHERE category = '$tutcountcat'", $conn);


I'm assuming you know what to do with the resource identifier that mysql_query returns...

Post back if you need more help.

JBot[/code]
hexkid
JBotAlan wrote:
mysql_query needs both the query itself and the connection resource id that you got when you connected.


Sorry Smile

No, it doesn't. In the absence of the connection resource, mysql_query() will use the last opened connection (or try to open one itself). You must pass the connection resource to mysql_query() if you have more than one connection resource.
Code:
<?php
$conn1 = mysql_connect('host1', 'user1', 'pwd1') or die();
mysql_select_db('db1'); // use $conn1
$conn2 = mysql_connect('host22', 'user2', 'pwd2') or die();
mysql_select_db('db2'); // uses $conn2

mysql_query("select * from table2 limit 10"); // uses $conn2
mysql_query("select * from table1 limit 10", $conn1);
?>
salman_500
jaybotalan...

i added that conn thingy ...but still no good.....

and hexkid... error reporting only increases the shown errors....doesnt actually tell whats wrong....

dont you think there is somthing else rong...cos it says somthing about the mysql_result thingy..... i use this same kinda code for other counts in other places and they work fine.....

like this is one of em...

Code:
$get = mysql_query("SELECT COUNT(*) FROM pmessages WHERE touser = '$loggedinuser' AND unread='unread'");
$numbermessages = mysql_result($get, 0);
if ($numbermessages == 0)
{
echo ("<a href=\"account.php?sub_section=inbox\">You have <strong>0</strong> new messages !</a>");

}
else
{
echo "<a href=\"account.php?sub_section=inbox\">You have <strong>$numbermessages</strong> new messages</a><br><br>";

}
hexkid
salman_500 wrote:
dont you think there is somthing else rong

Yes I do.

The query in your first post is ok.
salman_500 wrote:
Code:
$tutcountsql = mysql_query("SELECT COUNT (*) FROM tutorials WHERE category = '$tutcountcat'");

The error reported is not consistent with your code
salman_500 wrote:
Quote:
The error was: 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 'FROM 'tutorials' WHERE 'category' = 'test'' at line 1


Your code has single quotes only around $tutcountcat; the reported error shows single quotes around tutorials, category and test (the $tutcountcat).

Either you are lying to me, or PHP is lying to you ...

If you're lying to me, please don't do that.
If PHP is lying to you, one way to find out why he's doing that is by verifying all error reported when error_reporting(E_ALL) is in effect.
salman_500
hexkid wrote:
salman_500 wrote:
dont you think there is somthing else rong

Yes I do.

The query in your first post is ok.
salman_500 wrote:
Code:
$tutcountsql = mysql_query("SELECT COUNT (*) FROM tutorials WHERE category = '$tutcountcat'");

The error reported is not consistent with your code
salman_500 wrote:
Quote:
The error was: 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 'FROM 'tutorials' WHERE 'category' = 'test'' at line 1


Your code has single quotes only around $tutcountcat; the reported error shows single quotes around tutorials, category and test (the $tutcountcat).

Either you are lying to me, or PHP is lying to you ...

If you're lying to me, please don't do that.
If PHP is lying to you, one way to find out why he's doing that is by verifying all error reported when error_reporting(E_ALL) is in effect.



ohh....no actually thats no one lying..... i posted the code here....then somthin came to my mind....aded single qoutes and then rana again...got error again....and posted that error here...... so in a sense its my mistake...

sorry... here is the real thing.....

Code:
( <b>";
   
$tutcountcat = $tutcat3['name'];
$tutcountsql = mysql_query("SELECT COUNT (*) FROM tutorials WHERE category = '$tutcountcat'");
$tutcount = mysql_result($tutcountsql, 0);

if ($tutcountsql === false) {
  echo "There was an error in mysql_query().<br>\n";
  echo "The error was: ", mysql_error(), "<br>\n";
  echo "Script terminated.";
  die(1);
}
if ($tutcount == 0){
echo "0";
}
else {
echo $tutcount;
}   
   echo "</b> )



and the error...

Quote:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Group\Apache2\htdocs\Solo Themes\modules\tutorials\tutorials.php on line 32
There was an error in mysql_query().
The error was: 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 '(*) FROM tutorials WHERE category = 'test'' at line 1
Script terminated.
hexkid
salman_500 wrote:
Quote:
The error was: 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 '(*) FROM tutorials WHERE category = 'test'' at line 1


Oops ... my bad regarding the first post of yours.

"select count (*) from ..." is an error. There must not be a space in "count (*)", the correct syntax is "select count(*) from ..."
salman_500
hexkid wrote:
salman_500 wrote:
Quote:
The error was: 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 '(*) FROM tutorials WHERE category = 'test'' at line 1


Oops ... my bad regarding the first post of yours.

"select count (*) from ..." is an error. There must not be a space in "count (*)", the correct syntax is "select count(*) from ..."


omg !!!

that was it !!! just on space !!! and that all that was causing me a page full of errors !!!!!!! im leavin no more spaces in my code from now on !!! lol Wink Razz


thnx sooo much hexkid !!! your the man ! Very Happy Laughing Wink Razz Laughing Smile Very Happy Cool
Related topics
Fantastico Error AGAIN!!!!!
alternatives to adsense
Simple full Sig tutorial
.zip files??
All you need to know about Religion Islam....
Is God a man?
Tellatale
Am I allergic to the sun ?
Free web design services!
Dunno what...but somthing wrong here .. :(
Chris Benoit & Family Found Dead
Apache
Opencart , Problem cart.php !!!!
Opencart problem cart.php
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.