corsasport.co.uk
 

Corsa Sport » Message Board » Off Day » Geek Day » Anyone here good with MySQL?


New Topic

New Poll
  Subscribe | Add to Favourites

You are not logged in and may not post or reply to messages. Please log in or create a new account or mail us about fixing an existing one - register@corsasport.co.uk

There are also many more features available when you are logged in such as private messages, buddy list, location services, post search and more.


Author Anyone here good with MySQL?
Ash_EP3
Member

Registered: 15th May 07
Location: Melksham, Wiltshire
User status: Offline
20th Jun 08 at 10:30   View User's Profile U2U Member Reply With Quote

Right basically me and my IT Officer need some help, we are trying to create fields in a table (the table itself is being created "on the fly") and the user shall determine how many fields there are within this table by entering a number via an input box (text box)

We are trying to run a query within another query (while loop)... and the IT Officer said he wasn't quite sure how to do it

Here is the code:

$tablenameQ = "Q";
$CountQ = Q;
$QuestionName = "Question";
$Contablename = $SurveyName.$tablenameq;
mysql_query("CREATE TABLE". $contablename.
Qindex INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (Qindex),
CreationDate varchar(15),"
&& while ($row = $TotalNumberOfQuestions){
$Count = $CountQ + 1
$Question = $QuestionName.$Count;
printF ("%s varchar (255)", $Question);
}&&")")
or die (mysql_error());


Any help greatly appreciated

p.s we are running Apache 2.2, MySQL 5 and PHP 5

[Edited on 20-06-2008 by S214_UPP]
xa0s
Banned

Registered: 4th Mar 08
Location: Dartford, Kent Car: Turbo'd Fabia vRS
User status: Offline
20th Jun 08 at 14:10   View User's Profile U2U Member Reply With Quote

Probably best asking on a specific forum mate. That's getting pretty deep. I could probably write you something but I'm a little busy at the moment.

Try phpFreaks.
Tim
Site Administrator

Avatar

Registered: 21st Apr 00
User status: Offline
21st Jun 08 at 19:51   View Garage View User's Profile U2U Member Reply With Quote

Seems an odd way of doing things. Tables themselves are usually pretty static. Might be worth reading some database design examples, including normalisation, etc...

If you're making some survey software (plenty of freeware PHP ones on the net if you want to download instead), then make just one table called 'questions' with the following fields (for example):

SurveyID
QuestionID
QuestionName
QuestionValue

To create a new question you just insert a row, and multiple surveys can live in one table.
ed
Member

Registered: 10th Sep 03
User status: Offline
21st Jun 08 at 20:04   View User's Profile U2U Member Reply With Quote

Surely a proper relational databse would be the prefered way of doing it?
ed
Member

Registered: 10th Sep 03
User status: Offline
21st Jun 08 at 20:04   View User's Profile U2U Member Reply With Quote

Oh, what Tim said
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Offline
22nd Jun 08 at 02:18   View Garage View User's Profile U2U Member Reply With Quote

That's not only odd, it's massively difficult to work with, likely you'll have errors often and a complete nightmare to maintain.

If you don't know how many fields you want, you need a table with foreign keys that relate to your surveys tables, exactly as Tim has posted but I perhaps would suggest that question id is the first field so it makes sense to make that the primary in that table, ie.

surveys
s_id|username|date
-------------
1|Ian|21/06/08
2|Ashley|22/06/08
3|usernames...|dates...

questions
q_id|s_id|questiontext
------------
1|1|Was our service good
2|1|Did you buy anything
3|1|Would you come again
4|2|Do you like surveys
5|2|More questions....

You can then of course use the primary key in the questions table as the foreign in your answers:

answers
a_id|q_id|answertext
-----------
1|3|Yes, I would come again
2|3|Perhaps I may come again

In this example, two people have answered question 3, 'Would you come again', which if you look back in the questions table you see has a survey id of 1, therefore was my survey.

Look at the relationships - one survey has many questions. One question has many answers.

The while loop that you describe you still need but of course to INSERT in to the questions table rather than that messy string concat thing.

Hope this fits your issue. Give us more scenario if you want more design.

Side note, you should also learn about aggregate functions, namely GROUP BY and COUNT(*) for summing your answers and displaying stats etc. Get some data in the system then perhaps we can discuss that at a later date when you write the reporting application.
Ian
Site Administrator

Avatar

Registered: 28th Aug 99
Location: Liverpool
User status: Offline
22nd Jun 08 at 02:20   View Garage View User's Profile U2U Member Reply With Quote

quote:
Originally posted by SRi Luke
Probably best asking on a specific forum mate.
I thought he already did that

Not a PHP issue, if the design is correct, PHP won't actually be doing anything particularly special.

[Edited on 22-06-2008 by Ian]

 
New Topic

New Poll

  Related Threads Author Forum Replies Views Last Post
Linking .jsp pages to a MySQL database chris_lee100 Geek Day 7 1425
1st Feb 04 at 14:23
by Ian
 
forum hacking - SQL's Drew Geek Day 15 1713
9th Sep 04 at 23:50
by Ian
 
Server Help Jodi_the_g Geek Day 7 1453
19th Jan 06 at 15:18
by Jodi_the_g
 
My SQL-Nearly there! Tom J Geek Day 31 3177
25th Dec 06 at 23:32
by Ian
 

Corsa Sport » Message Board » Off Day » Geek Day » Anyone here good with MySQL? 29 database queries in 0.0095110 seconds