Web Hosting Talk







View Full Version : mysql_num_rows() is printing error - i dun know why, plzz help


latheesan
09-22-2005, 02:21 PM
Hello,

this is one of my page that suppose to show all the games for the genre 'action'.

This is the code

<?php

if (!defined('IN_SCRIPT'))
{
echo "Sorry, you can't access this page directly!";
exit();
}else{

//The real content
include ("./admin/db.php");
echo "<table align=\"center\" border=\"0\" width=\"800\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"160\" height=\"4\" align=\"center\" valign=\"top\" bgcolor=\"#FFFFFF\">
</td>
<td width=\"480\" height=\"4\" align=\"left\" valign=\"top\" bgcolor=\"#FFFFFF\">
</td>
<td width=\"160\" height=\"4\" align=\"left\" valign=\"top\" bgcolor=\"#FFFFFF\">
</td>
</tr>
<tr>
<td width=\"160\" align=\"center\" valign=\"top\" bgcolor=\"#FFFFFF\">";
include ("./latest_mp3s.php");
include ("./latest_movies.php");
echo "</td>
<td width=\"480\" align=\"left\" valign=\"top\" bgcolor=\"#FFFFFF\">
<table border=\"0\" width=\"480\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"480\" height=\"20\" align=\"left\" class=\"box\" background=\"images/box2.gif\">
&nbsp; Action Games</td>
</tr>
<tr>
<td width=\"480\" align=\"left\" valign=\"top\" class=\"box-txt\">
<br>";
$select = "SELECT * FROM games ORDER BY gamename DESC";
$result = mysql_query($select);
$num = mysql_num_rows($result);
$i = 0;
while ($i < $num){
$gamename = mysql_result($result,$i,"gamename");
$screenshot = mysql_result($result,$i,"screenshot");
$author = mysql_result($result,$i,"author");
$description = mysql_result($result,$i,"description");
$played = mysql_result($result,$i,"played");
echo "<table border=\"0\" width=\"460\" cellspacing=\"0\" cellpadding=\"0\">
<tr>
<td width=\"110\" height=\"110\" align=\"center\" class=\"box-txt2\" rowspan=\"4\" bgcolor=\"#FFF0D7\">
<a href=\"#\">
<img border=\"0\" src=\"$screenshot\" width=\"100\" height=\"100\"></a></td>
<td width=\"10\" height=\"110\" align=\"left\" valign=\"top\" class=\"box-txt2\" rowspan=\"4\">
</td>
<td width=\"10\" height=\"22\" align=\"left\" valign=\"middle\" class=\"box-txt2\" bgcolor=\"#FFF0D7\">
</td>
<td width=\"330\" height=\"22\" align=\"left\" valign=\"middle\" class=\"box-txt2\" bgcolor=\"#FFF0D7\">
<b>Game Name</b> - $gamename</td>
</tr>
<tr>
<td width=\"10\" height=\"22\" align=\"left\" valign=\"middle\" class=\"box-txt2\" bgcolor=\"#FFF9EA\">
</td>
<td width=\"330\" height=\"22\" align=\"left\" valign=\"middle\" class=\"box-txt2\" bgcolor=\"#FFF9EA\">
<b>Author</b> - $author</td>
</tr>
<tr>
<td width=\"10\" height=\"44\" align=\"left\" valign=\"middle\" class=\"box-txt2\" bgcolor=\"#FFF0D7\">
&nbsp;</td>
<td width=\"330\" height=\"44\" align=\"left\" valign=\"middle\" class=\"box-txt2\" bgcolor=\"#FFF0D7\">
<b>Description</b> - $description</td>
</tr>
<tr>
<td width=\"10\" height=\"22\" align=\"left\" valign=\"middle\" class=\"box-txt2\" bgcolor=\"#FFF9EA\">
</td>
<td width=\"330\" height=\"22\" align=\"left\" valign=\"middle\" class=\"box-txt2\" bgcolor=\"#FFF9EA\">
<b>Game Played</b> - $played</td>
</tr>
</table><br><br>";
$i++;
}
echo "<br>
</td>
</tr>
</table>
<font size=\"1\">
&nbsp;
</font>
</td>
<td width=\"160\" align=\"center\" valign=\"top\" bgcolor=\"#FFFFFF\">";
include ("./top10.php");
echo "</td>
</tr>
</table>";
}

?>

Nothing complicated there, but, for some reason, i keep getting this wierd error message

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in root\mysite.com\games\genre_action.php on line 36

i thought i did it correct, so i got the querying the db part and printing the result from the above code and inserted into test.php file and run the script and it is working properly :S how is this????

Can someone lead me in the rith direction to solve this bug plz, thanks in advance

Dan L
09-22-2005, 03:11 PM
Do it on $select, not $result.

maxymizer
09-22-2005, 03:16 PM
DanX, he did it OK, but his query is failing and he doesn't know why.
That's why you have to use constructs like:

$result = mysql_query($select) or die('Error: '. mysql_error());

Replace your line of code with this one and see which error is reported and fix it.

latheesan
09-22-2005, 04:43 PM
well, this is kinda weird... this is the error message im getting

Error: Table 'mysite.games' doesn't exist

for some reason, it is selecting a table called mysite.games where it should select a tabel called 'games'

any ideas why the error is created?

hiryuu
09-22-2005, 04:51 PM
mysite is the database name. I don't see you selecting a database (or connecting, for that matter), so perhaps you're in the wrong one?

Dan L
09-22-2005, 04:51 PM
Can you paste an edited version of db.php?

latheesan
09-22-2005, 05:51 PM
thanks alot DanX,

i went to copy the code for you and found that, i've been connecting to the wrong db.php file, the current one i was connecting to was selecting a different database

now i've fixed it, its working great

innova
09-22-2005, 08:19 PM
Dude you needed a template engine yesterday for all that messy code, my poor eyes hurt reading all that html sprinkling :)

latheesan
09-23-2005, 02:05 AM
Originally posted by innova
Dude you needed a template engine yesterday for all that messy code, my poor eyes hurt reading all that html sprinkling :)

lolzzz, template engine eh, sounds intresting, i might code one when i research a bit more into it later on :D

maxymizer
09-23-2005, 06:18 AM
Originally posted by latheesan
lolzzz, template engine eh, sounds intresting, i might code one when i research a bit more into it later on :D

A simple DB problem and then a TE..hmmm :rolleyes:
Whatever you do, just don't use Smarty :)

Dan L
09-23-2005, 03:24 PM
Template engines don't necessarily have to be complex. The main goal is to separate the front-end and the back-end.

For starters, save your HTML into template/*.php, and then include the relative file in the code, rather than having the HTML in the code.

maxymizer
09-23-2005, 03:48 PM
Smarty is one template engine that kills off the point of template engine.
Main goal was to separate presentation (html) from logic (php that retrieves data from db for example).
That way, as we all know, is easier to modify the looks of the page without touching the code that's responsible for filling the page with data.

But what happens when you use Smarty? You have templates that require you to learn Smarty's syntax (it's like learning another language). It's templates have their own logic (so much for separation of logic and presentation) and in the end you realise that Smarty is completely useless. You could have used PHP for conditional loops and what not.
I suggest looking at phpbb's TE which IMO does it's job perfectly - puts HTML related stuff in one file, and lets' PHP handle fetching info from DB etc.

latheesan
09-23-2005, 05:22 PM
phpbb's TE

is this some kinda script? i know whats a phpbb forum is, but whats that?

Dan L
09-23-2005, 07:00 PM
I don't believe it's a separate script, but it works similarly to what I described, except variables would be accessed like {VARIABLE_NAME} instead of <?=$variable_name;?>

hiryuu
09-23-2005, 10:15 PM
While I like phpbb's template concept, I would be very wary of borrowing that specific implementation. The fact that they build the template into a PHP script, and then execute it, has been the source of most of their security problems. Something that uses regex or str_replace to build the page would be slower, but much safer.

innova
09-25-2005, 07:24 PM
It's templates have their own logic (so much for separation of logic and presentation) and in the end you realise that Smarty is completely useless.

I think you do not understand the purpose of smarty. Have you used it? Its syntax is dead simple, I dont find it difficult and I have just started using it.

Thats not the point, though... Yes, smarty has logic in the templates. I would assert that the purpose is not to separate logic/presentation, but rather to separate BUSINESS logic from PRESENTATION logic.

The nice thing about smarty, aside from the ease of use, is the fact that it saves you a TON of time. I particularly enjoy the {section} tags, because all my sites that I am working on seem to have the common element of fetching data and showing it in some sort of table with user controls based on IDs.

I have things setup so that I include a single setup file, which sets up smarty, DB stuff, etc. I can then have relatively tiny php files that are easy to understand and debug.. its a matter of gathering variables and/or arrays, and just assigning them to the template. From a programming perspective it doesnt get any simpler than that.

I do disagree with the smarty fanatic's assertion that it is easy for designers. It is really not for designers at all.. I dont believe they could be bothered to read the manual and learn all the presentation logic.. however, since YOU are the programmer, you can specify a generic mockup they can do for you, and then you can create some .tpl files with the necessary logic in them.

One other thing.. "smarty is slow" .. I hear it all the time. Baloney. In my experience with smarty and non-smarty sites, the #1 easiest thing to fix is looking at database queries and/or table design. I dunno, ymmv but I love smarty.

maxymizer
09-26-2005, 04:17 AM
I think you do not understand the purpose of smarty. Have you used it?

Believe me, I do understand it's purpose and I have used it.

Yes, smarty has logic in the templates.

This is the part that makes Smarty completely useless to me.
If you take a look at phpbb's TE and it's template files, you will notice that there are only some blocks designated with HTML comments (<!-- -->) and some variables encapsulated with {} and that's it. Also, that's perfect.
If you work closely with a designer, or someone who is an expert in client-side part of the web - he or she will most likely be familliar with xhtml/css but if that person stumbles upon "foreach" and similar constructs - a problem occurs.

Yes, since I am a programmer I can grasp the syntax of smarty faster than someone who isn't a programmer (developer).
But, since I am a developer - do I need Smarty then? Why would I have an application layer that does what PHP does anyway?
I would have to learn Smarty's syntax (which, if you look at the documentation is fairly big for something that should ease your work).
Smarty isn't slow, since it "compiles" it's templates (translates to php scripts) but that's just double work and it's concept is wrong.
It does not help or speed up things for me. It does not help me to separate my presentation from logic completely, while phpbb's TE CAN do that (with a little modification and smarter usage).
Just my 2 cents.
:)

innova
09-26-2005, 09:50 AM
I admit to not being familiar with phpBB's template engine. I will have to go have a look at it and see how it works.

The part I am having a hard time understanding is...

So if there is NO logic (presentational or otherwise) in the phpBB template, are you implicitly stating that there IS display logic in the php code? I cant see how you can have one without the other, but like I said - I will go investigate :)

If that is the case, then to me the phpBB solution is weaker than the smarty solution. However, either way is better than having html and php in the same file. I think we can agree that separation of presentation from logic on some level is beneficial, and from then on its a matter of philosophy and preference.

maxymizer
09-26-2005, 11:07 AM
Well, we all have our preferences. If you like Smarty and if it helps you work faster then go for it.

hiryuu
09-26-2005, 05:01 PM
Fair enough. If you like Smarty's feature level, though, you should also look into XSLT, which gives you powerful presentation flexibility in a language-independent parser.

I've used a phpbb-style template system for awhile, but you wind up with a lot of logic split across the template and code. For instance, special handling for 0 results, or adding a table field with a formatted value. I'm working on an approach right now that moves all logic into php, leaving a single template of building blocks. Business logic should be in objects, anyway.

Dan L
09-26-2005, 10:44 PM
If templates aren't imported from an external source, having PHP in a template is no issue. IMHO the only logic in a template should be an if, to test for user_logged_in, et cetera.

XSL is a great solution too, but how many designers even are familiar with html/css? Anyone who slices with photoshop will most likely have problems with XSL.

I still haven't seen the "perfect" templating system, but if you have the patience, I guess XSL would be the closest anyone can get.

(For anyone not familiar, XSL transforms an XML file. It can be transformed by either PHP or the browser, but PHP is preferred since few browsers support it. The main problem with XSL is that it's very very very strict with what you do, so coding a layout takes that much extra work.)

maxymizer
09-27-2005, 04:34 AM
DanX, in that case server-side language should somehow generate XML. After that, you need XSL stylesheet to format your XML doc to (X)HTML.

Sounds like double work to me. Usefull only if you intend to use that XML across several systems, not just for displaying (X)HTML.

Also, you have to change the PHP script to modify/add XML elements. I wouldn't call this "closest you can get".

In the end, the best way might be using PHP all the way. But as I've said - we all have our preferences, our way of doing things that are logicall only to us so it might not be an issue what's the "best" here, but what's best suited to your logic and way of doing things.

innova
09-27-2005, 09:30 AM
IMHO the only logic in a template should be an if, to test for user_logged_in, et cetera.

This is precisely how to use a template inappropriately. The application layer should decide whether a user is logged in, NOT the template. Deciding whether a user is logged in is NOT presentational logic, its business logic.

An appropriate use of logic in a template would be to show different blocks of content depending on a user's login status. The crucial difference here is that the template itself does not make the decision, it only loads certain content based on what it is told from the calling script.

Dan L
09-27-2005, 12:43 PM
That's what I meant innova, I just worded it badly. I usually like to pass a $logged variable set to an interger based on their userlevel (0-9), then in the nav I just do if($logged >= 1), et cetera. It's easier than having multiple templates.

maxymizer, I suppose you could look at it two ways. In the classic style, you make variables with data available to the template. With XML, you have the script print XML with data in it, and pass that to XSL. For smaller scripts, I usually just do $out .= '<title>Page</title>'; and pass $out to a function to do the dirty work.

I mean, I'm open to suggestions if you can outline a better way to do it, but unless you either have a hundred templates stored in a database (or flat files if you're a masochist), I can't see a very easy way to generate templates. (Besides for propogating databases with "dummy" data, this is my least favorite part of programming.)

maxymizer
09-27-2005, 01:10 PM
I see no reason to have more than hundreds of templates..that's just insane and megalomanic.
And I was in a similar discussion the other day (topic was templating) so I'd like to skip anything further about this theme since programmer's preferences are the key factor to templating approach.

innova
09-27-2005, 02:12 PM
That's what I meant innova, I just worded it badly. I usually like to pass a $logged variable set to an interger based on their userlevel (0-9), then in the nav I just do if($logged >= 1), et cetera. It's easier than having multiple templates.

Good call. I do the same thing, because otherwise after awhile you end up with hundreds of tiny .tpl files :)