FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » You have an error in your SQL syntax;
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
You have an error in your SQL syntax; [message #174120] Sun, 22 May 2011 14:01 Go to next message
Co is currently offline  Co
Messages: 75
Registered: May 2011
Karma: 0
Member
Hi all,

I run a query based on the input in a listbox.
The query looks for users from a certain country.
When no users are from the chosen country I get a error message.
Is there no way to check if there are records in the query before
trying to output
so we only get a message saying: No records for this search.... or
something like it.

Regards
Marco
Re: You have an error in your SQL syntax; [message #174124 is a reply to message #174120] Sun, 22 May 2011 17:11 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 22-05-2011 16:01, Co wrote:
> Hi all,
>
> I run a query based on the input in a listbox.
> The query looks for users from a certain country.
> When no users are from the chosen country I get a error message.
> Is there no way to check if there are records in the query before
> trying to output
> so we only get a message saying: No records for this search.... or
> something like it.
>
> Regards
> Marco

I hope you know that there is a great MANUAL online at:
http://www.php.net

It has this great info:
http://php.net/manual/en/function.mysql-num-rows.php

--
Luuk
Re: You have an error in your SQL syntax; [message #174129 is a reply to message #174124] Sun, 22 May 2011 20:11 Go to previous messageGo to next message
Co is currently offline  Co
Messages: 75
Registered: May 2011
Karma: 0
Member
On 22 mei, 19:11, Luuk <L...@invalid.lan> wrote:
> On 22-05-2011 16:01, Co wrote:
>
>> Hi all,
>
>> I run a query based on the input in a listbox.
>> The query looks for users from a certain country.
>> When no users are from the chosen country I get a error message.
>> Is there no way to check if there are records in the query before
>> trying to output
>> so we only get a message saying: No records for this search.... or
>> something like it.
>
>> Regards
>> Marco
>
> I hope you know that there is a great MANUAL online at:http://www.php.net
>
> It has this great info:http://php.net/manual/en/function.mysql-num-rows.php
>
> --
> Luuk

Thanks Luuk,
I didn't know.
Anyways I tried an example from the page:

$num_rows = mysql_num_rows($sql2);
if($num_rows <> 0) {
while($row = mysql_fetch_array($sql2)) { ......

else {
print ("<p>No records for this search were found.</p>");
}

But I still get the error line saying:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL
result resource

Marco
Re: You have an error in your SQL syntax; [message #174130 is a reply to message #174129] Sun, 22 May 2011 20:21 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
Co wrote:
> On 22 mei, 19:11, Luuk <L...@invalid.lan> wrote:
>> On 22-05-2011 16:01, Co wrote:
>>
>>> Hi all,
>>> I run a query based on the input in a listbox.
>>> The query looks for users from a certain country.
>>> When no users are from the chosen country I get a error message.
>>> Is there no way to check if there are records in the query before
>>> trying to output
>>> so we only get a message saying: No records for this search.... or
>>> something like it.
>>> Regards
>>> Marco
>> I hope you know that there is a great MANUAL online at:http://www.php.net
>>
>> It has this great info:http://php.net/manual/en/function.mysql-num-rows.php
>>
>> --
>> Luuk
>
> Thanks Luuk,
> I didn't know.
> Anyways I tried an example from the page:
>
> $num_rows = mysql_num_rows($sql2);
> if($num_rows <> 0) {
> while($row = mysql_fetch_array($sql2)) { ......
>
> else {
> print ("<p>No records for this search were found.</p>");
> }
>
> But I still get the error line saying:
> Warning: mysql_num_rows(): supplied argument is not a valid MySQL
> result resource
>
> Marco

Then the original query failed.

If you stick in some debug like $query=(whatever your query is);
echo $query;

then you can cut and paste that into an interactive mysql session and
test it.
Re: You have an error in your SQL syntax; [message #174131 is a reply to message #174129] Sun, 22 May 2011 20:21 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/22/2011 4:11 PM, Co wrote:
> On 22 mei, 19:11, Luuk<L...@invalid.lan> wrote:
>> On 22-05-2011 16:01, Co wrote:
>>
>>> Hi all,
>>
>>> I run a query based on the input in a listbox.
>>> The query looks for users from a certain country.
>>> When no users are from the chosen country I get a error message.
>>> Is there no way to check if there are records in the query before
>>> trying to output
>>> so we only get a message saying: No records for this search.... or
>>> something like it.
>>
>>> Regards
>>> Marco
>>
>> I hope you know that there is a great MANUAL online at:http://www.php.net
>>
>> It has this great info:http://php.net/manual/en/function.mysql-num-rows.php
>>
>> --
>> Luuk
>
> Thanks Luuk,
> I didn't know.
> Anyways I tried an example from the page:
>
> $num_rows = mysql_num_rows($sql2);
> if($num_rows<> 0) {
> while($row = mysql_fetch_array($sql2)) { ......
>
> else {
> print ("<p>No records for this search were found.</p>");
> }
>
> But I still get the error line saying:
> Warning: mysql_num_rows(): supplied argument is not a valid MySQL
> result resource
>
> Marco

It means there was a problem with your SQL statement.

You need to check the results of mysql_query(), and if it is false,
determine why the SQL statement was in error.

You should ALWAYS check the return value of mysql_query() to ensure
worked correctly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: You have an error in your SQL syntax; [message #174133 is a reply to message #174129] Sun, 22 May 2011 20:35 Go to previous messageGo to next message
Luuk is currently offline  Luuk
Messages: 329
Registered: September 2010
Karma: 0
Senior Member
On 22-05-2011 22:11, Co wrote:
> On 22 mei, 19:11, Luuk <L...@invalid.lan> wrote:
>> On 22-05-2011 16:01, Co wrote:
>>
>>> Hi all,
>>
>>> I run a query based on the input in a listbox.
>>> The query looks for users from a certain country.
>>> When no users are from the chosen country I get a error message.
>>> Is there no way to check if there are records in the query before
>>> trying to output
>>> so we only get a message saying: No records for this search.... or
>>> something like it.
>>
>>> Regards
>>> Marco
>>
>> I hope you know that there is a great MANUAL online at:http://www.php.net
>>
>> It has this great info:http://php.net/manual/en/function.mysql-num-rows.php
>>
>> --
>> Luuk
>
> Thanks Luuk,
> I didn't know.
> Anyways I tried an example from the page:
>
> $num_rows = mysql_num_rows($sql2);
> if($num_rows <> 0) {
> while($row = mysql_fetch_array($sql2)) { ......
>
> else {
> print ("<p>No records for this search were found.</p>");
> }
>
> But I still get the error line saying:
> Warning: mysql_num_rows(): supplied argument is not a valid MySQL
> result resource
>
> Marco

The argument to mysql_num_rows() should not be a sql-statement, so
sending a parameter with the name $sql2 seems confusing

The argument you need to pass is the result from mysql_query();

like the example:
$result = mysql_query("SELECT * FROM table1", $link);
$num_rows = mysql_num_rows($result);




--
Luuk
Re: You have an error in your SQL syntax; [message #174134 is a reply to message #174133] Sun, 22 May 2011 20:56 Go to previous messageGo to next message
Co is currently offline  Co
Messages: 75
Registered: May 2011
Karma: 0
Member
On 22 mei, 22:35, Luuk <L...@invalid.lan> wrote:
> On 22-05-2011 22:11, Co wrote:
>
>
>
>
>
>
>
>
>
>> On 22 mei, 19:11, Luuk <L...@invalid.lan> wrote:
>>> On 22-05-2011 16:01, Co wrote:
>
>>>> Hi all,
>
>>>> I run a query based on the input in a listbox.
>>>> The query looks for users from a certain country.
>>>> When no users are from the chosen country I get a error message.
>>>> Is there no way to check if there are records in the query before
>>>> trying to output
>>>> so we only get a message saying: No records for this search.... or
>>>> something like it.
>
>>>> Regards
>>>> Marco
>
>>> I hope you know that there is a great MANUAL online at:http://www.php.net
>
>>> It has this great info:http://php.net/manual/en/function.mysql-num-rows.php
>
>>> --
>>> Luuk
>
>> Thanks Luuk,
>> I didn't know.
>> Anyways I tried an example from the page:
>
>> $num_rows = mysql_num_rows($sql2);
>> if($num_rows <> 0) {
>> while($row = mysql_fetch_array($sql2)) { ......
>
>> else {
>> print ("<p>No records for this search were found.</p>");
>> }
>
>> But I still get the error line saying:
>> Warning: mysql_num_rows(): supplied argument is not a valid MySQL
>> result resource
>
>> Marco
>
> The argument to mysql_num_rows() should not be a sql-statement, so
> sending a parameter with the name $sql2 seems confusing
>
> The argument you need to pass is the result from mysql_query();
>
> like the example:
> $result = mysql_query("SELECT * FROM table1", $link);
> $num_rows = mysql_num_rows($result);
>
> --
> Luuk

Guys,

I solved it.
I put this in the beginning:
$nr = mysql_num_rows($sql); // Get total of Num rows from the database
query
if ($nr){ // if we found any records we will proceed

If the query returns 0 records we pass all the code and just say "No
records found".

The only problem I still have is when I load the page first time I get
an error saying:
Notice: Undefined index
Somehow on this line:

if (($_POST['listByq'] == "newest_members")) {

listByq has not been defined.
Can I declare it at the top of the page?

Marco
Re: You have an error in your SQL syntax; [message #174135 is a reply to message #174134] Sun, 22 May 2011 21:16 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 5/22/2011 4:56 PM, Co wrote:
> On 22 mei, 22:35, Luuk<L...@invalid.lan> wrote:
>> On 22-05-2011 22:11, Co wrote:
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>> On 22 mei, 19:11, Luuk<L...@invalid.lan> wrote:
>>>> On 22-05-2011 16:01, Co wrote:
>>
>>>> > Hi all,
>>
>>>> > I run a query based on the input in a listbox.
>>>> > The query looks for users from a certain country.
>>>> > When no users are from the chosen country I get a error message.
>>>> > Is there no way to check if there are records in the query before
>>>> > trying to output
>>>> > so we only get a message saying: No records for this search.... or
>>>> > something like it.
>>
>>>> > Regards
>>>> > Marco
>>
>>>> I hope you know that there is a great MANUAL online at:http://www.php.net
>>
>>>> It has this great info:http://php.net/manual/en/function.mysql-num-rows.php
>>
>>>> --
>>>> Luuk
>>
>>> Thanks Luuk,
>>> I didn't know.
>>> Anyways I tried an example from the page:
>>
>>> $num_rows = mysql_num_rows($sql2);
>>> if($num_rows<> 0) {
>>> while($row = mysql_fetch_array($sql2)) { ......
>>
>>> else {
>>> print ("<p>No records for this search were found.</p>");
>>> }
>>
>>> But I still get the error line saying:
>>> Warning: mysql_num_rows(): supplied argument is not a valid MySQL
>>> result resource
>>
>>> Marco
>>
>> The argument to mysql_num_rows() should not be a sql-statement, so
>> sending a parameter with the name $sql2 seems confusing
>>
>> The argument you need to pass is the result from mysql_query();
>>
>> like the example:
>> $result = mysql_query("SELECT * FROM table1", $link);
>> $num_rows = mysql_num_rows($result);
>>
>> --
>> Luuk
>
> Guys,
>
> I solved it.
> I put this in the beginning:
> $nr = mysql_num_rows($sql); // Get total of Num rows from the database
> query
> if ($nr){ // if we found any records we will proceed
>
> If the query returns 0 records we pass all the code and just say "No
> records found".
>
> The only problem I still have is when I load the page first time I get
> an error saying:
> Notice: Undefined index
> Somehow on this line:
>
> if (($_POST['listByq'] == "newest_members")) {
>
> listByq has not been defined.
> Can I declare it at the top of the page?
>
> Marco

You can declare it, but rather you should use:

if (isset($_POST['listByq']))
$listByq = $_POST['listByq'];
else
$listByq = 'Some default value'; // Use an appropriate value

Which can also be shortened to:

$listByq = isset($_POST['listByq']) ? $_POST['listByq'] : 'Some default
value';

Then use $listByq from there on.

And BTW - ALWAYS validate any input from the user, including $_POST
values. There is no guarantee these values came from your form, for
instance. A hacker can easily provide whatever value he wants.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: You have an error in your SQL syntax; [message #174137 is a reply to message #174135] Sun, 22 May 2011 21:22 Go to previous message
Co is currently offline  Co
Messages: 75
Registered: May 2011
Karma: 0
Member
On 22 mei, 23:16, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 5/22/2011 4:56 PM, Co wrote:
>
>
>
>
>
>
>
>
>
>> On 22 mei, 22:35, Luuk<L...@invalid.lan>  wrote:
>>> On 22-05-2011 22:11, Co wrote:
>
>>>> On 22 mei, 19:11, Luuk<L...@invalid.lan>  wrote:
>>>> > On 22-05-2011 16:01, Co wrote:
>
>>>> >> Hi all,
>
>>>> >> I run a query based on the input in a listbox.
>>>> >> The query looks for users from a certain country.
>>>> >> When no users are from the chosen country I get a error message.
>>>> >> Is there no way to check if there are records in the query before
>>>> >> trying to output
>>>> >> so we only get a message saying: No records for this search.... or
>>>> >> something like it.
>
>>>> >> Regards
>>>> >> Marco
>
>>>> > I hope you know that there is a great MANUAL online at:http://www.php.net
>
>>>> > It has this great info:http://php.net/manual/en/function.mysql-num-rows.php
>
>>>> > --
>>>> > Luuk
>
>>>> Thanks Luuk,
>>>> I didn't know.
>>>> Anyways I tried an example from the page:
>
>>>> $num_rows = mysql_num_rows($sql2);
>>>> if($num_rows<>  0) {
>>>> while($row = mysql_fetch_array($sql2)) { ......
>
>>>> else {
>>>> print ("<p>No records for this search were found.</p>");
>>>> }
>
>>>> But I still get the error line saying:
>>>> Warning: mysql_num_rows(): supplied argument is not a valid MySQL
>>>> result resource
>
>>>> Marco
>
>>> The argument to mysql_num_rows() should not be a sql-statement, so
>>> sending a parameter with the name $sql2 seems confusing
>
>>> The argument you need to pass is the result from mysql_query();
>
>>> like the example:
>>> $result = mysql_query("SELECT * FROM table1", $link);
>>> $num_rows = mysql_num_rows($result);
>
>>> --
>>> Luuk
>
>> Guys,
>
>> I solved it.
>> I put this in the beginning:
>> $nr = mysql_num_rows($sql); // Get total of Num rows from the database
>> query
>> if ($nr){  // if we found any records we will proceed
>
>> If the query returns 0 records we pass all the code and just say "No
>> records found".
>
>> The only problem I still have is when I load the page first time I get
>> an error saying:
>> Notice: Undefined index
>> Somehow on this line:
>
>> if (($_POST['listByq'] == "newest_members")) {
>
>> listByq has not been defined.
>> Can I declare it at the top of the page?
>
>> Marco
>
> You can declare it, but rather you should use:
>
> if (isset($_POST['listByq']))
>    $listByq = $_POST['listByq'];
> else
>    $listByq = 'Some default value'; // Use an appropriate value
>
> Which can also be shortened to:
>
> $listByq = isset($_POST['listByq']) ? $_POST['listByq'] : 'Some default
> value';
>
> Then use $listByq from there on.
>
> And BTW - ALWAYS validate any input from the user, including $_POST
> values.  There is no guarantee these values came from your form, for
> instance.  A hacker can easily provide whatever value he wants.
>
> --
> ==================
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> ==================

Jerry,

thanks this works,
and thanks for the advises.

Marco
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Detecting Redirected Output
Next Topic: Stats comp.lang.php (last 7 days)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 22:00:52 GMT 2024

Total time taken to generate the page: 0.03085 seconds