GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Apr-2004, 03:04
jgnasser jgnasser is offline
New Member
 
Join Date: Apr 2004
Posts: 3
jgnasser is on a distinguished road

PHP MySQL query


I'm a PHP/MySQL Newbie. Just created a database and among other things, I would like to display the number or records in MyTable on the Webpage. Here is what I'm trying but its returning an error. I have other parts of the code that create the connection to the database and another variable, $result which is pulling out the database contents fine.

PHP Code:

$resultcount = @mysql_query( 'SELECT COUNT(*) as count FROM MyTable' );
$row1 = mysql_fetch_row($resultcount);
echo($row1['count']); 




OK where do I go wrong?
Last edited by JdS : 06-Apr-2004 at 07:27. Reason: Please enclose PHP code in [php] & [/php] for syntax highlighting
  #2  
Old 06-Apr-2004, 07:34
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Hello jgnasser,

Try not to use function names as column/field/alias names...

Try using a different ALIAS in your SQL, perhaps;
PHP Code:

<?php
$resultcount = @mysql_query( 'SELECT COUNT(*) as `ttl` FROM MyTable' );

$row1 = mysql_fetch_row($resultcount);
// ALSO, I missed this in my reply and am now adding it in, in the 'edit' mode...

// since you're using mysql_fetch_row(), you have access only to the numerical index...
// so replace this:
// $row1 = mysql_fetch_row($resultcount);
// with
$row1 = mysql_fetch_assoc($resultcount);
?>

  #3  
Old 08-Apr-2004, 05:23
jgnasser jgnasser is offline
New Member
 
Join Date: Apr 2004
Posts: 3
jgnasser is on a distinguished road
Actually I used the index and it worked. does mysql_fetch_assoc work like mysql_fetch_array?

Quote:
Originally Posted by JdS
Hello jgnasser,

Try not to use function names as column/field/alias names...

Try using a different ALIAS in your SQL, perhaps;
PHP Code:

<?php
$resultcount = @mysql_query( 'SELECT COUNT(*) as `ttl` FROM MyTable' );

$row1 = mysql_fetch_row($resultcount);
// ALSO, I missed this in my reply and am now adding it in, in the 'edit' mode...

// since you're using mysql_fetch_row(), you have access only to the numerical index...
// so replace this:
// $row1 = mysql_fetch_row($resultcount);
// with
$row1 = mysql_fetch_assoc($resultcount);
?>

  #4  
Old 08-Apr-2004, 05:42
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
mysql_fetch_array() allows you to access both, the array numerically indexed like $row[0], $row[1] or via the column names (associative index) i.e. $row['a'], $row['ttl'] etc. Of course you can use the CONSTANTS available for this function to refine the results either way.

mysql_fetch_row() and mysql_fetch_assoc() does only one or the other (numerical or associative).
 
 

Recent GIDBlogA Week in Kuwait by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Free Web, Emal, PHP, MySQL Host avida Free Web Hosting 4 28-Dec-2006 15:33
PHP, MySQL, WML skyloon MySQL / PHP Forum 0 05-Mar-2004 07:53
FREE 25 MB, No Ads, Control Panel, ASP, ColdFusion, PHP, MySQL, Access Hosting rkmails Free Web Hosting 0 08-Sep-2003 05:49
[script] Password Protect web pages/site using PHP and MySQL JdS PHP Code Library 0 23-Jul-2003 10:02
Windows: From only £20p/y,Linux: from $10p/m. ASP, ASP.NET, PHP, Free MySQL, +More EyotaHosts Web Hosting Advertisements & Offers 0 28-Jun-2003 13:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:49.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.