Jump to content


Photo

Transfer ID to new query??


  • Please log in to reply
2 replies to this topic

#1 JackW

JackW
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 11 August 2006 - 04:51 PM

I think I have a duh question. I am attaching an ID number to the url
Example someurl?variable1. The ID number is there and I can retrieve it.  How do I get it into my select * from query?


Here is my code:

// open data base
@ $db = mysql_pconnect('someserver.net', 'somedatabase', ' My password');
mysql_select_db('somedatabase1') or die( "Unable to select database");
//fetch Row ID number
$var1 = $_GET['variable1'];
echo $var1;
{
$query= 'SELECT * FROM `sometable` WHERE `ID` LIKE ??  LIMIT 0, 5 ';
$query_results=mysql_query($query);
$match_results=mysql_num_rows($query_results);
}

for ($i=0; $i < $match_results; $i++)
{
$row=mysql_fetch_array($query_results);
echo ($row ['body'] );

}

mysql_close($db);

The variable I want is being transferred and it shows up at echo $var1 just fine.  How do I get it to be where the ?? is in:
$query= 'SELECT * FROM `sometable` WHERE `ID` LIKE ??  LIMIT 0, 5 ';

Everything I have tried has presented me with an error message.

If I replace the ?? with the ID number I do get the results I want, but how do I get it place the ID number in the select line automatically?  ???
An old man trying to learn new tricks.

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 11 August 2006 - 04:54 PM

Depends what you want...
If you want to select the rows where the ID column is equal to $var1, do this:
$query="SELECT * FROM `sometable` WHERE `ID`='$var1'  LIMIT 0, 5";

Btw, you should read about SQL injections and about mysql_real_escape_string()

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 JackW

JackW
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 11 August 2006 - 05:03 PM

Thank you :)

I will give that a try, and also try to find information on SQL injections and about mysql_real_escape_string()
An old man trying to learn new tricks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users