0

Hope someone can help me

I have declared the variable

 <?php $sitename = "http://" .$_SERVER["SERVER_NAME"]; ?>

and would like to use the variable in a mysql query:

$query_rs_main = "SELECT * FROM g_page WHERE g_page_site = "echo $sitename" AND g_page_url = '/index.asp'";

How do I do the "echo $sitename" part? thanks

4 Answers 4

3
$sitename = "http://" .$_SERVER["SERVER_NAME"];
$sitename = mysql_real_escape_string($sitename);
$query_rs_main = "SELECT * FROM g_page WHERE g_page_site = '" . $sitename . "' AND g_page_url = '/index.asp'";
Sign up to request clarification or add additional context in comments.

2 Comments

Hi hsz - This works for me, I learned something new as well with the mysql_real_escape_string($sitename); function! - Thanks
Your welcome ! Use mysql_real_escape_string always when you have to put any untrusted data in SQL queries. If you use integer values, parse it to int: $id = (int) $_GET['id'], etc
1
$query_rs_main = "SELECT * FROM g_page WHERE g_page_site = '". mysql_real_escape_string($sitename) . "' AND g_page_url = '/index.asp'";

Two things to pull away from this:

  1. You don't want to echo a variable "into" a sql query, this just doesn't make sense. You want to concatonate the variable with the rest of the string with the "." operator.

  2. You ALWAYS want to sanitize your input when inserting something into a database. In this case you want to escape your string to prevent SQL injections.

Comments

0
$query_rs_main = "SELECT * FROM g_page WHERE g_page_site = '".$sitename."' AND g_page_url = '/index.asp'";

Comments

-1
$query_rs_main = "SELECT * FROM g_page WHERE g_page_site = $sitename AND g_page_url = '/index.asp'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.