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.

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

 

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.