473,325 Members | 2,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Multiple queries with mysql and php

65
Hi all,

Im really baffled with this one. I have 2 tables that i simply wish to row count and display. the first works fine, but i just cant get the second to work, even if i rename the results variable

the code is below

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. $link = mysql_connect("localhost", "un", "pw") or die('error 1<hr>'.mysql_error());
  4. mysql_select_db("db", $link) or die('error 2<hr>'.mysql_error());
  5.  
  6. $result = mysql_query("SELECT ID FROM tbl_sellers", $link) or die('Query1 Error<hr>'.mysql_error());
  7. $num_rows = mysql_num_rows($result);
  8.  
  9. echo "$num_rows Private Seller records\n";
  10. echo "<br>\n";
  11.  
  12.  
  13. $result = "";
  14.  
  15. $result2 = mysql_query("SELECT id FROM index-peaform", $link) or die('Query2 Error<hr>'.mysql_error());
  16. $num_rows = mysql_num_rows($result2);
  17.  
  18. echo "$num_rows records\n";
  19. echo "<br>\n";
  20. mysql_free_result($result2);
  21.  
  22. mysql_close($link);
  23.  
  24. ?>
  25.  
  26.  
Jan 30 '10 #1
7 2454
Dormilich
8,658 Expert Mod 8TB
why don’t you use MySQL’s internal row counter?
Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(`id`) FROM table
Jan 30 '10 #2
kovik
1,044 Expert 1GB
I find aliasing the COUNT makes it easier to access.

Expand|Select|Wrap|Line Numbers
  1. $result = mysql_query('select count(*) as `count` from `table`');
  2. echo mysql_fetch_object($result)->count;
* NOTE: PHP4 dislikes using the dereferencer (->) on non-variables, including directly on return values.
Jan 31 '10 #3
whitep8
65
hi,

Thanks for the responses.

I dont think ive been clear. I understand the counts, but my problem is that the second query returns only an error. The table exists, and there is data to count, but all i get is the same error.

4 Private Seller records
Query2 Error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'index-peaform' at line 1
Jan 31 '10 #4
kovik
1,044 Expert 1GB
It's a good idea to always surround table and column names with "`" characters. Also, it is standard to use underscores instead of dashes for this exact reason. SQL sees dashes as the subtraction operation, not as a character in a name.
Jan 31 '10 #5
dgreenhouse
250 Expert 100+
Ding, ding, ding... And the prize goes to kovik!
Jan 31 '10 #6
whitep8
65
Hi,

Thank you all for your responses but my question is regarding the code in that the first query runs, the second one doesnt.

I dont see how your responses relate to my question
Jan 31 '10 #7
kovik
1,044 Expert 1GB
Really, ~whitep8? Did you even read my response...?

Basically, replace this:
Expand|Select|Wrap|Line Numbers
  1. SELECT id FROM index-peaform
With this:
Expand|Select|Wrap|Line Numbers
  1. SELECT `id` FROM `index-peaform`
And like I said, for future reference, DO NOT USE DASHES in table names or column names. Use underscores.
Jan 31 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: RG | last post by:
Hopefully someone here can help. I would like to perform multiple queries to a MySQL database and have all the results in 1 result set. Is this possible? TIA RG
2
by: Craig Keightley | last post by:
I need to create a form using multiple quantities that can be updated by one single update. eg: (HTML PAGE) <form method="post" enctype = "multipart/formdata"> <? do { ?> <?php echo...
0
by: Ralph Guzman | last post by:
TASK: I have to generate a report with all categories, subcategories and products in database. PROBLEM: I want to write one query that will return: 1. category 2. subcategory: determined by...
0
by: Rob | last post by:
Good afternoon, I'm looking for information on good benchmarking implementations with which to test MySQL using multiple clients. The benchmark utilities included with MySQL are, as far as I...
1
by: Mark Everett | last post by:
Hi, I am currently running out of space on one of my database servers. Is it possible to move the relevant files for tables onto another drive and instuct MySql to use both folders for it's...
4
by: DG | last post by:
Hi, Can anyone advise how to execute multiple statements in a single query batch. For example- update customers set customer_name = 'Smith' where customer_name = 'Smyth'; select * from...
0
by: CountDraculla | last post by:
Fixing Multiple Database bug in adoDB popular data access layer for php, adoDB can support multiple databases from different provider at time, but not from same provider. what I mean is if you...
7
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both...
7
by: paladin.rithe | last post by:
I have 2 statements that I'd like to string together in a query, but according to everything I'm seeing, it's not possible. What I'm doing is an insert of a row, where the primary key is an auto...
8
by: kumarboston | last post by:
Hi All, I have a mysql database and 3 pages which queries and returns the data. 1st page(main.html) is just a form in html which takes query from the user and is connected to "form...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.