natasha23 Posted February 9, 2010 Share Posted February 9, 2010 Hi I have just started using php and am trying to use a passed variable in my sql query. I can get the passed variable to print on its own but every time i try to add a query it doesn't display anything at all! any help you can give me will be great, i have tried changing the variable and re-arranging the query from what i have seen in my tutorials but nothing seems to be working. I know that the passed variable is working as it changes in the url when the variable is changed. Here is what i have so far: mysql_connect("localhost", $Username, $Password); mysql_select_db($Database);//connecting to database echo $clickedon;//passed query - this prints out until i try adding a query as well echo $elememnts ('SELECT * FROM match WHERE Name = '.$clickedon.''); // one of my attempts to match the passed variable to the name in the table. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted February 9, 2010 Share Posted February 9, 2010 Hi I have just started using php and am trying to use a passed variable in my sql query. I can get the passed variable to print on its own but every time i try to add a query it doesn't display anything at all! any help you can give me will be great, i have tried changing the variable and re-arranging the query from what i have seen in my tutorials but nothing seems to be working. I know that the passed variable is working as it changes in the url when the variable is changed. Here is what i have so far: mysql_connect("localhost", $Username, $Password); mysql_select_db($Database);//connecting to database echo $clickedon;//passed query - this prints out until i try adding a query as well echo $elememnts ('SELECT * FROM match WHERE Name = '.$clickedon.''); // one of my attempts to match the passed variable to the name in the table. what is this $elememnts? I am slightly confused at what you are trying to do but your query string is wrong. It should be the following: "SELECT * FROM match WHERE Name = '" . $clickedon . "'" The first is a double quote, second is a single quote then double quote, third is a double quote, single quote,double quote. Also if you are trying to query the database then you will need to use the mysql_query function with this string $result = mysql_query("SELECT * FROM match WHERE Name='" . $clickedon . "'"); Then you will need to used a loop in order to extract each row of the result like such while($row=mysql_fetch_array($result)){ echo $row['Name']; } That will print out each name that matches $clickedon Quote Link to comment Share on other sites More sharing options...
natasha23 Posted February 10, 2010 Author Share Posted February 10, 2010 Thanks for your advice, i'm still having problems printing anything out. If i get rid of the WHERE clause then it pulls all the names in my database so i think the problem is still in: WHERE $clickedon= $get_['clickedon']; $Elements = mysql_query("SELECT * FROM elements" ); //WHERE Name= '".$clickedon."'"); -- if i get ris of this then it pulls all the names from the database, when i add it back to the mysql query the results are blank. while($Row=mysql_fetch_array($Elements)) { echo $Row['Name']; Quote Link to comment Share on other sites More sharing options...
mattal999 Posted February 10, 2010 Share Posted February 10, 2010 This: $clickedon= $get_['clickedon']; Should be: $clickedon= $_GET['clickedon']; Quote Link to comment Share on other sites More sharing options...
natasha23 Posted February 10, 2010 Author Share Posted February 10, 2010 K, Thank you i have changed GET but it still does not display any results. Is there something else that i am missing? Quote Link to comment Share on other sites More sharing options...
mattal999 Posted February 10, 2010 Share Posted February 10, 2010 Ok can you just do this to see what query is actually being run: echo "SELECT * FROM match WHERE Name='" . $clickedon . "'"; And post the output. Quote Link to comment Share on other sites More sharing options...
natasha23 Posted February 10, 2010 Author Share Posted February 10, 2010 ok, it now prints out SELECT * FROM elements WHERE Name= '' my ' " . $clickedon . " ' " is still red in my select statement, in all the examples i've looked at its blue but i cant see whats different to what i have. Quote Link to comment Share on other sites More sharing options...
mattal999 Posted February 10, 2010 Share Posted February 10, 2010 Can you just post the entire code and the URL that you use to view the page (with the parameters) so I can take a look through it. Quote Link to comment Share on other sites More sharing options...
natasha23 Posted February 10, 2010 Author Share Posted February 10, 2010 mysql_connect("localhost", $Username, $Password); mysql_select_db($Database); $clickedon = $GET_['clickedon']; echo "SELECT * FROM elements WHERE clickedon= '". $clickedon ."'"; while($Row=mysql_fetch_array($Result)) { echo $Row['Name']; } and this is the code that i use to pass the variable: echo "<A href='info.php?clickedon= ".$Row['Name']." ' Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted February 10, 2010 Share Posted February 10, 2010 $GET_['clickedon'] should be $_GET['clickedon'] You're also not running your SQL query properly. To run mysql queries you need to use the mysql_query function. So this echo "SELECT * FROM elements WHERE clickedon= '". $clickedon ."'"; while($Row=mysql_fetch_array($Result)) { echo $Row['Name']; } Should be $Result = mysql_query("SELECT * FROM elements WHERE clickedon= '". $clickedon ."'"); if(mysql_num_rows($Result) > 0) { while($Row=mysql_fetch_array($Result)) { echo $Row['Name']; } } Quote Link to comment Share on other sites More sharing options...
natasha23 Posted February 10, 2010 Author Share Posted February 10, 2010 I have tried this before and it comes up with blank results. If you get rid of the WHERE clause though every name appears thats in the database. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted February 10, 2010 Share Posted February 10, 2010 In order for the code to work properly you need to be going to info.php?clickedon=somevaluehere Going directly to info.php will not produce any results. Can you post the url you're using to access info.php? Quote Link to comment Share on other sites More sharing options...
natasha23 Posted February 10, 2010 Author Share Posted February 10, 2010 the variable that i passed through the URL changes depending on what i clicked on so if i click on Hydrogen on my 1st page this is the URL: info.php?clickedon=%20Hydrogen and this is the code i use to pass Name into the URL <A href='info.php?clickedon= ".$Row['Name']."' Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted February 10, 2010 Share Posted February 10, 2010 Remove the space after the equals sign when generating your links echo '<A href="info.php?clickedon="'.$Row['Name'].'">'.$Row['Name'].'</a>'; Quote Link to comment Share on other sites More sharing options...
natasha23 Posted February 10, 2010 Author Share Posted February 10, 2010 thank you for all your help, it is now working! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.