Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

PHP/MySQL confusion...

Thread Tools
 
Search this Thread
 
Old 07 March 2002, 09:13 AM
  #1  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Question

Okey kokey, I'm trying to get to grips with the above at the moment. I've installed all the relevant programs locally for testing.

I've created a lil' db called test and a table called stuff in mySQL.

In my PHP code I have the following:
$connect = @mysql_connect
$db_select = @mysql_select_db($database) or die("db error");

$SQL = "SELECT * FROM $table";
And so on...

The $database and $table variables are assigned higher up the code.

However, my problem is when I vew the page I get the following error:
Parse error: parse error, unexpected T_VARIABLE in my_local_path\test.php on line 39


Line 39 is my $db_select = @mysql... line - anyone see anything wrong?
All required services are running, the db and table definately exist and PHP is quite happy. Getting a bit confused now

Ian

[Edited by Trotty - 7/3/2002 9:49:43 AM]
Old 07 March 2002, 11:11 AM
  #2  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Talking

What a tool!
No-one noticed my mistake though...
I was selecting:
$query = "SELECT * FROM test";

When it should have been:
$query = "SELECT * FROM stuff";

Test is the db name, stuff is the table name - and ta-da! It now works

All I need to do now is work out how to displayed a db item depending on what is selected from a dropdown combo box...

D'oh!

[Edited by Trotty - 7/3/2002 11:11:39 AM]
Old 07 March 2002, 03:34 PM
  #3  
legacyPete
Scooby Regular
 
legacyPete's Avatar
 
Join Date: Dec 2001
Posts: 202
Likes: 0
Received 0 Likes on 0 Posts
Post

Right... here we go

You don't need queries on submit buttons (if I'm getting what you mean). It's quite straightforward really, and if you want to be really clever you can build up queries using arrays and variable variables...

EXAMPLE:

This example uses two tables - towns and bookings

towns:
townID
townName

bookings:
bookingID
name
townID

To put info into a combo box put this in a form:

<select name="townID" class="input">
<?
$townquery = mysql_query("SELECT townID, townName from towns order by townName ASC");
while($towns = mysql_fetch_array($townquery)) {
echo"<option value=\"".$towns["townID"]."\">".$towns["townName"]."</option>";
}
?>
</select>

The form is submitted to another page (or the same one if you want) and then queries the database using the selected townID from the form:

if($id) {
// perform query and display stuff
$query = mysql_query("select bookings.*, towns.* from bookings, towns where bookings.bookingID = $townID AND bookings.townID = towns.townID");
$data = mysql_fetch_array($query);
// ...output stuff
} else {
// ...tell 'em to bugger off
}
?>

That's a basic example to give you an idea of what to do, it can be made a lot better. If you want a better example or need more help I could email you some pages.
Pete

[Edited by legacyPete - 7/3/2002 3:35:12 PM]
Old 07 April 2002, 11:31 AM
  #4  
legacyPete
Scooby Regular
 
legacyPete's Avatar
 
Join Date: Dec 2001
Posts: 202
Likes: 0
Received 0 Likes on 0 Posts
Post

That's an easy one

$query = "SELECT * FROM data WHERE part like \"$series\"";

Best to escape the value using \" as the input is free text.

To make it case-insensitive you can put \"%$series%\" in the query instead (I think).

Any form objects are turned into variables when they are submitted, hence $series; just put the dollar in front!

TIP:

instead of
or die ("bugger");

try putting
or die (mysql_error());

The error message isn't as amusing, but it may help you

[Edited by legacyPete - 7/4/2002 11:33:39 AM]
Old 07 April 2002, 11:51 AM
  #5  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Thumbs up

Pete your a star!
That makes sense once I've seen it...
So I suppose I could still do the combo box, but manually populate the options, and pass the selection as the query variable? Like:

<form action="result.php" method="post">
<select name="select">
<option>EX</option>
<option>VEX</option>
<input type="submit" value="Search">
</select>
</form>

And then use "\$select%\""?

Ian

[Edited to say]
~ Woohoo, it does work ~

Thanks for that Pete, really appreciate it.

[Edited by Trotty - 7/4/2002 11:58:12 AM]
Old 07 April 2002, 12:05 PM
  #6  
legacyPete
Scooby Regular
 
legacyPete's Avatar
 
Join Date: Dec 2001
Posts: 202
Likes: 0
Received 0 Likes on 0 Posts
Post

Yes, it really is that easy!

However, instead of buggering about doing the combo yourself, get the info from the database:

<select name="select" class="input">
<?
$seriesquery = mysql_query("SELECT distinct series from data order by series group by series");
while($seriesdata = mysql_fetch_array($seriesquery)) {
echo"<option value=\"".$seriesdata["series"]."\">".$seriesdata["seriesdata"]."</option>";
}
?>
</select>

Yes, then you would use $select, or whatever you called the form object.

[Edited to say]
Glad I could help!

[Edited by legacyPete - 7/4/2002 12:07:30 PM]
Old 03 July 2002, 10:38 AM
  #7  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Unhappy

Bugger! Right, fixed the above problem by changing:
$connect = @mysql_connect
$db_select = @mysql_select_db($database) or die("db error");

To:

mysql_connect("localhost", "usernamehere", "passwordhere");
mysql_select_db("dbname") or die ("db error");


However, now I've come across another oddity? My next piece of code just select all my data from the table and spits it out - well, it should... The code is:

$SQL = "SELECT * FROM test";
$result = mysql_query($SQL) or die("could not complete your query");
while($row = @mysql_fetch_array($result)) {
$id = $row["id"];
$name = $row["name"];
$description = $row["description"];
echo "$name - $description <P>";
}

I now get "could not complete your query"
Does $result need to be defined as a variable somewhere? I got the impression that it didn't need to be?

Anyone...

Ian


Trending Topics

Old 03 July 2002, 11:32 AM
  #8  
snowcrash
Scooby Regular
 
snowcrash's Avatar
 
Join Date: Mar 2002
Location: www.scoobyzone.co.uk
Posts: 2,082
Likes: 0
Received 0 Likes on 0 Posts
Post

uhm, answered it yourself
Old 03 July 2002, 11:48 AM
  #9  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Cool

Well, I like to share...
Old 03 July 2002, 01:59 PM
  #10  
snowcrash
Scooby Regular
 
snowcrash's Avatar
 
Join Date: Mar 2002
Location: www.scoobyzone.co.uk
Posts: 2,082
Likes: 0
Received 0 Likes on 0 Posts
Post

php and mysql are lovely i have to say - pisses all over asp and M$ based crap
Old 03 July 2002, 03:15 PM
  #11  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Post

Does seem pretty good, well, from my first impressions anyway.

Just trying to work out how someone can select something from a combo box (or two) and then have mySQL lookup the relevant data and display it...

I gather I need to assign a query to the submit button of my form but would I need a shedload of if...else statements to account for all the options?

Slowly getting lost!
Old 03 July 2002, 03:29 PM
  #12  
dsmith
Scooby Regular
 
dsmith's Avatar
 
Join Date: Mar 1999
Posts: 4,518
Likes: 0
Received 0 Likes on 0 Posts
Post

or a switch statement.....
Old 03 July 2002, 03:50 PM
  #13  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Post

Thanks for the response gents.
Pete - that's kind of what I'm tring to do. Basically I'd only have one (pretty big) table and when the user selects two possible attributes the query would look up and display all available options at the bottom of the same page (if poss).

The reason is, the company I work for sells precision bearings. They are classified by various attributes but the main two are series and bore size. We want the page to list all series and bores in two lists and when two have been selected the bottom part of the page will list all available options for those bearings (preloads, pairings, etc).

Badly explained but hopefully you know what I mean...

Thanks,

Ian
Old 03 July 2002, 04:11 PM
  #14  
dsmith
Scooby Regular
 
dsmith's Avatar
 
Join Date: Mar 1999
Posts: 4,518
Likes: 0
Received 0 Likes on 0 Posts
Wink

You see that sort of proper response is why people (i.e. me ) who are only a couple of pages further ahead in "php for dummies" should keep their mouths shut
Old 03 July 2002, 04:14 PM
  #15  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Post

Heh heh heh, well, all input is appreciated
Old 03 July 2002, 04:19 PM
  #16  
legacyPete
Scooby Regular
 
legacyPete's Avatar
 
Join Date: Dec 2001
Posts: 202
Likes: 0
Received 0 Likes on 0 Posts
Post

Ok then, have two combo boxes called series and bore like in my above post. Change the queries to:

series:
select distinct series from catalogue order by series

bore
select distinct bore from catalogue order by bore

Then when the search is submitted use a query like:
select * from catalogue where series = \"$series\" and bore = \"$bore\" order by series, bore

You can't get the page to be updated without it refreshing, so you would just have the page submit to itself and display any search results underneath the search form. Or you could use (whisper it) a frameset with the search in one frame and the result page in another??

I would advise trying to split the data into different tables as it makes searching more efficient and also reduces duplicate data - e.g having a series table with a unique seriesID for different series names / codes. Then in your catalogue table you would reference the seriesID. This is a lot easier to search and also means that if the series name / code changes you only have to change 1 record. However, without knowing more about the data it's hard to say what the best method would be.
Old 03 July 2002, 04:45 PM
  #17  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Post

Ok, I see. Well, a typical part no. for us would be:
VEX257CE1

VEX is the series, 25 the bore, 7 the precision, CE1 the contact angle. However, a single bearing (listed above) can be put into different sets so you could have:

VEX257CE1
VEX257CE1UL
VEX257CE1DUL
VEX257CE1TDL

And so on...

I was planning on having one table with all parts in and then pulling out all available ones depending on series/bore selection. Couldn't think of an easier way to split the data up? (DB's never were a strong point!)

Any help?
Old 03 July 2002, 05:54 PM
  #18  
legacyPete
Scooby Regular
 
legacyPete's Avatar
 
Join Date: Dec 2001
Posts: 202
Likes: 0
Received 0 Likes on 0 Posts
Post

I'm a bit confused by the set thing, are they treated as individual items?

Having one table will work, it's just that it's not so good to have lots of repeating data.

If you try the combo boxes and search in my previous post, it should work - just change the queries accordingly. It's hard to explain it properly so if there's anything I haven't explained very well, just say.
Old 03 July 2002, 07:50 PM
  #19  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Post

Thanks for that Pete, I'll have a good look at it tomorrow and see how it goes.

Ian
Old 04 July 2002, 10:24 AM
  #20  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Post

Okey dokey, clearly I'm crap at all this malarkey Shoudn't have started dabbling...

Just to get going I've change the idea a bit. I've now got two pages, tech.php and result.php. I've stuck a text entry field in tech.php and the result from that, when submitted gets passed over as series.
I'm now trying to get the query on result.php to use series as a query variable? Here's my code:

tech.php
<form action="result.php" method="post">
<font size="2">Bearing Series:</font>
<input type="text" name="series">
<input type="submit" value="Search">
</form>

result.php
<?php
$query = "SELECT * FROM data WHERE part like ???";
$result = @mysql_query($query) or die("bugger!");

while($row = @mysql_fetch_array($result)) {
$id = $row["id"];
$part = $row["part"];
$description = $row["description"];
echo "$part - $description<P>";
}
?>
All I want to do is change the query so it selects 'part like ' series typed in first page. I thought this would be easier than the combo box idea for now...

When the form submits the data does it 'carry it over' as series or Search?

Sorry for the length of post! Not too hot at this php stuff...

Cheers for any help,

Ian

[Edited by Trotty - 7/4/2002 10:28:12 AM]

[Edited by Trotty - 7/4/2002 10:28:55 AM]
Old 04 July 2002, 12:43 PM
  #21  
Trotty
Scooby Regular
Thread Starter
 
Trotty's Avatar
 
Join Date: Feb 2002
Posts: 179
Likes: 0
Received 0 Likes on 0 Posts
Cool

Just tried the above - works like a good 'un!
Now to setup a Series and size multiple select - should be the same as the earlier example wouldn't it?
Old 04 July 2002, 01:17 PM
  #22  
legacyPete
Scooby Regular
 
legacyPete's Avatar
 
Join Date: Dec 2001
Posts: 202
Likes: 0
Received 0 Likes on 0 Posts
Post

Yep, should be v. similar, just change names and the queries a bit
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
RichB
Computer & Technology Related
4
19 October 2007 10:37 AM
gregh
Computer & Technology Related
2
26 May 2004 04:26 PM
Stueyb
Computer & Technology Related
9
19 May 2004 11:04 PM



Quick Reply: PHP/MySQL confusion...



All times are GMT +1. The time now is 10:14 AM.