Technical Discussion
  >> Web Design / HTML / Web hosting Forum


Register (or login) on our website and you will not see this ad.


These posts have been archived and can no longer be replied to or modified.
  Print Thread
Standard User Taras
(eat-sleep-adslguide) Sun 06-Jun-10 15:56:19
Print Post

Noob PHP mysql prob :P


[link to this post]
 
$key="id=22";
$result = mysql_query('SELECT `tblproducts`.`configoption3` '.' FROM tblproducts '.' WHERE `tblproducts`. $key ') or die(mysql_error());
$row = mysql_fetch_assoc($result);

What am I doing wrong with the above in regards $key variable. Basically i want declare the id once and use the $result multiple times (ie resusable ) and change the id value as when and where

Getting error "Unknown column 'tblproducts.$key' in 'where clause' "

Its probably a silly syntax issue but am in programmers blind spot tongue .. As I've not really touched php that much or "programming proper " for years.

I need to change configoption3 to be stored idealy as a variable .. So that I can reassign as fit tongue ...

Any help would be appreciated ..

_____________________________________________
<randomness>
streaming music - your music - spotify
Everything websites.... soon
My Blog .......... here (still in dev mode)
Me twittering go add

Edited by Taras (Sun 06-Jun-10 15:56:41)

Standard User Kenneth
(legend) Sun 06-Jun-10 16:08:33
Print Post

Re: Noob PHP mysql prob :P


[re: Taras] [link to this post]
 
In reply to a post by Taras:
$key="id=22";
$result = mysql_query('SELECT `tblproducts`.`configoption3` '.' FROM tblproducts '.' WHERE `tblproducts`. $key ') or die(mysql_error());
$row = mysql_fetch_assoc($result);

What am I doing wrong with the above in regards $key variable. Basically i want declare the id once and use the $result multiple times (ie resusable ) and change the id value as when and where

is your id column = "tblproductsid"?

I would always just store the the id as a number and do something like
$sql = "SELECT p.configoption3 FROM tblproducts p WHERE p.id = ${id}" if I wasn't using prepared queries as it makes it much clearer what the SQL is

edit: or even `tblproducts`. $key' (you need double quotes to parsed variables I believe) - and I don't believe you need the mysql quote char ` (which makes it hard to read I find)

Ken

Expecting matters to worsen ensures they do; expecting them to improve is merely foolishness

Edited by Kenneth (Sun 06-Jun-10 16:13:44)

Standard User Taras
(eat-sleep-adslguide) Sun 06-Jun-10 16:21:27
Print Post

Re: Noob PHP mysql prob :P


[re: Kenneth] [link to this post]
 
In reply to a post by Kenneth:
In reply to a post by Taras:
$key="id=22";
$result = mysql_query('SELECT `tblproducts`.`configoption3` '.' FROM tblproducts '.' WHERE `tblproducts`. $key ') or die(mysql_error());
$row = mysql_fetch_assoc($result);

What am I doing wrong with the above in regards $key variable. Basically i want declare the id once and use the $result multiple times (ie resusable ) and change the id value as when and where

is your id column = "tblproductsid"?

I would always just store the the id as a number and do something like
$sql = "SELECT p.configoption3 FROM tblproducts p WHERE p.id = ${id}" if I wasn't using prepared queries as it makes it much clearer what the SQL is

edit: or even `tblproducts`. $key' (you need double quotes to parsed variables I believe) - and I don't believe you need the mysql quote char ` (which makes it hard to read I find)


No the id being refered to is in the table tblproducts - so tblprodutcs.id...

_____________________________________________
<randomness>
streaming music - your music - spotify
Everything websites.... soon
My Blog .......... here (still in dev mode)
Me twittering go add


Register (or login) on our website and you will not see this ad.

Standard User uno
(committed) Sun 06-Jun-10 16:22:41
Print Post

Re: Noob PHP mysql prob :P


[re: Taras] [link to this post]
 
That database structure looks familiar wink

Matt

-
uno Broadband
t: 0808 221 8642
Official Maidenhead, Milton Keynes & Manchester Speedtest.net Host
Our current offers
Standard User Kenneth
(legend) Sun 06-Jun-10 16:28:01
Print Post

Re: Noob PHP mysql prob :P


[re: Taras] [link to this post]
 
In reply to a post by Taras:
No the id being refered to is in the table tblproducts - so tblprodutcs.id...


I suggest you try putting double quotes around the select statement instead of single - link

I'd suggest doing something like

Text
1
23
45
$key="id=22";
$sql = "SELECT  configoption3 FROM tblproducts WHERE ${key} ";// you can then put a debug statement to see what SQL you are calling here
$result = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($result);


Ken

Expecting matters to worsen ensures they do; expecting them to improve is merely foolishness
Standard User Taras
(eat-sleep-adslguide) Sun 06-Jun-10 19:12:56
Print Post

Re: Noob PHP mysql prob :P


[re: Kenneth] [link to this post]
 
Thanks found out what was going wrong had two '.' which were not needed.

final code

$key=22;
$options= "configoption3";
$prodid="SELECT `tblproducts` . `". $options ."`". "FROM tblproducts WHERE `tblproducts`.`id`='$key'";
$result = mysql_query($prodid) or die(mysql_error());


I need to use $options in a number of places other than the sql query!

_____________________________________________
<randomness>
streaming music - your music - spotify
Everything websites.... soon
My Blog .......... here (still in dev mode)
Me twittering go add
Standard User deleted
(deleted) Thu 10-Jun-10 14:31:37
Print Post

Re: Noob PHP mysql prob :P


[re: Taras] [link to this post]
 
Getting error "Unknown column 'tblproducts.$key' in 'where clause' "

Its probably a silly syntax issue but am in programmers blind spot tongue .. As I've not really touched php that much or "programming proper " for years.

What's with all the " '.' " splitting up your query string?

Because you are using single quotes you cannot add a var by just using "$key" as the string must be escaped first ie: " 'some string'.$somevar.'string' ". if you look closely at the query you will see why it has failed. See bellow for my fix.
PHP
1
23
$key="id=22";
$result = mysql_query("SELECT `tblproducts`.`configoption3` FROM tblproducts WHERE `tblproducts`.$key ") or die(mysql_error());$row = mysql_fetch_assoc($result);

What am I doing wrong with the above in regards $key variable. Basically i want declare the id once and use the $result multiple times (ie resusable ) and change the id value as when and where

As long as you don't over write the $results var you can reuse as much as you like. You could store it as a session var so it can be used across multiple pages. Obviously if you change any part of the query you will need to rerun it to get the new result.
  Print Thread

Jump to