[Solved] Help with my query string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

[Solved] Help with my query string

Post by Addos »

If I run this

Code: Select all

<?php echo nl2br($row_getProperties['properties_idpk']); ?>
I get the correct value of the number 2 returned.

I’m then trying to pass a value to the following but without success and I wonder if it is possible or am I simply getting the syntax wrong. I have tried various ways but most keep throwing different errors.

Code: Select all

$query_rstThumbs = sprintf("SELECT * FROM images WHERE idpk = $row_getProperties['properties_idpk']", $colname_rstThumbs);
Thanks for any help.
B
Last edited by Addos on Thu Aug 18, 2005 8:56 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try

Code: Select all

$query_rstThumbs = sprintf("SELECT * FROM images WHERE idpk = '{$row_getProperties['properties_idpk']}'", $colname_rstThumbs);
with that said, sprintf() doesn't seem like it's a best fit for that query unless $row_getProperties['properties_idpk'] is like %s or something that'd use $colname_rstThumbs..
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks for your help it’s much appreciated.
I have tried what you suggested but it didn’t work and I even removed sprintf() as I actually can get away without this and tried this -

Code: Select all

$query_rstThumbs = "SELECT * FROM images WHERE idpk = '{$row_getProperties['properties_idpk']}'";
but nothing returns the value I want. If I simply try replacing

Code: Select all

{$row_getProperties['properties_idpk']}
with a number i.e. 1 it gives me the desired output in my browser.
I will happily post all the code if necessary but maybe this is sufficient for more help for the moment.
Thank you again
B
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$query_rstThumbs = "SELECT * FROM images WHERE idpk = '".$row_getProperties['properties_idpk']."'";
If that doesn't work.. use the same query and echo it to your browser


Code: Select all

$query_rstThumbs = "SELECT * FROM images WHERE idpk = '".$row_getProperties['properties_idpk']."'"; 
echo $query_rstThumbs;
See if it's giving you what you think it should be giving you.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Still no luck with this. Here are a few of my results. If I echo

Code: Select all

<? echo $row_getProperties['properties_idpk'];?>
the output is 1 which I expect so I know that this is returning a value.

Code: Select all

$query_rstThumbs = "SELECT * FROM images WHERE idpk = '".$row_getProperties['properties_idpk']."'";
Result
SELECT * FROM images WHERE idpk = ''

This works if I hand code it in that I get the correct output in the browser but for some reason I can seem to pass this through a variable.

Code: Select all

$query_rstThumbs = "SELECT * FROM images WHERE idpk = 1";
Result
SELECT * FROM images WHERE idpk = 1

Thanks very much
B
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Hrm, I really do not know why that wouldn't work. Perhaps set it equal to a single variable

Like:

Code: Select all

$var = $row_getProperties['properties_idpk'];

$query_rstThumbs = "SELECT * FROM images WHERE idpk = '$var'";
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post by Jean-Yves »

Try the $var without the single quotes around it. Since it's an integer, it should not need it.

EDIT: Mind you, it should make no difference. I can't see why it would not work either, to be honest.
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Ok I got this sorted but I’m probably showing up my in experience but I simply moved

Code: Select all

mysql_select_db********** **********);
$query_rstThumbs = "SELECT * FROM images WHERE idpk = '".$row_getProperties['properties_idpk']."'";
$rstThumbs = mysql_query($query_rstThumbs, $appartments) or die(mysql_error());
$row_rstThumbs = mysql_fetch_assoc($rstThumbs);
$totalRows_rstThumbs = mysql_num_rows($rstThumbs);
To below this in stead of the other way round!

Code: Select all

$query_getProperties = "SELECT * FROM properties, locations, bedrooms
WHERE properties.properties_idpk = bedrooms.bedrooms_idpk
And properties.locations_idfk = locations.locations_idpk";

if ($_POST['bedrooms_idpk']) {
  $query_getProperties .= ' AND properties.bedrooms_idfk >= ' .stripslashes($_POST['bedrooms_idpk']);
  }
if ($_POST['locations_idpk']) {
  $query_getProperties .= ' AND locations.locations_idpk = ' .$_POST['locations_idpk'];
  }
$getProperties = mysql_query(******** **********) or die(mysql_error());
$row_getProperties = mysql_fetch_assoc($getProperties);
$totalRows_getProperties = mysql_num_rows($getProperties);
and then the value was passed. I obviously didn’t realise the order of the code was this important but maybe you can enlighten me if it is.

Thanks again for all you time.
B
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you did what I suspect, that's a fairly common starting mistake, I'd imagine a lot of us here have done it. :)
Addos
Forum Contributor
Posts: 305
Joined: Mon Jan 17, 2005 4:13 pm

Post by Addos »

Thanks a mil.
Much apprecated and another lesson learnt!
:wink:
Post Reply