Hi
I need to be able to randomly have one of two responses that product an SQL query code.
So random result one will give a "no", and random result two will product the query.
How would I do this?
Randomly producing True or False - how?
Moderator: General Moderators
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Randomly producing True or False - how?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
- flying_circus
- Forum Regular
- Posts: 732
- Joined: Wed Mar 05, 2008 10:23 pm
- Location: Sunriver, OR
Re: Randomly producing True or False - how?
Code: Select all
<?php
$random = rand(0,1);
if($random == 0) {
echo 'No';
} else {
echo 'Query';
}
?>- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
Re: Randomly producing True or False - how?
Code: Select all
$var = rand(0,1) ? 'No' : 'SELECT ...';-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Randomly producing True or False - how?
I understand flying_circus's, but not Jonah's.... Jonah can you explain how yours operates?
This is the sql that would need to be placed in there:
This is the sql that would need to be placed in there:
Code: Select all
$sqlconn=@mysql_connect("localhost","root","");
$rs=@mysql_select_db("dbname",$sqlconn);Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
-
simonmlewis
- DevNet Master
- Posts: 4435
- Joined: Wed Oct 08, 2008 3:39 pm
- Location: United Kingdom
- Contact:
Re: Randomly producing True or False - how?
Code: Select all
<?php
$random = rand(0,1);
if($random == 0) {
echo 'Sorry there is a fault';
} else {
$sqlconn=@mysql_connect("localhost","root","");
$rs=@mysql_select_db("db_name",$sqlconn);
}
?>Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
All the best from the United Kingdom.
Re: Randomly producing True or False - how?
See ternary operator.simonmlewis wrote:I understand flying_circus's, but not Jonah's.... Jonah can you explain how yours operates?
That's no SQL, just some php code to create a connection to an SQL serverThis is the sql that would need to be placed in there:
Re: Randomly producing True or False - how?
Ternary operator is basically:Jonah Bron wrote:Code: Select all
$var = rand(0,1) ? 'No' : 'SELECT ...';
some variable = (condition) ? (result if condition == TRUE) : (result if condition == FALSE)
rand(0,1) will produce either a 0 or a 1. PHP interprets 1 as equivalent to boolean TRUE, so the first result will be used. PHP interprets 0 as equivalent to boolean FALSE, so the second result will be used.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.