• 0

Php error help


Question

I have came across an error on my website, I wondered if anyone could help? I have the word Banks O'Dee in my php database ($buyfrom) and I receive this error when accessing my page..

select location from opposition_team where opposition='Banks O'Dee'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 'Dee'' at line 1

And this is the code I use on my php page for this part


$q4="select location from opposition_team where opposition='$buyfrom'";
$qr4=mysql_query($q4,$ccppdbc)or die($q4.mysql_error());
$r4=mysql_fetch_object($qr4);
$location=$r4->location;
[/CODE]

Can I not make this work without changing the word Banks O'Dee so it doesn't have the [b]' [/b]as I believe that is whats causing it.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

php has a function addslashes you could do something like:

$q4="select location from opposition_team where opposition='" . addslashes($buyfrom) . "'";
$qr4=mysql_query($q4,$ccppdbc)or die($q4.mysql_error());
$r4=mysql_fetch_object($qr4);
$location=$r4->location;[/CODE]

Link to comment
Share on other sites

  • 0

You need to properly escape the value in the $buyfrom variable.


$buyfrom = mysql_escape_string($buyfrom);
/* code detailed above follows */
[/CODE]

Link to comment
Share on other sites

  • 0

You should always escape or validate variables before they are used in any form of query - leads to many issues and it's a great big security hole.

Link to comment
Share on other sites

  • 0

I have came across an error on my website, I wondered if anyone could help? I have the word Banks O'Dee in my php database ($buyfrom) and I receive this error when accessing my page..

And this is the code I use on my php page for this part


$q4="select location from opposition_team where opposition='$buyfrom'";
$qr4=mysql_query($q4,$ccppdbc)or die($q4.mysql_error());
$r4=mysql_fetch_object($qr4);
$location=$r4->location;
[/CODE]

Can I not make this work without changing the word Banks O'Dee so it doesn't have the [b]' [/b]as I believe that is whats causing it.

Hi marklcfc,

What others have said is true about needing to escape your queries, I would recommend the using [b]mysql_real_escape_string[/b] function as [b]mysql_escape_string[/b] has been depreciated since [b]php 5.3[/b] in [u]June 30, 2009[/u].

Since this is an issue showing up in your site there is also the possibility that there are other [i]unescaped [/i]queries in your website application. I would recommend updating your code to use [b]PDO and prepared statements[/b] to help increase the security of your site and help protect from SQL Injection.

I would also recommend validating your data before accepting it from the end user or from a storage system and using something like HTMLPurifier to run your data through to assist with XSS protection.

Please take some time checking out the [b]OWASP Top 10[/b] to get a good idea of some of the security issues to look into. This should help you review your current site security level and see where it needs to be improved.

Link to comment
Share on other sites

  • 0

I used that escape line, the page loads but it appears as Banks O\'Dee instead of Banks O'Dee

Firey's works well though..

To fix this issue, you just need to run stripslashes on your strings before echoing them out:


$buyfrom = mysql_real_escape_string($buyfrom);
$q4="select location from opposition_team where opposition='" . $buyfrom . "'";
$qr4=mysql_query($q4,$ccppdbc)or die($q4.mysql_error());
$r4=mysql_fetch_object($qr4);
$location=stripslashes($r4->location);
[/CODE]

The escaping is only necessary when inserting data into your database. Once you retrieve it, you can strip the slashes to return your data to normal. Hope that answers your questions!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.