I've written the following code to connect to my database, but its throwing up this error:
Parse error: syntax error, unexpected T_STRING in C:\xampp\mysql_connect.php on line 5

<?php # mysql_connect.php

// This file contains the database access information. It also establishes a connection to MySQL and selects the database.
// Set the database access information as constants.
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'sitename');

if ($dbc = my_sql_connect (DB_HOST, DB_USER, DB_PASSWORD))
{
	// Make the connection.

// If it can't select the database.
if (!mysql_select_db (DB_NAME))
{
	// Handle the error.
	trigger_error("Could not select the database.\n<br />MySQL Error: " . mysql_error());

	// Print a message to the user, include the footer, and stop the script.
	include ('./includes/footer.html');
	exit();
}
}

// If it couldn't connect to MySQL.
else
{
	// Print a message to the user, include the footer and stop the script.
	trigger_error("Could not connect to MySQL.\n<br />MySQL Error: " . mysql_error());
	include ('./includes/footer.html');
	exit();
}


// Create a function for escaping the data.
function escape_data ($data)
{
	//Address Magic Quotes.
	if (ini_get('magic_quotes_gpc'))
	{
		$data = stripslashes($data);
	}

// Check for mysql_real_escape_string() support.
if (function_exists('mysql_real_escape_string'))
{
	global $dbc;
	$data = mysql_real_escape_string(trim($data), $dbc);
}

else
{
	$data = mysql_real_escape_string(trin($data));
}

return $data;
}
?>

Does this mean there is a problem with the actual code I have typed? (i can't see any error) or is it a connection issue with the database?

    I think the problem is my_SQL_connect()

    try using the correct function name: mysql_connect()

    better yet, make life easier withe Pear's MDB2 or use PDO.

      i removed the underscore( _ ) and i'm still getting the same error.

        That sounds implausible, since the my_sql_connect should have given you a

        Fatal error: Call to undefined function ...
        

        While the above should work, but possible give you an error indicating unaccepted credentials etc. What is the actual error you get?

          Parse error: syntax error, unexpected T_STRING in C:\xampp\mysql_connect.php on line 5

            There are no parse errors on the first 5 lines in the posted code. The 5th line is the first define statement. The parse error means the error is on that line or somewhere before it. Since it's not in this file, it might be in the couple of lines before it, although I find it unlikely since that most likely should have given an error on the include/require statement. As such, I find it more likely that the code you run doesn't match what's posted here.

              I'm trying to run register.php
              This page runs fine, but when I the click on the register button, that's the error that's returned.
              I don't really know what else to say, here's some of the code for register.php if thats any use?

              // Handle the form
              if(isset($_POST['submitted']))
              {
              	// Connect to the database.
              	require_once ('../mysql_connect.php');
              
              // Check for a first name.
              if (eregi ('[[:alpha:]\.\'\-]{2,15}$', stripslashes(trim($_POST['first_name']))))
              {
              	$fn = escape_data($_POST['first_name']);
              }

                you have syntax error at the end of mysql_connect.php

                        $data = mysql_real_escape_string(trin($data));//syntax error trin(
                    }
                
                return $data; 

                  fixed the syntax error at the end but i'm still getting the same error message

                    There is now no errors in
                    <?php # mysql_connect.php
                    I have run it at my server.

                    Whaever syntax errors you have it is not in mysql_connect you have posted here.
                    But there can be many mysql_connect.php
                    in one harddrive .....

                      a) you can make your code a bit terser by getting rid of the if statements and using "or" instead like so:

                      $dbc = my_sql_connect (DB_HOST, DB_USER, DB_PASSWORD) or trigger_error('Could not connect to MySQL.\n<br />MySQL Error: ' . mysql_error(), E_USER_ERROR);
                      mysql_select_db (DB_NAME)) or trigger_error('Could not select the database.\n<br />MySQL Error: ' . mysql_error(), E_USER_ERROR); 
                      

                      b) you need to identify the exact line where your code fails. You can do this for example by "commenting out" everything and just "uncomment" a few lines from the top. Or an even better way is using eclipse and xdebug or any other IDE with some advanced debugging functionality.

                      e.g. for the "commenting out" variant:

                      <?php # mysql_connect.php 
                      
                      // This file contains the database access information. It also establishes a connection to MySQL and selects the database. 
                      // Set the database access information as constants. 
                      DEFINE ('DB_USER', 'root'); 
                      DEFINE ('DB_PASSWORD', 'password'); 
                      DEFINE ('DB_HOST', 'localhost'); 
                      DEFINE ('DB_NAME', 'sitename'); 
                      
                      // $dbc = my_sql_connect (DB_HOST, DB_USER, DB_PASSWORD) or trigger_error('Could not connect to MySQL.\n<br />MySQL Error: ' . mysql_error(), E_USER_ERROR);
                      //mysql_select_db (DB_NAME)) or trigger_error('Could not select the database.\n<br />MySQL Error: ' . mysql_error(), E_USER_ERROR); 
                      
                      //...more code, that has been commented out
                      //...still more , that....
                      
                      

                      If this code does not return errors you know that your first few lines are fine, then remove the // from the next line and so on.

                      Bjom

                        i had a look in the rest of the hard drive and found another file with the same name. i've changed the file names and it's reading the correct file now 🙂

                        I've tried to run the file again and its telling me that the eregi() function is deprecated. What should I be using instead of this?

                          Write a Reply...