Sponsored by NuSphere - PHP Software for PHP Application Developers - On Sale This Week for $100



Go Back   PHP-Editors > Linux, Apache, MySQL > MySQL Help

MySQL Help Post any question relating to MySQL here and hopefully someone can help

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 2007-01-06, 02:30 AM
Junior Member
 
Join Date: Jan 2007
Posts: 12
Wilson is on a distinguished road
Default Help with Mysql syntax

Sorry if this is terribly basic.

Writing code such as this is easy
Code:
$sql2 = 'SELECT COUNT( DISTINCT `Author` ) FROM `books` LIMIT 0, 100';
but the $64 question is, whats next to make the result show up on a web page? I have this code, please refer to the above code at the bottom of the page

Code:
<?
$dbh=mysql_connect ("localhost", "XXXX", "XXXXX") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("lunar_effort", $dbh);
$sql = ("INSERT INTO `books`(`Book`, `Year`, `Author`, `Genre`, `Number`, `Series`, `Complete`,`Recommendation`, `Status`, `Key`)
VALUES ('$_POST[Book]', '$_POST[Year]', '$_POST[Author]', '$_POST[Genre]', '$_POST[Number]', '$_POST[Series]', '$_POST[Complete]', '$_POST[Recommendation]', '$_POST[Status]', NULL)");
if (!mysql_query($sql,$dbh))
  {
  die('Error: ' . mysql_error());
}
echo $_POST ["Book"],"<br />";
echo $_POST ["Year"],"<br />";
echo $_POST ["Author"],"<br />";
echo $_POST ["Genre"],"<br />";
echo $_POST ["Number"],"<br />";
echo $_POST ["Series"],"<br />";
echo $_POST ["Complete"],"<br />";
echo $_POST ["Recommendation"],"<br />";
echo $_POST ["Status"],"<br />";
echo "1 record added","<br />";
echo "The number of records is ";
$sql1 = "SELECT * FROM books";
$result = mysql_query($sql1,$dbh);
echo mysql_num_rows($result);
 echo "<br />";
echo "The number of Authors is ";
$sql2 = 'SELECT COUNT( DISTINCT `Author` ) FROM `books` LIMIT 0, 100';
$result = mysql_query($sql2,$dbh);
echo mysql_num_fields($result);
 mysql_close($dbh);
?>
Everything works perfectly till I get to this code
Code:
echo "The number of Authors is ";
$sql2 = 'SELECT COUNT( DISTINCT `Author` ) FROM `books` LIMIT 0, 100';
$result = mysql_query($sql2,$dbh);
echo mysql_num_fields($result);
At the $result part, I have no clue what I am doing to make it appear when called. Can anyone point at a tutorial that isnt just about how to write the Select Count part and how to echo the results?
(or help with the code as to what should be there?)

Last edited by Wilson; 2007-01-06 at 02:37 AM.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 2007-01-06, 06:23 AM
Xnuiem's Avatar
Senior Member
 
Join Date: May 2004
Location: DFW, Texas
Posts: 1,104
Xnuiem will become famous soon enough
Send a message via Yahoo to Xnuiem
Default

ok, first your query is wrong.

$sql2 = 'SELECT COUNT( DISTINCT `Author` ) FROM `books` LIMIT 0, 100';
should be
$sql2 = "SELECT COUNT(DISTINCT(`Author`)) FROM `books`";

No point in using a LIMIT clause when only a single row is returned due to the select clause.

Next, you need to go and read the documentation on the MySQL functions, the example you gave isnt even close. I understand how you got there, but make sure to read the docs.

Here is the correct way to do it:

$sql2 = "SELECT COUNT(DISTINCT(`Author`)) FROM `books`";
$result = mysql_query($sql2); <-- Dont need the DB connection unless you have more than one.
$count = mysql_fetch_row($result);
print("The number of Authors is " . $count); <-- echo is fine too, I just the implicit print, a throwback to Jython/JAVA/Perl
__________________
I rarely give code examples.
No, I have never used IIS or Windows of any kind as a web server. Get a real OS!
Please don't PM me, I won't respond.
Reply With Quote
  #3 (permalink)  
Old 2007-01-06, 01:19 PM
Junior Member
 
Join Date: Jan 2007
Posts: 12
Wilson is on a distinguished road
Default

Thanks, I knew I was no wheres close to being right. Thats what i get for relying on MyPhpAdmin for scripts!

I used your example and the result was "The number of Authors is array"

So I did a little browsing and changed your example very very slightly
Original
Code:
print("The number of Authors is " . $count);
New
Code:
print("The number of Authors is " . $count[0]);
Dont really understand why it worked, but it did.

Unfortunately, 99% of the mysql tutorials give you mysql> type information. But I guess I should be looking in the PHP tutorials to find the information I need as far as expressing the results of queries in HTML pages

Thank you much for your help, is much appreciated.
Reply With Quote
  #4 (permalink)  
Old 2007-01-06, 03:18 PM
Xnuiem's Avatar
Senior Member
 
Join Date: May 2004
Location: DFW, Texas
Posts: 1,104
Xnuiem will become famous soon enough
Send a message via Yahoo to Xnuiem
Default

oops.

You can also try:

list($count) = mysql_fetch_row($result);

which will make $count work without messing with it as an array.
__________________
I rarely give code examples.
No, I have never used IIS or Windows of any kind as a web server. Get a real OS!
Please don't PM me, I won't respond.
Reply With Quote
  #5 (permalink)  
Old 2007-01-06, 03:54 PM
Junior Member
 
Join Date: Jan 2007
Posts: 12
Wilson is on a distinguished road
Default

Thanks again, I have a feeling your oops are much fewer than mine!
Reply With Quote
  #6 (permalink)  
Old 2007-01-07, 12:57 AM
Xnuiem's Avatar
Senior Member
 
Join Date: May 2004
Location: DFW, Texas
Posts: 1,104
Xnuiem will become famous soon enough
Send a message via Yahoo to Xnuiem
Default

we all start somewhere. Glad to help.
__________________
I rarely give code examples.
No, I have never used IIS or Windows of any kind as a web server. Get a real OS!
Please don't PM me, I won't respond.
Reply With Quote
Must read Review for Serious PHP Developers


NuSphere PhpED 5.0 : The Staff of php-editors.com recently spent a few days working with NuSphere PhpED 5.0 (a popular PHP IDE) and NuCoder 1.4 (a PHP Encoding Utility), read up on all the details.

Sponsored Links
Reply

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



All times are GMT +1. The time now is 01:14 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
© Copyright 2003-2008 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.