Jump to content

[SOLVED] Help with php and mysql


syntax101

Recommended Posts

<?php

 

if (!isset($_SESSION)) {

  session_start();

}

 

?>

 

 

correct if the code is:

$sql = 'SELECT * FROM orders WHERE transaction <> "COMPLETE" and cust_num = (Select cust_num from customer where cust_username = "squall")';

 

squall is a username the value

note:it returns a query

 

 

MY PROBLEM

wrong if the code is:

 

 

$sql = 'SELECT * FROM orders WHERE transaction <> "COMPLETE" and cust_num = (Select cust_num from customer where cust_username = '$_SESSION['MM_Username']);

 

$_SESSION['MM_Username']

 

can anyone help me how do i query the session variable why is it wrong and i try to change the aphpstrophe but it doesnt return any query

 

Link to comment
Share on other sites

For a start, it looks like you're trying to take advantage of PHP's magic quotes, but that only works if you're using double quotes, not single quotes.

 

e.g.

 

$a  = "Hello";

echo "$a and goodbye";

output: Hello and goodbye

 

However, IMO, it's better practise to strucutre MySQL queries using sprintf, as you can see more clearly where your variables etc. are.

 

your code would become:

$sql = sprintf('SELECT * FROM orders WHERE transaction <> "COMPLETE" and cust_num = (Select cust_num from customer where cust_username = "%s")', $_SESSION['MM_Username']);

Link to comment
Share on other sites

This is also redundant.  It's not broke, but it's not required either.  session_start() will resume a session if it's already started or create a new if it isn't on it's own

 


<?php

if (!isset($_SESSION)) {
 session_start();
}

?>

 

to just

 

<?php
session_start();
?>

Link to comment
Share on other sites

For a start, it looks like you're trying to take advantage of PHP's magic quotes, but that only works if you're using double quotes, not single quotes.

 

e.g.

 

$a = "Hello";

echo "$a and goodbye";

output: Hello and goodbye

 

However, IMO, it's better practise to strucutre MySQL queries using sprintf, as you can see more clearly where your variables etc. are.

 

your code would become:

$sql = sprintf('SELECT * FROM orders WHERE transaction <> "COMPLETE" and cust_num = (Select cust_num from customer where cust_username = "%s")', $_SESSION['MM_Username']);

 

thanks to all of you guys. your great

thanx a lot man it really work.

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.