Jump to content

Is there an 'OR' statement in PHP?


xProteuSx

Recommended Posts

I cannot find anything regarding an 'OR' type operator/statement in PHP.  I seem to remember functions like that existing in other programming languages, so I figure there has to be something like that here.  Here is the sort of scenario that I am looking at:

[code]if ($password1 != $password2) or ($password1 == "") { ...[/code]

Thanks in advance.  ???
Link to comment
Share on other sites

I am really stuck on my project, and no one has been able to help me with this:  I wrote code to create a table in MySQL but once I have created the table I am not able to DROP, TRUNCATE, or do anything else except for add and query records.  I have all rights to the DB and I am sure that my connection snippet is correct.  Could someone please take 5 minutes to help?  I'll post all the code below.  Its very amateur so it is easy to understand. 

Index.php collects four variables ($serverurl, $nameofuser, $passofuser, and $databasename).  It then goes to index2.php.

Index2.php uses the fwrite command to create dbconnect.php which stores the connection string.  Then it uses the connection string in the format of an include statement and creates a table called 'users'.  It also collects four more variables ($username, $password1, $password2, and $email) then it proceeds to index3.php.  These variables and index3.php are inconsequential.  My mistake/error is somewhere within index.php, index2.php or dbconnect.php.

index.php -->

[code]
<body>
<form method="post" action="index2.php">
<p>Server URL:  <input type="text" name="serverurl" size="30"></p>
<p>Database Name:  <input type="text" name="databasename" size="20"></p>
<p>Database Username:  <input type="text" name="nameofuser" size="20"></p>
<p>Database Password:  <input type="password" name="passofuser" size="20"></p>
<input type="submit" value="Submit">
</form>
</body>
[/code]

index2.php -->

[code]
<body>

<?php
//opens a new file and writes the database connection string to it then closes this new file
$connectfile = fopen("dbconnect.php","w");
fwrite($connectfile, "
<?php \$dbh=mysql_connect (\"$serverurl\", \"$nameofuser\", \"$passofuser\")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db (\"$databasename\");?>");
fclose($connectfile);
echo "Got 'er!<br><br>";

include ("dbconnect.php");

$sql = "
DROP TABLE users;
CREATE TABLE users (
users_id int(6) NOT NULL auto_increment,
users_handle varchar(20) default NULL,
users_password varchar (20) default NULL,
users_email varchar (40) default NULL,
users_datejoined timestamp NOT NULL,
users_visits int (6) default '0',
users_lastvisit timestamp NOT NULL,
users_questionsanswered int(6) default '0',
users_correctanswers int(6) default '0',
users_percentcorrect float default '0',
users_totalscore int (6) default '0',
users_pagesviewed int(8) default '0',
users_visitbonus int(6) default '0',
users_activity int(6) default '0',
PRIMARY KEY(users_id),
UNIQUE(users_handle)
) TYPE=MYISAM;
";

?>

<form method="post" action="index3.php">
<p>New Username  <input type="text" name="username" size="30"></p>
<p>Password:  <input type="password" name="password1" size="20"></p>
<p>Confirm Password:  <input type="password" name="password2" size="20"></p>
<p>E-Mail:  <input type="text" name="email" size="20"></p>
<input type="submit" value="Submit">
</form>

</body>
[/code]

dbconnect.php -->

[code]
<?php$dbh=mysql_connect ("myhost", "myusername", "mypassword")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("mydatabase");?>
[/code]

Now, if you look at the [color=red]DROP TABLE users;[/color] near the top of index2.php, that doesn't work.  Neither does it work if I replace it with TRUNCATE TABLE users;  HELP!!!!!!  I'm stuck.
Link to comment
Share on other sites

you STRINGified the rest of your code and exited PHP at the same time, that's why it doesn't work

look here
[quote]
<?php
//opens a new file and writes the database connection string to it then closes this new file
$connectfile = fopen("dbconnect.php","w");
fwrite($connectfile, [size=16pt]"[/size] [b]<-- You forgot to close your quote[/b]
<?php \$dbh=mysql_connect (\"$serverurl\", \"$nameofuser\", \"$passofuser\")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db (\"$databasename\");?>[size=16pt]"[/size]); <--So it thinks this is the end of the string I.E. your argument for fwrite
[/quote]

and also you, for some reason opened another PHP tag (labeled in green above)

change that snippet to this and you should be fine

[code=php:0]
<?php
//opens a new file and writes the database connection string to it then closes this new file
$connectfile = fopen("dbconnect.php","w");
fwrite($connectfile, "");
$dbh=mysql_connect ($serverurl, $nameofuser, $passofuser)
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ($databasename);
[/code]
Link to comment
Share on other sites

[quote]What purpose does the variable '$connection' serve?[/quote]
You're right on one note....
theoretically you don't have to assign the connection to a variable
once you initialize the function it does it's thing and connects like you want, which is all you want at that point.

But then you might want to error check.
when you run mysql_connect();
it will try to connect and if it does it....for the sake of less explanation..........it doesn't return FALSE.

that way later you can check your variable to make sure it's not FALSE to make sure you actually connected.



Also, suppose you had multiple MySQL servers.
I've never seen anyone do it, but you can assign a connection to a multiple variables....
that way you can say
select database A on this Server
and then select database Y on another Server.
otherwise, whenever you try to select a database with
mysql_select_db()
it just uses the default connection, or the ONLY connection you've made
Link to comment
Share on other sites

zanus, I have tried what you suggest, though I cannot find where I didn't close the quote:

[code]
fwrite($connectfile, " <---opening quote
<?php \$dbh=mysql_connect (\"$serverurl\", \"$nameofuser\", \"$passofuser\")
or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db (\"$databasename\");?>" <---closing quote);
[/code]

I changed the snippet to what you posted above, and I still get an error if I have the DROP TABLE users line in index2.php.  Without that line everything is fine.

This is what confuses the crap out of me (among many other things):  if the DROP TABLE command is not present and the table 'users' does not exist all goes smooth meaning that a new table named 'users' is created.  If the table 'users' does exist before the code is executed I simply get a message stating that 'Table 'users' already exists'. 

Now, if I add the line "DROP TABLE users;" above "CREATE TABLE users (..." the code should delete the table 'users' then create a new one, right?  Instead I get the error "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '; CREATE TABLE users ( users_id int(6) NOT NULL auto_increment,". 

If I remove the semicolon I simply get the error "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE users ( users_id int(6) NOT NULL auto_increment, u"

I am at a total loss.

And what about the fact that mysql_select_db does take 2 arguments?  You only show one in your code.  Is this correct?  Thanks zanus!
Link to comment
Share on other sites

I don't know exactly why you're DROP statement fails, but I'm almost sure that users is reserved somewhere in MySQL, maybe if you put backticks around it it would work, but nevermind that way anyway..

It's better to use the IF EXISTS method...example


[code]
DROP TABLE IF EXISTS `my_users`;
CREATE TABLE IF NOT EXISTS `my_user` (
......blah-blah-blah[/code]


[quote]And what about the fact that mysql_select_db does take 2 arguments?  You only show one in your code.  Is this correct?  Thanks zanus![/quote]

[quote]Description
bool mysql_select_db ( string database_name [, resource link_identifier] )
[color=green]Notice the second argument is in brackets.
In most all programming syntax manuals, brackets mean optional.[/color]


Sets the current active database on the server that's associated with the specified link identifier. Every subsequent call to mysql_query() will be made on the active database.
Parameters

database_name

    The name of the database that is to be selected.
link_identifier

    The MySQL connection. [b]If the link identifier is not specified, the last link opened by mysql_connect() is assumed.[/b] If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level warning is generated.[/quote]



[b]EDIT: I MERGED YOUR TWO TOPICS TOGETHER SINCE THEY HAD TO DO WITH THE SAME QUESTION[/b]
Link to comment
Share on other sites

Well, I am unsure why this works, but it does.

[code]
include ("dbconnect.php");

$sql1 = "DROP TABLE users";

$result1 = MYSQL_QUERY($sql1);
if (!$result1){
print mysql_error();
}

$sql = "
CREATE TABLE users (
users_id int(6) NOT NULL auto_increment,
users_handle varchar(20) default NULL,
users_password varchar (20) default NULL,
users_email varchar (40) default NULL,
users_datejoined timestamp NOT NULL,
users_visits int (6) default '0',
users_lastvisit timestamp NOT NULL,
users_questionsanswered int(6) default '0',
users_correctanswers int(6) default '0',
users_percentcorrect float default '0',
users_totalscore int (6) default '0',
users_pagesviewed int(8) default '0',
users_visitbonus int(6) default '0',
users_activity int(6) default '0',
PRIMARY KEY(users_id),
UNIQUE(users_handle)
) TYPE=MYISAM;
";

$result = MYSQL_QUERY($sql);
if (!$result){
print mysql_error();
exit;
[/code]

I don't like making things work and then keep going without first understanding why they worked.  For some reason if I dropped the table as a separate entity everything works out.  Again, I think this has something to do with the whole $sql variable, which I am hazy on.  I just made another, $sql1, and did things that way.  Querky ...

On another note, Zanus, thank you VERY MUCH for your help.  Please do not lose patience with me.  I have 3 text books in front of me and 4 tutorials opened up on my desktop.  I'm not trying to make anyone write the code for me.  Really, I am trying to learn.  The help I get at PHPFreaks accelerates my learning process.  What an awesome community!
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.