Hello,

I know I shouldn't use mysql_.
So I have a mysql_query which updates my table (for a hit counter).
However, when I open the page, I get to see 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 '1' WHERE shorturl='example'' at line 1

My code:

<?php
include ('config.inc.php');

$connection = mysql_connect($host, $user, $password);

mysql_select_db($database, $connection);

$shorturl = $_GET['shorturl'];
$shorturl = rtrim($shorturl, '/');
$query = mysql_query('SELECT longurl,count FROM `' . $prefix . 'urls` WHERE shorturl=\'' . $shorturl . '\'');
$array = mysql_fetch_array($query);
$num_rows = mysql_num_rows($query);
if($num_rows > 0) {
    mysql_query('UPDATE `' . $prefix . 'urls` SET count=\'' . $array['count']+1 . '\' WHERE shorturl=\'' . $shorturl . '\'');
    echo mysql_error();
    //header('Location: ' . $array['longurl']);
} else {
    header('Location: http://short.sydcul.com/index.php?message=' . rawurlencode('The short URL you submitted was not found. Why not create it below?'));
}
mysql_close($connection);
?>

The problem is at line 14. I commented out line 16 for debugging.
I checked everything, compared this to my other UPDATE-queries I used, nothing special. I also tried to remove the \' at the count (which is getting read as a ', I know I could use ", but I just prefer ' because of PHP reading it in a different way), but that will not solve it.

Recommended Answers

All 3 Replies

echo your query and see what is the result of query and can you tell me what value $prefix hold i think it has null value and echo your both qeury

Hmm, that's weird. When I echo the stuff in the query I get this:

1' WHERE shorturl='example'

The $prefix works, it contains:

short_

I'll have a look at the query.

I have no idea why, but this solved it:

<?php
include ('config.inc.php');

$connection = mysql_connect($host, $user, $password);

mysql_select_db($database, $connection);

$shorturl = $_GET['shorturl'];
$shorturl = rtrim($shorturl, '/');
$query = mysql_query('SELECT longurl,count FROM `' . $prefix . 'urls` WHERE shorturl=\'' . $shorturl . '\'');
$array = mysql_fetch_array($query);
$num_rows = mysql_num_rows($query);
if($num_rows > 0) {
    $count = $array['count']+1;
    mysql_query('UPDATE `' . $prefix . 'urls` SET count=\'' . $count . '\' WHERE shorturl=\'' . $shorturl . '\'');
    header('Location: ' . $array['longurl']);
} else {
    header('Location: http://short.sydcul.com/index.php?message=' . rawurlencode('The short URL you submitted was not found. Why not create it below?'));
}
mysql_close($connection);
?>

So I basically removed the $array-part from the query, and set it in a variable above it. Then I used the variable in the query.

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.