PHP & MySQL Error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dannyg
Forum Newbie
Posts: 3
Joined: Tue Oct 03, 2006 2:29 pm

PHP & MySQL Error

Post by dannyg »

Hi,

I'm trying to implement the following code into a larger piece of code:

Code: Select all

for($count = 0; $count < mysql_num_rows($result); $count++)
	{
		$name = mysql_result($result, $count, "name");
		$koodi = mysql_result($result, $count, "koodi");									
									
		echo "<tr><td>";
		echo "<p>".$name."<a href='".$koodi."'>".$koodi."</a></p>";				
		echo "</tr></td>";
																					
	}
But when I try and access it via a web browser, I get:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/******/public_html/******/index.php on line 257

I've only just started to learn PHP and MySQL so this is all quite new to me.

Any ideas?

Thanks in advance,

Daniel.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

that error usually means your query failed.

we'll need to see more of the code to really help you though.

show us the query (mysql_query()).
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Post the code where you make your query... it should look something like $result = mysql_query("<MYSQL QUERY WILL BE HERE");
dannyg
Forum Newbie
Posts: 3
Joined: Tue Oct 03, 2006 2:29 pm

Post by dannyg »

Code: Select all

switch($season)
	{
		case 1:
			$result = mysql_query("SELECT id, name, koodi FROM one ORDER BY name");										
		break;
		case 2:
			$result = mysql_query("SELECT id, name, koodi FROM two ORDER BY name");										
		break;
		case 3:
			$result = mysql_query("SELECT id, name, koodi FROM three ORDER BY name");										
		break;
		case 4:
			$result = mysql_query("SELECT id, name, koodi FROM four ORDER BY name");										
		break;
		case 5:
			$result = mysql_query("SELECT id, name, koodi FROM five ORDER BY name");										
		break;
		case 6:
			$result = mysql_query("SELECT id, name, koodi FROM six ORDER BY name");										
		break;
		case 7:
			$result = mysql_query("SELECT id, name, koodi FROM seven ORDER BY name");										
		break;
		case 8:
			$result = mysql_query("SELECT id, name, koodi FROM eight ORDER BY name");										
		break;
		case 9:
			$result = mysql_query("SELECT id, name, koodi FROM nine ORDER BY name");										
		break;
		case 10:
			$result = mysql_query("SELECT id, name, koodi FROM ten ORDER BY name");										
		break;
		case 11:
			$result = mysql_query("SELECT id, name, koodi FROM eleven ORDER BY name");										
		break;
		case 12:
			$result = mysql_query("SELECT id, name, koodi FROM twelve ORDER BY name");										
		break;
		case 13:
			$result = mysql_query("SELECT id, name, koodi FROM thirteen ORDER BY name");										
		break;
		case 14:
			$result = mysql_query("SELECT id, name, koodi FROM fourteen ORDER BY name");										
		break;
		case 15:
			$result = mysql_query("SELECT id, name, koodi FROM fifteen ORDER BY name");										
		break;
		case 16:
			$result = mysql_query("SELECT id, name, koodi FROM sixteen ORDER BY name");										
		break;
		case 17:
			$result = mysql_query("SELECT id, name, koodi FROM seventeen ORDER BY name");										
		break;
		case 18:
			$result = mysql_query("SELECT id, name, koodi FROM eightteen ORDER BY name");										
		break;
		case 19:
			$result = mysql_query("SELECT id, name, koodi FROM nineteen ORDER BY name");										
		break;
		case 20:
			$result = mysql_query("SELECT id, name, koodi FROM twelve ORDER BY name");										
		break;
	}
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

instead of actually performing your query within your switch, just set a string variable equal to your query then run your query at the end. Doing it this way will allow you to echo your query to determine that it really is what you think it is.

ex:

Code: Select all

switch($season)
{
   case 1:
   $qry = "SELECT `id`, `name`, `koodi` FROM `one` ORDER BY `name`";
   break;
   case 2:
   $qry = "SELECT `id`, `name`, `koodi` FROM `two` ORDER BY `name`";
   break;
   // ... etc
}
echo $qry;
$result = mysql_query($qry)
   or die(mysql_error());
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You have 19 or 20 structural identical tables and you use them for the same purpose? Why not adding a field to one table and store all the data in there?
anyway, at least reduce the redundancy in your code.

Code: Select all

$tables = array(
		1=>'one','two','three','four',
		'five','six','seven','eight',
		'nine','ten','eleven','twelve',
		'thirteen','fourteen','fifteen','sixteen',
		'seventeen','eightteen','nineteen','twelve' /* 20=>twelve is correct? */
	);

if ( !isset($tables[$season]) ) {
	die('no such season: '.$season);
}
else {
	$query = 'SELECT id, name, koodi FROM '.$tables[$season].' ORDER BY name';
	$result = mysql_query($query) or die(mysql_error() . ': '.$query);
}
dannyg
Forum Newbie
Posts: 3
Joined: Tue Oct 03, 2006 2:29 pm

Post by dannyg »

When I use Burrito's advice, I get:

"SELECT `id`, `name`, `koodi` FROM `one` ORDER BY `name`No database selected"


And when I use volka's advice I get nothing, no information or error messages, just blankness.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

ok, so you don't have a database selected.

make sure you are also connected to a mysql_server.

here are two functions you'll need to make use of: mysql_connect() and mysql_select_db()

ex:

Code: Select all

mysql_connect("localhost","username","password")
  or die(mysql_error());
mysql_select_db("somedatabase")
  or die(mysql_error());
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

Heh, I know exactly where this code is from, i was looking at it the other day... the code should work as is... i noticed the massive duplication of the queries, but since the purpose of the code is technically illegal (see ....) i wasn't about to fix it...

Burrito: If the content is illegal, then there's no reason to post a link to it. I removed the link.


Edit: Sorry about that... The content of the site i linked to was removed due to a request from FOX, so I assumed it would be okay, but I should know what happens when you assume... ;)

Won't happen again...
Post Reply