I am writing a URL shortener... I've that one error which i cannot appear to eliminate. Personally i think its really apparent but I am not seeing it.

(I am a noob ) ) Here's the mistake:

'mysql_result(): supplied argument is not a valid MySQL result resource in <b>/home2/bythewa1/public_html/jbgc/func.inc.php</b> on line <b>23'

And here's my code from that page.

<?php
include("db.inc.php");

function is_min($url)
{

return(preg_match("/jbgc\.me/i", $url)) ? true : false; 

}


function gen_code()
{
$charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';    
return substr(str_shuffle($charset), 0, 6);
}



function exists($code) {
$code = mysql_real_escape_string($code);
$code_exists = mysql_query("SELECT COUNT('url_id') FROM 'mini_shortener' WHERE                  'code'= '$code' LIMIT 1");
return (mysql_result($code_exists, 0) == 1) ? true : false;
 }



function shorten($url, $code){
$url = mysql_real_escape_string($url);
$code = mysql_real_escape_string($code);
mysql_query("INSERT INTO 'mini_shortener' VALUES('', '$url', '$code')");
return $code;


}



?>

You've errors inside your SQL queries -- and really should make use of the [cde] function to discover much more about individuals errors -)


Still, inside your situation, you're while using following query :

mysql_error()

you shouldn't use single-quotes arround the fields names -- but backticks : SELECT COUNT('url_id') FROM 'mini_shortener' WHERE 'code'= '$code' LIMIT 1 `
Same inside your second query, btw.


With MySQL :

  • Single-quotes are use arround literal-strings,
  • and Backticks ' ` are use arround names.

You cannot choose from the string literal. Use back-ticks rather than single-quotes.

SELECT COUNT('url_id') FROM 'mini_shortener' WHERE 'code' = ...

The instances in which you put column names in single-quotes aren't syntax errors, however they most likely avoid that which you intend. For example, SELECT COUNT(`url_id`) FROM `mini_shortener` WHERE `code` = ... is going to be true only if $code generally is the string 'code'. It doesn't compare $code towards the items in the column 'code' = '$code'.

Likewise you should utilize back-ticks for that table title inside your Place statement:

code

For review:

A string is really a sequence of bytes or figures, enclosed within either single quote (“'”) or double quote (“"”) figures. When the ANSI_QUOTES SQL mode is enabled, string literals could be cited only within single quotes just because a string cited within double quotes is construed being an identifier.

The identifier quote character may be the backtick (“`”). When the ANSI_QUOTES SQL mode is enabled, it's also allowable to quote identifiers within double quotes.