How to cancel a select statement when a WHERE parameter is empty or does not exist? [duplicate]
This question already has an answer here:
check if row exists with mysql
3 answers
“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
27 answers
I hope someone can help me on this? Here's my select statement based on a session variable.
$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
I want to be able to SELECT IF the session variable 'user' exists... IF NOT, I do not want to select anything and cancel the database query so I don't see a 'Notice: Undefined index: error' notice?
Any help on how this is done would be very much appreciated.
Thank you.
Steve
php database if-statement session
marked as duplicate by Alon Eitan, sticky bit, Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 22:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
check if row exists with mysql
3 answers
“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
27 answers
I hope someone can help me on this? Here's my select statement based on a session variable.
$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
I want to be able to SELECT IF the session variable 'user' exists... IF NOT, I do not want to select anything and cancel the database query so I don't see a 'Notice: Undefined index: error' notice?
Any help on how this is done would be very much appreciated.
Thank you.
Steve
php database if-statement session
marked as duplicate by Alon Eitan, sticky bit, Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 22:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
check if row exists with mysql
3 answers
“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
27 answers
I hope someone can help me on this? Here's my select statement based on a session variable.
$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
I want to be able to SELECT IF the session variable 'user' exists... IF NOT, I do not want to select anything and cancel the database query so I don't see a 'Notice: Undefined index: error' notice?
Any help on how this is done would be very much appreciated.
Thank you.
Steve
php database if-statement session
This question already has an answer here:
check if row exists with mysql
3 answers
“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
27 answers
I hope someone can help me on this? Here's my select statement based on a session variable.
$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
I want to be able to SELECT IF the session variable 'user' exists... IF NOT, I do not want to select anything and cancel the database query so I don't see a 'Notice: Undefined index: error' notice?
Any help on how this is done would be very much appreciated.
Thank you.
Steve
This question already has an answer here:
check if row exists with mysql
3 answers
“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP
27 answers
php database if-statement session
php database if-statement session
edited Jan 1 at 21:52
Funk Forty Niner
1
1
asked Jan 1 at 21:39
Steven CheshireSteven Cheshire
105
105
marked as duplicate by Alon Eitan, sticky bit, Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 22:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Alon Eitan, sticky bit, Funk Forty Niner
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 1 at 22:29
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.
$users = ;
if (isset($_SESSION['user'])) {
$sql = "SELECT * FROM wbcusers WHERE username = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$_SESSION['user']]);
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.
Perfect, thanks Bill, worked a treat. Much appreciated. Steve
– Steven Cheshire
Jan 2 at 21:49
add a comment |
if (isset($_SESSION['user'])) {
$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
....
}
else
{
//something else
}
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.
$users = ;
if (isset($_SESSION['user'])) {
$sql = "SELECT * FROM wbcusers WHERE username = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$_SESSION['user']]);
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.
Perfect, thanks Bill, worked a treat. Much appreciated. Steve
– Steven Cheshire
Jan 2 at 21:49
add a comment |
Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.
$users = ;
if (isset($_SESSION['user'])) {
$sql = "SELECT * FROM wbcusers WHERE username = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$_SESSION['user']]);
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.
Perfect, thanks Bill, worked a treat. Much appreciated. Steve
– Steven Cheshire
Jan 2 at 21:49
add a comment |
Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.
$users = ;
if (isset($_SESSION['user'])) {
$sql = "SELECT * FROM wbcusers WHERE username = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$_SESSION['user']]);
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.
Set your users array to an empty array initially, then fill it with data if and only if the session variable you need is set.
$users = ;
if (isset($_SESSION['user'])) {
$sql = "SELECT * FROM wbcusers WHERE username = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$_SESSION['user']]);
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
P.S.: Please use query parameters like above, instead of ugly string concat nonsense. It's way easier to write the code, and you don't have to worry about SQL injection vulnerabilities.
edited Jan 2 at 21:52
answered Jan 1 at 21:43
Bill KarwinBill Karwin
380k64518672
380k64518672
Perfect, thanks Bill, worked a treat. Much appreciated. Steve
– Steven Cheshire
Jan 2 at 21:49
add a comment |
Perfect, thanks Bill, worked a treat. Much appreciated. Steve
– Steven Cheshire
Jan 2 at 21:49
Perfect, thanks Bill, worked a treat. Much appreciated. Steve
– Steven Cheshire
Jan 2 at 21:49
Perfect, thanks Bill, worked a treat. Much appreciated. Steve
– Steven Cheshire
Jan 2 at 21:49
add a comment |
if (isset($_SESSION['user'])) {
$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
....
}
else
{
//something else
}
add a comment |
if (isset($_SESSION['user'])) {
$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
....
}
else
{
//something else
}
add a comment |
if (isset($_SESSION['user'])) {
$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
....
}
else
{
//something else
}
if (isset($_SESSION['user'])) {
$users = 'SELECT * FROM wbcusers WHERE username = "'.$_SESSION['user'].'" ';
....
}
else
{
//something else
}
edited Jan 1 at 21:53
answered Jan 1 at 21:43
SimonareSimonare
14.8k11840
14.8k11840
add a comment |
add a comment |