Jump to content

help using a passed variable in an mysql query


natasha23

Recommended Posts

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.

 

 

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

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'];

 

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']." '

$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'];
    }
}

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?

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']."'

 

 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.