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
https://forums.phpfreaks.com/topic/80306-solved-help-with-php-and-mysql/
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']);

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();
?>

hello,

 

you will need to put session variable follows

 

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

 

Thanks.

 

[email protected]

http://www.we4freelance.com

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.

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.