quite an advanced if statement..

  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1243
  • Loc: UK
  • Status: Offline

Post July 9th, 2004, 5:18 am

hey,

Say if i have a script that only runs when certain variables are entered...

I want to make an if statement so...

"If" that script has run then do this action...

} else { "do nothing".

how will i be able to achieve this? I will need to name the code that runs once certain variables are placed?
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 9th, 2004, 5:18 am

  • Veter
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 08, 2004
  • Posts: 20
  • Status: Offline

Post July 9th, 2004, 5:33 am

Put your scripts in functions and call to the functions when you need it.

Use returning values then you need to know if your script is done or not.
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Joined: Jul 28, 2003
  • Posts: 774
  • Loc: Brisbane, Australia
  • Status: Offline

Post July 9th, 2004, 5:45 am

Uh, what language is this? If it's PHP, then, what he said
  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1243
  • Loc: UK
  • Status: Offline

Post July 9th, 2004, 6:21 am

so for example

PHP Code: [ Select ]
$function1 = "code here";


then
PHP Code: [ Select ]
"if (function1 = 1);
 
 
 
second code here
  1. "if (function1 = 1);
  2.  
  3.  
  4.  
  5. second code here


yes this is php, im kind of new to this but oh well
i need this to be done if the connection to the database is done, so i should really do:

PHP Code: [ Select ]
if (conn = 1)
??
becuase on my connection script i have
PHP Code: [ Select ]
$conn = mysql_connect("$location","$username","$password");
  • Veter
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 08, 2004
  • Posts: 20
  • Status: Offline

Post July 9th, 2004, 6:47 am

PHP Code: [ Select ]
$conn = mysql_connect("$location","$username","$password");
 
 
 
if($conn){ ...do your script... } else {echo "Youre not connected"; }
 
 
 
 
  1. $conn = mysql_connect("$location","$username","$password");
  2.  
  3.  
  4.  
  5. if($conn){ ...do your script... } else {echo "Youre not connected"; }
  6.  
  7.  
  8.  
  9.  


and put @ before mysql_connect()

PHP Code: [ Select ]
$conn = @mysql_connect("$location","$username","$password");


It will not show errors if something will be wrong with mysql server or viriables.
  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1243
  • Loc: UK
  • Status: Offline

Post July 9th, 2004, 6:52 am

what is the "@" for?
GSDomains.com -Click here - Packages starting from £3.69 a month. 1.5GB Space & 10GB Bandwidth.
  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1243
  • Loc: UK
  • Status: Offline

Post July 9th, 2004, 6:53 am

Another Q: how do i insert current date and time in to a mysql row??
GSDomains.com -Click here - Packages starting from £3.69 a month. 1.5GB Space & 10GB Bandwidth.
  • Veter
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 08, 2004
  • Posts: 20
  • Status: Offline

Post July 9th, 2004, 7:02 am

Nem wrote:
what is the "@" for?

Read my post. It said right under the code box.

Nem wrote:
Another Q: how do i insert current date and time in to a mysql row??


Use time(); function
PHP Code: [ Select ]
$curtime = time();

$curtime will have current timestamp.

And then add $curtime in your mysql querry.
  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1243
  • Loc: UK
  • Status: Offline

Post July 9th, 2004, 7:04 am

oh ok, thanks!

but the time bit wont add the date as well though, will it?
  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1243
  • Loc: UK
  • Status: Offline

Post July 9th, 2004, 7:06 am

so altogether:

PHP Code: [ Select ]
 
$conn = @mysql_connect("$location","$username","$password");
 
 
 
if($conn){ ...do your script... } else {echo "Youre not connected"; }
 
 
  1.  
  2. $conn = @mysql_connect("$location","$username","$password");
  3.  
  4.  
  5.  
  6. if($conn){ ...do your script... } else {echo "Youre not connected"; }
  7.  
  8.  



//edited bit underneath

could it be: $lastvisit = date( "F d, Y, H:i, T.");

where $lastvisit is a row
  • Veter
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 08, 2004
  • Posts: 20
  • Status: Offline

Post July 9th, 2004, 7:15 am

You have to extract the date from the timestamp with date() function.

or do like this:
PHP Code: [ Select ]
$time   = date( "Y-m-d H:i:s", time() );
  • Nem
  • Guru
  • Guru
  • No Avatar
  • Joined: Feb 13, 2004
  • Posts: 1243
  • Loc: UK
  • Status: Offline

Post July 9th, 2004, 7:22 am

do you see anything wrong with this?

PHP Code: [ Select ]
 
$conn = @mysql_connect("$location","$username","$password");
 
if ($conn) {
 
 
 
$ip = $_SERVER["REMOTE_ADDR"];
 
$user = $_POST["user"] ;
 
$pass = $_POST["pass"];
 
$lastvisit = date( "F d, Y, H:i, T.");
 
 
 
$sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
 
        VALUES ('$ip','$lastvisit','$user', '$pass')";  
 
    $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
 
   echo = "creating your admin account";
 
   
 
   } else {
 
   echo ("Could not connect"); }
 
 
  1.  
  2. $conn = @mysql_connect("$location","$username","$password");
  3.  
  4. if ($conn) {
  5.  
  6.  
  7.  
  8. $ip = $_SERVER["REMOTE_ADDR"];
  9.  
  10. $user = $_POST["user"] ;
  11.  
  12. $pass = $_POST["pass"];
  13.  
  14. $lastvisit = date( "F d, Y, H:i, T.");
  15.  
  16.  
  17.  
  18. $sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
  19.  
  20.         VALUES ('$ip','$lastvisit','$user', '$pass')";  
  21.  
  22.     $query = mysql_query($sql) or die("Cannot query the database.<br>" . mysql_error());
  23.  
  24.    echo = "creating your admin account";
  25.  
  26.    
  27.  
  28.    } else {
  29.  
  30.    echo ("Could not connect"); }
  31.  
  32.  
GSDomains.com -Click here - Packages starting from £3.69 a month. 1.5GB Space & 10GB Bandwidth.
  • Scorpius
  • Proficient
  • Proficient
  • User avatar
  • Joined: Mar 20, 2004
  • Posts: 401
  • Loc: Scorpion Hole
  • Status: Offline

Post July 9th, 2004, 7:32 am

Well you have an un needed space
PHP Code: [ Select ]
$user = $_POST["user"] ;

change to
PHP Code: [ Select ]
$user = $_POST["user"];

don't know if thats the only one, but only one i see.
  • Veter
  • Novice
  • Novice
  • User avatar
  • Joined: Jul 08, 2004
  • Posts: 20
  • Status: Offline

Post July 9th, 2004, 7:41 am

PHP Code: [ Select ]
echo ("Could not connect");

should be
PHP Code: [ Select ]
echo "Could not connect";
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Joined: May 21, 2004
  • Posts: 3229
  • Loc: South Africa
  • Status: Offline

Post July 9th, 2004, 8:06 am

For the date time you could also use the SQL NOW() function.
PHP Code: [ Select ]
 
$conn = @mysql_connect("$location","$username","$password");
 
if ($conn) {
 
 
 
$ip = $_SERVER["REMOTE_ADDR"];
 
$user = $_POST["user"] ;
 
$pass = $_POST["pass"];
 
 
 
$sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
 
       VALUES ('$ip',NOW(),'$user', '$pass')";      
 
    $query = @mysql_query($sql);
 
   if (mysql_errno($conn) > 0){
 
        echo = "A database error occured<br>" . mysql_error();
 
    }else{
 
        echo = "creating your admin account";    
 
    }
 
  } else {
 
    echo ("Could not connect");
 
}
 
 
  1.  
  2. $conn = @mysql_connect("$location","$username","$password");
  3.  
  4. if ($conn) {
  5.  
  6.  
  7.  
  8. $ip = $_SERVER["REMOTE_ADDR"];
  9.  
  10. $user = $_POST["user"] ;
  11.  
  12. $pass = $_POST["pass"];
  13.  
  14.  
  15.  
  16. $sql = "INSERT INTO gs_admin (ip, lastvisit, user, pass)
  17.  
  18.        VALUES ('$ip',NOW(),'$user', '$pass')";      
  19.  
  20.     $query = @mysql_query($sql);
  21.  
  22.    if (mysql_errno($conn) > 0){
  23.  
  24.         echo = "A database error occured<br>" . mysql_error();
  25.  
  26.     }else{
  27.  
  28.         echo = "creating your admin account";    
  29.  
  30.     }
  31.  
  32.   } else {
  33.  
  34.     echo ("Could not connect");
  35.  
  36. }
  37.  
  38.  


is the way I would deal with this. Why dont you just write your connection to a function
PHP Code: [ Select ]
 
function openCon(){
 
 $db = connect to db;
 
 if ($db){
 
 return $db;
 
 }else{
 
  return false;
 
 }
 
}
 
 
 
if ($db_con = openCon()){
 
//process
 
}else{
 
//failed to connect
 
}
 
 
  1.  
  2. function openCon(){
  3.  
  4.  $db = connect to db;
  5.  
  6.  if ($db){
  7.  
  8.  return $db;
  9.  
  10.  }else{
  11.  
  12.   return false;
  13.  
  14.  }
  15.  
  16. }
  17.  
  18.  
  19.  
  20. if ($db_con = openCon()){
  21.  
  22. //process
  23.  
  24. }else{
  25.  
  26. //failed to connect
  27.  
  28. }
  29.  
  30.  


HTH
Watch me grow
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post July 9th, 2004, 8:06 am

Post Information

  • Total Posts in this topic: 20 posts
  • Users browsing this forum: No registered users and 197 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.