Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 12-04-2006, 08:00 AM   #1 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
Question MySQL Is Going Mad!

I have the below script to select and output all products in my database where the type value is 2:

PHP Code:
<?php
$u 
"*********";
$p "******";
$d "*********";
mysql_connect("**************",$u,$p);
@
mysql_select_db($d) or die("Fatal MySQL Error");

$query "SELECT * FROM products WHERE type=2";
$result mysql_query($query);
$num mysql_num_rows($result);

$i 0;
while (
$i<=$num) {
    
$name mysql_result($result,$i,"name");
    
$desc mysql_result($result,$i,"desc");
    
$id mysql_result($result,$i,"id");
    
$price mysql_result($result,$i,"price");
    
$msg  "<tr>\n";
    
$msg .= "<td width=\"15%\">\n";
    
$msg .= "<a href=\"product.php?id=$id\">$name</a><br>$price\n";
    
$msg .= "</td>\n";
    
$msg .= "<td width=\"85%\">\n";
    
$msg .= "$desc\n";
    
$msg .= "</td>\n";
    
$msg .= "</tr>\n";
    echo(
$msg);
    
$i++;
}
?>
Instead of outputting my table full of products, it outputs the table with this above it:

Warning: mysql_result(): Unable to jump to row 2 on MySQL result index 4 in e:\domains\s\stuff4web.co.uk\user\htdocs\flash-catalogue.php on line 31

Warning: mysql_result(): Unable to jump to row 2 on MySQL result index 4 in e:\domains\s\stuff4web.co.uk\user\htdocs\flash-catalogue.php on line 32

Warning: mysql_result(): Unable to jump to row 2 on MySQL result index 4 in e:\domains\s\stuff4web.co.uk\user\htdocs\flash-catalogue.php on line 33

Warning: mysql_result(): Unable to jump to row 2 on MySQL result index 4 in e:\domains\s\stuff4web.co.uk\user\htdocs\flash-catalogue.php on line 34

I have got it to work by changing $i = 0 to $i = 0.5 but this solution seems kinda dodgy


Please help me!

Last edited by markster; 12-04-2006 at 08:02 AM. Reason: Forgot to add part of information
markster is offline   Reply With Quote
Old 12-04-2006, 08:22 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
i think your connection is bad. you are supressing the errors.

here is also an slightly different method to use to loop through your results...
PHP Code:
<?php
$u 
"*********";
$p "******";
$d "*********";
mysql_connect("**************",$u,$p);

// first off, take the @ sign out from before this
// you need to know if you are getting an error while debugging.
// if you didn't know, the @ sign supresses errors
mysql_select_db($d) or die("Fatal MySQL Error");

$query "SELECT * FROM products WHERE type=2";
$result mysql_query($query);

// i prefer mysql_fetch_assoc.. you may too
while ($row mysql_fetch_assoc($result)) {

    
$name $row['name'];
    
$desc $row['desc'];
    
$id $row['id'];
    
$price $row['price'];
    
    
$msg  "<tr>\n";
    
$msg .= "<td width=\"15%\">\n";
    
$msg .= "<a href=\"product.php?id=$id\">$name</a><br>$price\n";
    
$msg .= "</td>\n";
    
$msg .= "<td width=\"85%\">\n";
    
$msg .= "$desc\n";
    
$msg .= "</td>\n";
    
$msg .= "</tr>\n";
    echo(
$msg);
}
?>
__________________
Mike
sde is offline   Reply With Quote
Old 12-04-2006, 08:36 AM   #3 (permalink)
markster
Code Monkey
 
markster's Avatar
 
Join Date: Jun 2006
Posts: 65
markster is on a distinguished road
WOW! Thanks you SOOOOOOOOOOOOOO much!
markster is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Replication / Failover idx Everything SQL ( MySQL, MSSQL, DB2, Postgre, Oracle, etc...) 2 02-23-2005 07:22 PM
How to insert HTML form data into mysql database ? plomon PHP 5 02-06-2005 08:23 AM
and on to mysql .. sde Linux / BSD / OS X 2 01-18-2003 07:39 PM


All times are GMT -8. The time now is 03:54 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting