0

I want to create dynamically a new php file. Inside the new file i want my sql query. Here's my code

    $myfile = "".$file.".php"; // or .php  
    //echo $myFile;
    $fh = fopen($myfile, 'w'); // or die("error");  
    $stringData = '<?php

            $sql = "SELECT * FROM users, epixeir WHERE users.user = ".$_SESSION["user"]." AND user.pass = ".$_SESSION["pass"]." ;";
            $result = $conn->query($sql);
            ?>

    ';   
    fwrite($fh, $stringData);

In new file $myfile there is a "Notice: Trying to get property of non-object".

If i edit it to '".$_SESSION["user"]"' working fine, but this i want to do it dinamically. So if i write my above code like

    $myfile = "".$file.".php"; // or .php  
    //echo $myFile;
    $fh = fopen($myfile, 'w'); // or die("error");  
    $stringData = '<?php

            $sql = "SELECT * FROM users, epixeir WHERE users.user = '".$_SESSION["user"]."' AND user.pass = '".$_SESSION["pass"]."' ;";
          //$sql = "SELECT * FROM users, epixeir WHERE users.user = '".$_SESSION['user']."' AND user.pass = '".$_SESSION['pass']."' ;";
            $result = $conn->query($sql);
            ?>

    ';   
    fwrite($fh, $stringData);

Then i receive "Parse error: syntax error, unexpected '"' "

I'm confused and I need your help.

3
  • Did you try to escape your " character? Try this: $sql = "SELECT * FROM users, epixeir WHERE users.user = '".$_SESSION[\"user\"]."' AND user.pass = '".$_SESSION[\"pass\"]."' ;"; Commented Jun 8, 2015 at 19:15
  • The problem is in the first and last quot '".$_SESSION["user"]."' not there you say Commented Jun 8, 2015 at 19:17
  • 1
    I tried like you say in the first and it's ok, thank you and sorry for my fast answer Commented Jun 8, 2015 at 19:23

3 Answers 3

1

You should use " instead of ' initially and then escape the other " you want in the string. In the example your string has ended in the second '. So all the rest is a syntax error. You can also use double quotes.

There is more information in these links:

Escaping quotation marks in PHP

How to escape strings in SQL Server using PHP?

Sign up to request clarification or add additional context in comments.

Comments

0

Try this....

$sql = "SELECT * FROM users, epixeir WHERE users.user = '".$_SESSION['user']."' AND user.pass = '".$_SESSION['pass']."'";

1 Comment

In first code single quot will close $stringData. I tried myself. James gave me the answer, thank you
0

' is literal " is processed

If you start the string using ' the inner ' need to be escaped. If you star using ", the inner " need to be escaped.

If you use ' the inner variables won't be processed and your string will receive the variables as literal text.

If you use " the variables inside the string will be changed to values before the string is aserted to the variable.

You can escape characters using \ before the character that needs to be literal.

Examples:

$fruit = 'apple';

$ex1 = "I like $fruit"; // I like apple

$ex2 = 'I like $fruit'; // I like $fruit

or escaping special character:

$ex3 = "I like \$fruit"; // I like $fruit

$ex4 = "I like \"$fruit\""; // I like "apple"

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.