473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Difference between local SQL and host SQL

nathj
938 Expert 512MB
Hi,

I have a problem with some SQL inside a php file. The tricky thing is that the code works just fine locally and I only have the problems when I load it to the host.

The code is used to check if the desired username exists in the database. If it does then the user is informed, at the time of entering the data, using an AJAX process. Part of this process call a php file (datacheck.php) with a query string that contains the item to check. The code that is the problem is this:
Expand|Select|Wrap|Line Numbers
  1. $lcItemToCheck = $_GET['tocheck']
  2. $lcCheckSQL = 
  3. "SELECT COUNT(*) AS test 
  4. FROM credential 
  5. WHERE userName = $lcItemToCheck" ;
  6. /* I have tried adding '' around the variable, I have tried ending the SQL at =" and adding the variable to it, both with and without ''. 
  7. The query ... userName = '" . $lcItemToCheck ."'"; seems to check the database for 'nathj07' which isn't there, I need to check for nathj07.
  8.  
  9. $result = mysql_query($lcCheckSQL);
  10.  
When the SQL is executed it returns the following error:

Expand|Select|Wrap|Line Numbers
  1.  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 '\'nathj07\'' at line 3
  2.  
nathj07 is what was entered in the form. This item is the database, I added it for testing purposes and when I run this locally it works a treat.

Does anyone have any suggestions on what is causing the problem or how to fix it?

Many thanks
nathj
Jul 13 '07 #1
15 1848
mwasif
802 Expert 512MB
[PHP]$lcItemToCheck = mysql_real_escape_string($_GET['tocheck']);
$lcCheckSQL =
"SELECT COUNT(*) AS test
FROM credential
WHERE userName = '$lcItemToCheck'" ;

$result = mysql_query($lcCheckSQL) or die(mysql_error());[/PHP]

You must have to use single quotes around $lcItemToCheck, because you are comparing a string column. Look at mysql_real_escape_string(), you'll know why I used this.
Jul 13 '07 #2
nathj
938 Expert 512MB
[PHP]$lcItemToCheck = mysql_real_escape_string($_GET['tocheck']);
$lcCheckSQL =
"SELECT COUNT(*) AS test
FROM credential
WHERE userName = '$lcItemToCheck'" ;

$result = mysql_query($lcCheckSQL) or die(mysql_error());[/PHP]

You must have to use single quotes around $lcItemToCheck, because you are comparing a string column. Look at mysql_real_escape_string(), you'll know why I used this.
Thanks for that, it is most helpful.

However, the username is stored as nathj07 and I am now looking for 'nathj07' and so this comes back and says the item is not found when really it should be found.

Why would it work on my laptop under localhost but not on the server? (that's more a curiosity than a real question). Is there a way of getting this to return the correct result?

Many thanks
nathj
Jul 13 '07 #3
mwasif
802 Expert 512MB
  1. Make sure you are connecting to the correct database
  2. Make sure you have required data in the table
  3. Make sure you are using the exact column name (take care of case)
  4. echo the resultant query and execute that query online (in phpMyAdmin or any GUI) to verify the results.
  5. Show the code where you verifying the existence of the record
Jul 13 '07 #4
nathj
938 Expert 512MB

  1. Make sure you are connecting to the correct database
  2. Make sure you have required data in the table
  3. Make sure you are using the exact column name (take care of case)
  4. echo the resultant query and execute that query online (in phpMyAdmin or any GUI) to verify the results.
  5. Show the code where you verifying the existence of the record
Hi mwasif

1. Yep connecting to the right database and table
2. the data is in the table
3. Column name is correct allowing for case sensitivity
4. Here's the query:
Expand|Select|Wrap|Line Numbers
  1.          SELECT COUNT(*) AS test                  FROM credential                  WHERE userName = '\'nathj07\''
  2.  
5. The code to verify the existence of the record is:
Expand|Select|Wrap|Line Numbers
  1. if($result)
  2.         {                
  3.         while ($row = mysql_fetch_array($result))
  4.         {
  5.             $lnCount = $row['test'];
  6.             if ($lnCount >= 1)
  7.              {
  8.                  $lcToDisplay = $lcErrorText;
  9.              }
  10.         }
  11.         }
  12.         else
  13.         {
  14.         $lcToDisplay = mysql_error();
  15.         }
  16.  
  17.  
The variables are set under a control structure earlier in the procedure. Basically at the start all required data is labelled in red, when it is valid it is labelled in black. If, as in user name, it is already in the DB a specific warning in re is printed.


Thanks for all the help.
nathj
Jul 13 '07 #5
mwasif
802 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*) AS test
  2. FROM credential
  3. WHERE userName = '\'nathj07\''
Did you notice backslash and single quote (\') around nathj07? Why is it? Are you inserting single quotes in the form field? Are these single quotes are the part of username? If these are not the part of usename then debug the code to determine the cause of this.
Jul 13 '07 #6
nathj
938 Expert 512MB
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(*) AS test
  2. FROM credential
  3. WHERE userName = '\'nathj07\''
Did you notice backslash and single quote (\') around nathj07? Why is it? Are you inserting single quotes in the form field? Are these single quotes are the part of username? If these are not the part of usename then debug the code to determine the cause of this.
I was a bit curious about that I must admit. I have done a little bit of debugging (echo the item from the query string) and can see that it contains " ' " makrs around the string. I am not explcitally adding these when the user fills in the form.

What could cause their appearance and what can be done to correct this? I tried a strtreplace but withouit much luck, any ideas?

Thank you for al your help, I really appreciate it.
nathj
Jul 13 '07 #7
mwasif
802 Expert 512MB
Check your HTML form fields.
Jul 13 '07 #8
nathj
938 Expert 512MB
Check your HTML form fields.
Ok, here's the form field in question:
Expand|Select|Wrap|Line Numbers
  1. <input id="username" type="text" size="47" onchange="validateItem('usernamelabel',this.value,'Username:',5,false,true,3);" alt="Username, for use on the Forum" title="Username, for use on the Forum" />
  2.  
What could be the problem with this? I don't see anything that would add '' cahracters around the user entered value.

Thanks for all your help.
nathj
Jul 14 '07 #9
mwasif
802 Expert 512MB
<input id="username" type="text" size="47" onchange="validateItem('usernamelabel',this.value, 'Username: ',5,false,true,3);" alt="Username, for use on the Forum" title="Username, for use on the Forum" />
what is the function of validateItem()?
Where is the 'name' in the <input>?
Jul 14 '07 #10
nathj
938 Expert 512MB
what is the function of validateItem()?
Where is the 'name' in the <input>?
validateItem() is a java script function that, in this case, checks that the user name is between 8 and 16 characters and then checks the database to ensure it is not already in use. What do you mean by 'name'? I am testing the username on the form. It is during the JS function that the php I am having difficulty with is called. This is donoe using the HTTPRequest object that onstatechange calls the php that checks the database.

If I could ensure that the comparison either has " ' " present on both sides or missing on both sides that would be great.
(preferrably the latter)

Cheers
nathj
Jul 15 '07 #11
ak1dnar
1,584 Expert 1GB
validateItem() is a java script function that, in this case, checks that the user name is between 8 and 16 characters and then checks the database to ensure it is not already in use. What do you mean by 'name'? I am testing the username on the form. It is during the JS function that the php I am having difficulty with is called. This is donoe using the HTTPRequest object that onstatechange calls the php that checks the database.

If I could ensure that the comparison either has " ' " present on both sides or missing on both sides that would be great.
(preferrably the latter)

Cheers
nathj
Add name="username" to the html form element with id="username" as name attribute is the traditional HTML form elements identification.

Now before you post the data to the server side via HTTPRequest, print back the form element for double checking the post url.

try ,
Expand|Select|Wrap|Line Numbers
  1.  posturl = "username="+variable; 
  2. alert(posturl)
  3.  
then on server side get the user entered values to the script variables with

Expand|Select|Wrap|Line Numbers
  1.  $USERNAME = $_REQUEST['usernname'];
Then Pass it to Query String

[PHP]$lcCheckSQL =
"SELECT COUNT(*) AS test
FROM credential
WHERE userName ='$USERNAME' ";[/PHP]
Jul 16 '07 #12
nathj
938 Expert 512MB
=
Add name="username" to the html form element with id="username" as name attribute is the traditional HTML form elements identification.

Now before you post the data to the server side via HTTPRequest, print back the form element for double checking the post url.

try ,
Expand|Select|Wrap|Line Numbers
  1.  posturl = "username="+variable; 
  2. alert(posturl)
  3.  
then on server side get the user entered values to the script variables with

Expand|Select|Wrap|Line Numbers
  1.  $USERNAME = $_REQUEST['usernname'];
Then Pass it to Query String

[PHP]$lcCheckSQL =
"SELECT COUNT(*) AS test
FROM credential
WHERE userName ='$USERNAME' ";[/PHP]
Thanks for the help. However, (life is never simple is it?) I havetried the suggestion and it makes no difference. It seems to me that the entry in the database is without ' as you would expect but the variable passed across is enclosed in '. So when the code checks the database is says nathj07 does not equal 'nathj07' and so the desired username is valid.

The code at present (server side - as clled by onstatechange):

Expand|Select|Wrap|Line Numbers
  1. if(isset($_GET['check']) && isset($_GET['tocheck']))
  2.     {
  3.  
  4.         //establish connection - using dataobject.php did not work - for some reason the file was not available
  5.         $lvCon = mysql_connect('host', 'user', 'password');
  6.         //$lvCon = mysql_connect('host', 'user', 'password); 
  7.         if (!$lvCon)
  8.         {
  9.             die('Could not connect: ' . mysql_error());
  10.         }
  11.         mysql_select_db(database, $lvCon);
  12.         //mysql_select_db(database, $lvCon);
  13.  
  14.         $lnCheckType    = mysql_real_escape_string($_GET['check']);
  15.         $lcItemToCheck    = mysql_real_escape_string($_GET['tocheck']);    
  16.  
  17.         // branch the code
  18.         switch($lnCheckType)
  19.         {
  20.             case 5: // check for presence of username, will only ever be one match or no match
  21.                 $lcToDisplay = "Username:";
  22.                 $lcErrorText = "<span class='warninglabel'>Username in use:</span>";
  23.  
  24.                 $lcCheckSQL = 
  25.                 "SELECT COUNT(*) AS test 
  26.                 FROM credential 
  27.                 WHERE userName = '$lcItemToCheck'" ; 
  28.                 break;    
  29.  
  30.             case 2:// check for the presence of an email, this should be unique in the database
  31.                 $lcToDisplay = "E-Mail:"; 
  32.                 $lcErrorText = "<span class='warninglabel'>E-Mail already regisitered:</span>";
  33.                 $lcCheckSQL =
  34.                 "SELECT COUNT(*) AS test
  35.                 FROM eaddress
  36.                 WHERE eAddressType = 1 AND
  37.                 eAddress = '$lcItemToCheck'";
  38.                 break;
  39.         }    
  40.  
  41.         $result = mysql_query($lcCheckSQL);
  42.         if($result)
  43.         {                
  44.         while ($row = mysql_fetch_array($result))
  45.         {
  46.             $lnCount = $row['test'];
  47.             if ($lnCount >= 1)
  48.              {
  49.                  $lcToDisplay = $lcErrorText;
  50.              }
  51.         }
  52.         }
  53.         else
  54.         {
  55.         $lcToDisplay = mysql_error();
  56.         }
  57.  
  58.         echo $lcToDisplay;
  59.     }        
  60.  
You will see from this that I run a similar check on the email address as well. The theory is that once one works they will both work.

Originally I didi not have the 'mysql_real_escape_string' being used and the code worked a treat on localhost. However, I have loaded this to the test server and it doesn't work without this line, reporting an error in the SQL. However, with the lines in it simply doesn't find a match. It is most peculiar.

Any further suggestionons on this issue?

Many thanks
nathj
Jul 17 '07 #13
ak1dnar
1,584 Expert 1GB
Pass the URL parameters to ServerSideScript.php by executing its absolute URL of the script.
Expand|Select|Wrap|Line Numbers
  1. http://localhost/siteroot/ServerSideScript.php?check=somevalue&tocheck=someothervalue
And Let me know whether its working or Not.
Jul 17 '07 #14
nathj
938 Expert 512MB
Pass the URL parameters to ServerSideScript.php by executing its absolute URL of the script.
Expand|Select|Wrap|Line Numbers
  1. http://localhost/siteroot/ServerSideScript.php?check=somevalue&tocheck=someothervalue
And Let me know whether its working or Not.
Hi Ajaxrand,

Upon trying the absolute path approach I spotted that I had enclosed the query string parameter in ' so when extracting it it was setting the variable to 'nathj07'. I removed this and it works perfectly. I didn't need the absolute path.

However, had you not sugested it I don't think I would have closely examined that line of code at all. So many many thanks to you for that. It turns out it wasn't a SQL or PHP problem but a javascript problem.

I guess the important thing is that it works and that I have learnt something from this.

Thanks again for your help, I couldn't have done without you - that goes for everyone who helped.

Well, best go see who I can help now.
nathj
Jul 17 '07 #15
ak1dnar
1,584 Expert 1GB
Hi Ajaxrand,

Upon trying the absolute path approach I spotted that I had enclosed the query string parameter in ' so when extracting it it was setting the variable to 'nathj07'. I removed this and it works perfectly. I didn't need the absolute path.

However, had you not sugested it I don't think I would have closely examined that line of code at all. So many many thanks to you for that. It turns out it wasn't a SQL or PHP problem but a javascript problem.

I guess the important thing is that it works and that I have learnt something from this.

Thanks again for your help, I couldn't have done without you - that goes for everyone who helped.

Well, best go see who I can help now.
nathj
Well done! best of luck with your project....
-Ajaxrand
Jul 17 '07 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: steve | last post by:
Hi, I finally figured out the best way to synch local and remote script folders, containing many php script files. What I used to do before was try to ftp all the changed files, etc. which was...
3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
3
by: Pugi! | last post by:
On a freshly installed Fedora C3 (incl. webserver apache php mysql) i get the following problem when connecting to mysql through a browser (phpMyAdmin): : #2002 Can't connect to local MySQL...
13
by: Niki Kovacs | last post by:
Hi, I'm an Austrian writer living in Montpezat (South France). I just installed a local W3C validator on my machine (Slackware 10.1, local Apache server). It's accessible as...
1
by: Jonathan Lee | last post by:
Dear all I want to program implementation scan all of the IIS host or SqlServer host in the local network. But I only can search all of computer in the local network now. And how can I determind...
6
by: Brad | last post by:
I have a win2003 server workstation with multiple webs, each web has it's own ip address. In VS2005, if I select to open an existing web site, select Local IIS, the dialog correctly displays a...
4
by: Chuck Anderson | last post by:
My Web Host (Linux - Apache) recently upgraded to Php5. One thing lost in the upgrade was a global configuration that allowed me to place php.ini files in any directory and they would be read and...
5
by: Hooyoo | last post by:
Hi, here, How to get local machine name and IP address? Thanks.
5
by: =?Utf-8?B?SmVycnkgQw==?= | last post by:
I have a app that uses several membership/role providers. I can list these Providers with the code: Dim rootWebConfig1 As Configuration rootWebConfig1 =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.