Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Using PHP offline

Options
  • 21-02-2005 12:09pm
    #1
    Closed Accounts Posts: 680 ✭✭✭


    I'm trying to build an sql/php database that can be used offline, but i'm having a bit of trouble getting it working. I downloaded WAMP, which has sql, php, and apache all in one, and sets them all up to work together(apparently). However when i try to run php scripts offline, i can't get them to work. Here's a sample code from the project, used after a user inputs values in fields in a HTML page. The database name is "bands", and i've created a user "david", with password "pass".

    <?

    $db_name = "band";

    $connection = @mysql_connect(&quot;localhost", "david", "pass")
    or die(mysql_error());

    $db = @mysql_select_db($db_name, $connection) or die(mysql_error());

    {
    $sql = "INSERT INTO bookings Values ('$artistname','$venuename','$booked','$fee','$date','$notes')";
    if (!mysql_query($sql))
    {
    echo("<P>Error adding your information " . mysql_error() . "</P>");
    exit;
    }
    }

    header("c:\program files\wamp\www\bands\Index.htm");
    ?>

    Because i've never used php offline before, there's a couple of things i'm not sure of. Is my connection statement correct? Do i even need a connection statement? Is there anything else wrong with my code? Do i have to set something else up before i can use the php? Would i be better off scrapping wamp, and just downloading the components individually?


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    what kind of "not working" do we have here? Error messages? Strange behaviour?


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Sorry, i should have specified. Error messages, pretty much every single message thats included in the "if not" loops.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    And what are the errors it gives?

    Copy & paste a typical output....


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Just the errors in the if not loops, like

    "Error adding your information " . mysql_error() ."

    Whats suspicious about it is that it doesn't give an actual "mysql_error", it just gives the words, like i've put above. That would suggest to me that the sql is not actually working.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Sounds like the mysql module isn't loaded, although with the newer PHP4, it should come as standard, without the need to add the .dll.

    Try remove the @ before the mysql functions and see what happens.

    Can you paste the entire output of the script up here?


  • Advertisement
  • Closed Accounts Posts: 680 ✭✭✭Amaru


    No change!

    Here's the entire output when i try it:

    Error adding your information " . mysql_error() . "

    "); exit; } } header("c:\Program Files\wamp\www\bands\Index.htm"); ?>


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    That's weird, but first error I do see:

    if(!mysql_query($sql))

    mysql_query() requires two arguments. You should have

    mysql_query($sql, $connection)

    [EDIT:

    Or not, just doublechecked. Try using two args anyway.


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    No thats not it. I've been using old php code from a previous project i've done, with a remote database, and all the php code worked fine.

    And something is definitely, definitely wrong. I tried running a simple script, with just one echo statement in it, and nothing happened. There was just a blank screen.


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Yeah, must be a setup prob.

    The script is working fine here on my machine (IIS) i.e. it's giving all the right errors.

    Not familiar with WAMP, sorry.


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Yeah i think it might have been the wrong way to go. I'm looking into downloading the individual components now and setting it up that way.


  • Advertisement
  • Registered Users Posts: 7,868 ✭✭✭The_B_Man


    what version of PHP are u using? PHP4 has built in support for mysql but with PHP5 you have to edit the php.ini to load the php_mysql.dll . just search ur php.ini file for that dll and remove the ";" at the start of the line. also, you'll either have to copy and paste the dll to ur c:\php\ directory or change the path to the extensions to c:\php\extensions or whatever it is. by the way, the installer doesnt come with any of these extensions. download the zip file and extract it to the php directory and make sure to have php.ini in ur windows directory.


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Cheers B Man, but what zip file are you talking about?

    Also, does anybody know if you have to have SP1 installed on XP Pro for all this to work? I read it on a tutorial site, and i'm starting to think thats why its not working. I can't get it either because of my not-quite-legal copy of XP!


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    Amaru - you should try getting phptriad - its excellent for offline php development. It includes mysql via phpmyadmin.

    sokkit is another/same one, but I'm having troubles lately downloading a copy from the website, I think development of it has borked.


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    I actually got wamp working. the problem was that i thought you could place your files anywhere on your computer, but you have to place them in a specified directory. So its working now! although i'm still having trouble inserting variables from a form to the database....... :(


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    Try my lecuterers website. I assume htdocs was the folder you were having problems with ?


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Cheers, i'll check it out. The problem was that you have to put all your files/directorys in a www folder created by wamp, and then access the files by http://localhost/dir_name/files_name.

    Right now, i'm still stuck on putting variables from a form into the database. The problem isn't connecting to the database, because i've had it send values directly, but when i try to send values from a form, it puts a blank entry into the database. Here's the code i'm using(i'm trying to get one value in first, then i'll build up to the amount of fields i need)

    Tester.htm
    <html>
    <head>
    <title>Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <FORM ACTION="test.php" METHOD=POST>
    <p>
    <input type="text" name="test">
    </p>
    <p>
    <input type="submit" name="Submit" value="Submit">
    </p>
    </form>
    </body>
    </html>
    and heres the php file
    <?

    $db_name = "bandhire";

    $connection = mysql_connect()
    or die(mysql_error());

    $db = mysql_select_db($db_name, $connection) or die(mysql_error());

    {
    $sql = "INSERT INTO testing Values ('$test')";
    if (!mysql_query($sql,$connection))
    {
    echo("<P>Error adding your information " . mysql_error() . "</P>");
    exit;
    }
    }

    header("Location: http://localhost/bandhire/Index.htm");
    ?>

    can anybody see the problem? When i used this method for an old project online about a year ago, this worked no problem, but now, it doesn't pass the values. One thing i'm not sure about is where it should be "post" or "get" in the "form action" tag in the html for the form. Anybody?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    try
    $sql = "INSERT INTO testing Values ('".$HTTP_POST_VARS."')";


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Seamus, except for the fact i'm 100% sure you were a guy, i'd kiss you!


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Oh, and in case anybody was in any doubt as to whether i was going to stop being a dumbass for more than a minute, how then would i add 2 or more values? Where in between the 's and "s should my variables go?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Amaru wrote:
    Oh, and in case anybody was in any doubt as to whether i was going to stop being a dumbass for more than a minute, how then would i add 2 or more values? Where in between the 's and "s should my variables go?
    Two or more values to what? :)

    I assume you mean, how do I get and insert multiple form values?

    Let's say you've got a form like
    <form action = "POST" method="./test.php">
    <input type="text" name="var1">
    <input type="text" name="var2">
    <input type="text" name="var3">
    <input type="text" name="var4">
    </form>
    
    Ordinarily, my test.php would have a section similar to
    if(isset($HTTP_POST_VARS)) {
        $var1 = isset($HTTP_POST_VARS['var1'])? $HTTP_POST_VARS['var1'] : "";
        $var2 = isset($HTTP_POST_VARS['var2 '])? $HTTP_POST_VARS['var2 '] : "";
        $var3 = isset($HTTP_POST_VARS['var3 '])? $HTTP_POST_VARS['var3 '] : "";
        $var4 = isset($HTTP_POST_VARS['var3 '])? $HTTP_POST_VARS['var3 '] : "";
    
        $sql = "INSERT INTO myTable (col1, col2, col3, col4) VALUES ('$var1', '$var2', '$var3', '$var4')";
    
        ..........
    }
    

    This line here:
    $var1 = isset($HTTP_POST_VARS)? $HTTP_POST_VARS : "";
    Is a shorthand way of writing:
    if(isset($HTTP_POST_VARS['var1']))
          $var1 = $HTTP_POST_VARS['var1'];
    else
          $var1 = "";
    

    :)


  • Advertisement
  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Cool. But i actually just used the format you gave me, and use multiple ones seperated by commas.

    Now next question, although its possibly the last(though probably not)! Whats pissing me off is that all this worked on the last project i did, but it won't work now! I've had to completely change the code!

    Anyway, i'm trying to retrieve and display several fields like so
    <?
    $db_name = "bandhire";

    $connection = @mysql_connect()
    or die(mysql_error());

    $db = @mysql_select_db($db_name, $connection) or die(mysql_error());

    $sql = "select name, address1, address2, address3, county, country FROM artist WHERE name = 'george';

    $result = @mysql_query($sql,$connection) or die(mysql_error());

    while ($row = mysql_fetch_array($result)){
    $name = $row('name');
    $address1 = $row('address1');
    $address2 = $row('address2');
    $address3 = $row('address3');
    $county = $row('county');
    $country = $row('country');

    $display_block .= "

    <P><tr>
    <td>$name</td>
    <td>$address1</td>
    <td>$address2</td>
    <td>$address3</td>
    <td>$county</td>
    <td>$country</td>
    </P>

    ";
    echo "$display_block";
    }
    ?>
    And i get this
    Parse error: syntax error, unexpected '>' in c:\program files\wamp\www\bandhire\showartist.php on line 23

    Thats this line "<P><tr>" if you're wondering! What gives! And why can't php just be simple again!


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    The parse error is actually a few lines up - line 9.

    Your sql string isn't terminated by a " so it's interpreting all the text up until $display_block .= as part of your sql string.


  • Closed Accounts Posts: 680 ✭✭✭Amaru


    Right you are again, but now it says

    Fatal error: Function name must be a string in c:\program files\wamp\www\bandhire\showartist.php on line 14

    I really can't figure out why this works differently to the last time. The last time i used PHP on a project, my code looked like this
    <?

    $db_name = "projects";
    $table_one = "Travel";
    $table_two = "Personal";

    $connection = @mysql_connect(&quot;********", "user4833", "pw4833")
    or die(mysql_error());

    $db = @mysql_select_db($db_name, $connection) or die(mysql_error());

    $sql = "select Personal.firstname, Travel.region, Travel.destination, Personal.telephone, Personal.email FROM $table_one, $table_two WHERE Travel.username = Personal.username AND Travel.type = 'driv' ORDER BY region";

    $result = @mysql_query($sql,$connection) or die(mysql_error());

    while ($row = mysql_fetch_array($result)){
    $firstname = $row;
    $region = $row;
    $destination = $row;
    $telephone = $row;
    $email = $row;

    $display_block .= "

    <P><tr><td>$firstname</td><td ALIGN = \"center\">$region</td><td ALIGN = \"center\">$destination</td><td>$telephone</td><td>$email</td></P>

    ";

    }

    ?>

    Thats pretty much identical to whats posted above, and that worked fine, but when i try adapt this code, it gives me errors! :mad: :mad: :mad:


  • Closed Accounts Posts: 8,478 ✭✭✭GoneShootin


    In your previous code quote you have
     $display_block .= "
    
    <P><tr>
    <td>$name</td>
    <td>$address1</td>
    <td>$address2</td>
    <td>$address3</td>
    <td>$county</td>
    <td>$country</td>
    </P>
    
    ";
    echo "$display_block";
    }
    

    Wheras above your missing the echo statement. Why not try
    echo "
    <P><table><tr>
    <td>$name</td>
    <td>$address1</td>
    <td>$address2</td>
    <td>$address3</td>
    <td>$county</td>
    <td>$country</td>
    </table>
    </P>
    ";
    


Advertisement