Jump to content
xisto Community
Ash-Bash

Strange Php Problem

Recommended Posts

Hi there,

I have a problem with my URL shortner site.

Here is the error I get when shortening the URL:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/itneedn/public_html/tinylx.net/shorten.php on line 54


Here is the code on that line:

$already_shortened = mysql_result(mysql_query('SELECT id FROM ' . DB_TABLE. ' WHERE long_url="' . mysql_real_escape_string($url_to_shorten) . '"'), 0, 0);


Would anyone like to fix this for me and provide me with a fixed code.

Thanks in advance.

Share this post


Link to post
Share on other sites

i couldn't find any syntax error in this code, so please post the whole code in here, usually this error message generates from an sql query that doesn't done correctly. therefore you should search up and down the error line that is given in the error message.

Share this post


Link to post
Share on other sites

This is because of a problem with the code you are using to store your tiny urls that get generated from the site. You can read the help guides on mysql_result() at this site. Also check all your file directories and their paths, check the CHMOD of all your directories and use the built-in phpmyadmin tools to utilise or clean the tables.

 

Another tip will be to insert the query as 'mysql_num_rows' instead of 'mysql_(result) like below

 

$already_shortened = mysql_num_rows(mysql_query('SELECT id FROM ' . DB_TABLE. ' WHERE long_url="' . mysql_real_escape_string($url_to_shorten) . '"'), 0, 0);

 


That should solve it if it is a simple problem. If everything fails, then you can post your code like web_designer suggested.

 

edit: Used 'quote tags' instead of the code tag.

Edited by deadmad7 (see edit history)

Share this post


Link to post
Share on other sites

The mysql_query function could be returning false. The boolean false is not a resource and would explain the error. You should therefore store the result of mysql_query in a variable, use an if statement to verify the variable, and if it passes, obtain the MySQL result with mysql_result. If that still doesn't work, you can do what deadmad7 suggested and use an alternative function that pretty much does the same thing.

Share this post


Link to post
Share on other sites

I get this error when replacing it with the new code you gave me:

Warning: Wrong parameter count for mysql_num_rows() in /home/itneedn/public_html/tinylx.net/shorten.php on line 54http://forums.xisto.com/no_longer_exists/


Here is the full code:
<title>TinyLX.net Your URL shortening request completed!</title><a href="http://forums.xisto.com/no_longer_exists/ here to shorten another</a><br><a id="ck_email" class="stbar chicklet" href="javascript:void(0);"><img src="http://w.sharethis.com/chicklets/email.gif" /></a><a id="ck_facebook" class="stbar chicklet" href="javascript:void(0);"><img src="http://w.sharethis.com/chicklets/facebook.gif" /></a><a id="ck_twitter" class="stbar chicklet" href="javascript:void(0);"><img src="http://w.sharethis.com/chicklets/twitter.gif" /></a><a id="ck_sharethis" class="stbar chicklet" href="javascript:void(0);"><img src="http://w.sharethis.com/chicklets/sharethis.gif" />ShareThis</a><script type="text/javascript">	var shared_object = SHARETHIS.addEntry({	title: document.title,	url: document.location.href});shared_object.attachButton(document.getElementById("ck_sharethis"));shared_object.attachChicklet("email", document.getElementById("ck_email"));shared_object.attachChicklet("facebook", document.getElementById("ck_facebook"));shared_object.attachChicklet("twitter", document.getElementById("ck_twitter"));</script><br>The URL =<?php/* * First authored by Brian Cray * License: http://creativecommons.org/licenses/by/3.0/ * Contact the author at http://briancray.com/ */$url_to_shorten = get_magic_quotes_gpc() ? stripslashes(trim($_REQUEST['longurl'])) : trim($_REQUEST['longurl']);if(!empty($url_to_shorten) && preg_match('|^https?://|', $url_to_shorten)){	require('config.php');	// check if the client IP is allowed to shorten	if($_SERVER['REMOTE_ADDR'] != LIMIT_TO_IP)	{		die('You are not allowed to shorten URLs with this service.');	}		// check if the URL is valid	if(CHECK_URL)	{		$ch = curl_init();		curl_setopt($ch, CURLOPT_URL, $url_to_shorten);		curl_setopt($ch,  CURLOPT_RETURNTRANSFER, TRUE);		$response = curl_exec($ch);		curl_close($handle);		if(curl_getinfo($ch, CURLINFO_HTTP_CODE) == '404')		{			die('Not a valid URL');		}	}		// check if the URL has already been shortened	$already_shortened = mysql_num_rows(mysql_query('SELECT id FROM ' . DB_TABLE. ' WHERE long_url="' . mysql_real_escape_string($url_to_shorten) . '"'), 0, 0);	if(!empty($already_shortened))	{		// URL has already been shortened		$shortened_url = getShortenedURLFromID($already_shortened);	}	else	{		// URL not in database, insert		mysql_query('LOCK TABLES ' . DB_TABLE . ' WRITE;');		mysql_query('INSERT INTO ' . DB_TABLE . ' (long_url, created, creator) VALUES ("' . mysql_real_escape_string($url_to_shorten) . '", "' . time() . '", "' . mysql_real_escape_string($_SERVER['REMOTE_ADDR']) . '")');		$shortened_url = getShortenedURLFromID(mysql_insert_id());		mysql_query('UNLOCK TABLES');	}	echo BASE_HREF . $shortened_url;}function getShortenedURLFromID ($integer, $base = ALLOWED_CHARS){	$length = strlen($base);	while($integer > $length - 1)	{		$out = $base[fmod($integer, $length)] . $out;		$integer = floor( $integer / $length );	}	return $base[$integer] . $out;}

And if anyone would like to design it better with the script working then please do.

as it currently looks rather boring
http://forums.xisto.com/no_longer_exists/shorten.php

Share this post


Link to post
Share on other sites

You cannot replace a function name and expect the function to have an equal amount of parameters as the previous function. mysql_num_rows takes one parameter: the resource. Your code assumes it takes the same parameters as mysql_result. Therefore you get that error.

Share this post


Link to post
Share on other sites

i think you should replace the script, i checked it, it the same script in thin link, by the same author. but yours contains a different functions. so may be this is the error.any way i checked the script in this link and it works fine, but i couldn't get results because i tested locally, so if you want, replace it with yours and see.http://forums.xisto.com/no_longer_exists/ luck.

Share this post


Link to post
Share on other sites

With the new code I get this error:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/itneedn/public_html/tinylx.net/shorten.php  on line 36http://tinylx.net/0


Here is the full code:
<?php/* * First authored by Brian Cray * License: http://creativecommons.org/licenses/by/3.0/ * Contact the author at http://briancray.com/ */ $url_to_shorten=get_magic_quotes_gpc() ? stripslashes(trim($_REQUEST['longurl'])):trim($_REQUEST['longurl']); if(!empty($url_to_shorten)&&preg_match('|^https?://|',$url_to_shorten)){require('config.php');if($_SERVER['REMOTE_ADDR']!= LIMIT_TO_IP){die('You are not allowed to shorten URLs with this service.');} 	$already_shortened=mysql_result(mysql_query('SELECT id FROM '. DB_TABLE.' WHERE long_url="'.mysql_real_escape_string($url_to_shorten).'"'),0,0);if(!empty($already_shortened)){// URL has already been shortened$shortened_url=dechex($already_shortened);}else{// URL not in database, insertmysql_query('INSERT INTO '. DB_TABLE .' (long_url, created, creator) VALUES ("'.mysql_real_escape_string($url_to_shorten).'", "'.time().'", "'.mysql_real_escape_string($_SERVER['REMOTE_ADDR']).'")');$shortened_url=dechex(mysql_insert_id());}echo'http://'.$_SERVER['HTTP_HOST'].'/'.$shortened_url;}

Share this post


Link to post
Share on other sites

I don't know how you exactly assigned it, but could it be that DB_TABLE has been defined as a variable, and you forgot to place the '$' before it?

Share this post


Link to post
Share on other sites

With the new code I get this error:


are you talking about the code i gave you?

if yes, then be sure to follow the author's instructions. because i tested it locally on my computer and get no error at all.

also, i am wondering how your site was working before? you can't get an error in mysql query in a sudden, you should changed something in the code first.

Share this post


Link to post
Share on other sites

Ash, just stick with mysql_num_rows but this time make sure you don't pass any values to the function that the function does not support. mysql_query, as i understand it, will not return a resource if 0 rows are found. You won't have to modify the logic of your code, either, after changing to mysql_num_rows, since empty will return true if the value passed is 0.

Share this post


Link to post
Share on other sites

Can you just insert it into the full code for me I am very ill right now and cant spend alot of time on the pc

Share this post


Link to post
Share on other sites

I can't fully understand your post and what you want to do, i may require some time to think about your problem. I am interested on how this will develop so I check and loaded your script from the URL you provided and found out that I am only seeing a blank screen. Checking your root site gives me tones of errors telling about deprecated functions for assigning new to classes which is common to people using PHP 5 and have Deprecated warning turned on

To solve your most noticeable problem at your site, I suggest that you turn on file error logging on your control panel and then using this command on the very first line of all your main pages.

ini_set('error_reporting', E_ALL ^E_DEPRECATED ^E_USER_DEPRECATED); ini_set('display_errors', TRUE);

this command tells PHP to disable sending the deprecated messages and still send error messages.
in cases you want to disable NOTICE errors, just add this at the right side
^E_NOTICE

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.