PHP parse error using mysql functions in and if statement :S

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
techno_kid
Forum Newbie
Posts: 3
Joined: Tue Apr 21, 2009 2:23 pm

PHP parse error using mysql functions in and if statement :S

Post by techno_kid »

When i define a function:

Code: Select all

<?php
 
// Database Functions
function db($action,$sql){
    if ($action=="connect")
        mysql_connect("$db_host", "$db_user", "$db_password");
    elseif ($action=="connection")
        mysql_connect("$db_host", "$db_user", "$db_password");
    elseif ($action=="disconnect")
        mysql_close($db_connect);
    elseif ($action=="test")
        if (!mysql_connect("$db_host", "$db_user", "$db_password"))
            die('Cannot connect to database: ' . mysql_error());
        elseif (mysql_connect("$db_host", "$db_user", "$db_password"))
            echo('Connection seems to be active');
        else
            echo('Cannot tell whether database is connected or not :S How strange!');
    elseif($action=="createtable")
        if ($sql)
            mysql_select_db($db_name,mysql_connect("$db_host", "$db_user", "$db_password"));
            mysql_query($sql,mysql_connect("$db_host", "$db_user", "$db_password"));
        else
            echo('Please Provide SQL');
    elseif ($action=="validate")
        echo("validate");
    else
        echo('Invalid request');
}
 
?>
and call it with:

Code: Select all

db(connect,na);
db(createtable,"CREATE TABLE Persons(FirstName varchar(15),LastName varchar(15),Age int)");
i get:

Code: Select all

 
Parse error: parse error in C:\xampp\htdocs\cms\inc\db.php on line 22
 
why does this happen? it only appears to happen when i use two lines in an if statement :S
gregor171
Forum Newbie
Posts: 22
Joined: Thu Apr 16, 2009 5:09 pm
Location: Ljubljana, Slovenia

Re: PHP parse error using mysql functions in and if statement :S

Post by gregor171 »

Looks like error in line 22.

Code: Select all

        if ($sql)[color=#FF0000]{[/color]
             mysql_select_db($db_name,mysql_connect("$db_host", "$db_user", "$db_password"));
             mysql_query($sql,mysql_connect("$db_host", "$db_user", "$db_password"));
        [color=#FF0000]}[/color]else
 
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: PHP parse error using mysql functions in and if statement :S

Post by Pulni4kiya »

This is PHP, not python :)

If you have more than one statement, you must put them in { }

Code: Select all

 
if ($sql) {
    mysql_select_db($db_name,mysql_connect("$db_host", "$db_user", "$db_password"));
    mysql_query($sql,mysql_connect("$db_host", "$db_user", "$db_password"));
} else {
    echo('Please Provide SQL');
}
 
techno_kid
Forum Newbie
Posts: 3
Joined: Tue Apr 21, 2009 2:23 pm

Re: PHP parse error using mysql functions in and if statement :S

Post by techno_kid »

thanks, it cleared the error message but it still hasn't created the table like i wanted it to
techno_kid
Forum Newbie
Posts: 3
Joined: Tue Apr 21, 2009 2:23 pm

Re: PHP parse error using mysql functions in and if statement :S

Post by techno_kid »

and does that mean i only need {} with more than one line of code between each if or all the time?
Post Reply