test connectivity to mysql issue

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
suet
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Jan 12, 2017 2:36 am

Thu Jan 12, 2017 2:46 am

Hi
am very new to PHP so please excuse the question!
I have created a mysql database on my ISP hosted web server
db name xxxx_zzzz
password Qwerty
db username xxxx_zzzz

I have created a directory called testphp under the public_html directory and added the below file called phpdbtest.php
<?php
$db_host=“localhost”;
$db_username = “xxxx_zzzz”;
$db_pass=“Qwerty”;
$db_name=“xxxx_zzzz”;

mysql_connect(“$db_host”,$db_username”,$db_pass”) or die(mysql_error());
mysql_select_db(“$db_name”) or die(“no database that name”);
?>

then i have created a file called phpdbtest2.php with the below
<?php
include_once “connect_to_phpdbtest.php”;
echo “You are connected to your database”;
?>

then i goto google chrome and access this via www.xxx.com.au/testphp/phpdbtest2.php

i see the below error in chrome

the page www.xxx.com.au
is currently unable to handle this request.
HTTP ERROR 500

and in the error log i see the below

[12-Jan-2017 21:21:47 Antarctica/Macquarie] PHP Parse error: syntax error, unexpected 'are' (T_STRING), expecting ',' or ';' in /home/****/public_html/testphp/phpdbtestfile2.php on line 3

can anyone help me please?
thanks
Sue

AdoptiveSolution
New php-forum User
New php-forum User
Posts: 159
Joined: Wed Jun 15, 2016 8:35 am

Thu Jan 12, 2017 9:17 am

First, use the code button to display code. It's easier to read :

Code: Select all

<?php
$db_host="localhost";
$db_username = "xxxx_zzzz";
$db_pass="Qwerty";
$db_name="xxxx_zzzz";

mysql_connect("$db_host",$db_username",$db_pass") or die(mysql_error());
mysql_select_db("$db_name") or die("no database that name");
?>
and

Code: Select all

<?php
include_once "connect_to_phpdbtest.php";
echo "You are connected to your database";
?>
To get to your error, a " is missing around $db_username and $db_pass :

Code: Select all

mysql_connect("$db_host",$db_username",$db_pass") or die(mysql_error());
And use the correct filename. You use phpdbtest.php for the code and then you include it as connect_to_phpdbtest.php

User avatar
Strider64
php-forum Active User
php-forum Active User
Posts: 290
Joined: Sat Mar 23, 2013 8:24 am

Thu Jan 12, 2017 2:25 pm

You really should be using mysqli or PDO (My Recommendation)

I use CONSTANTS for my database like so:

Code: Select all

<?php
$server_name = filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL);
define('BASE_PATH', realpath(dirname(__FILE__)));
/* Setup constants for local server and remote server */
define('EMAIL_HOST', 'Email Host Name');
define('EMAIL_USERNAME', 'Username');
define('EMAIL_PASSWORD', 'Password');
define('EMAIL_ADDRESS', 'Email Address');
define('EMAIL_PORT', 587); // Local Port Address
define('PRIVATE_KEY', 'Private Key for ReCaptcha');
define("EMAIL_ADDRESS_ONE", "email address");
define("EMAIL_ADDRESS_TWO", "email address");
define("EMAIL_ADDRESS_THREE", "email address");
define("EMAIL_NAME", "Name");
$users = [['email' => 'email address for swift mailer']];
$email_address_one = 'email address for swiftmailer';
if (filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL) == "localhost") {
    define('DATABASE_HOST', 'local host name');
    define('DATABASE_NAME', 'database name');
    define('DATABASE_USERNAME', 'root -> usually this');
    define('DATABASE_PASSWORD', 'password');
    define('DATABASE_TABLE', 'database table name');
} else {
}
then I would have a utilities file (I call mine utilities.inc.php) :

Code: Select all

<?php
/* Turn on error reporting */
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
if (filter_input(INPUT_SERVER, 'SERVER_NAME', FILTER_SANITIZE_URL) == "localhost") {
    error_reporting(-1); // -1 = on || 0 = off
} else {
    error_reporting(0); // -1 = on || 0 = off
}
include 'connect/connect.php'; // Connection Variables:
$db_options = [
    /* important! use actual prepared statements (default: emulate prepared statements) */
    PDO::ATTR_EMULATE_PREPARES => false
    /* throw exceptions on errors (default: stay silent) */
    , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    /* fetch associative arrays (default: mixed arrays)    */
    , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
$pdo = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
Then if I want to create a php registration page I would do this:
register.php

Code: Select all

<?php
require_once 'lib/includes/utilities.inc.php'; // Configuration file settings and autoloaders:
$db_options = [
    /* important! use actual prepared statements (default: emulate prepared statements) */
    PDO::ATTR_EMULATE_PREPARES => false
    /* throw exceptions on errors (default: stay silent) */
    , PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
    /* fetch associative arrays (default: mixed arrays)    */
    , PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
];
$pdo = new PDO('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME . ';charset=utf8', DATABASE_USERNAME, DATABASE_PASSWORD, $db_options);
$submit = filter_input(INPUT_POST, submit);
if ($submit === 'register') {
    
    $password = password_hash(filter_input(INPUT_POST, password), PASSWORD_BCRYPT, ["cost" => 15]);
    
    $query = ' INSERT INTO ' . DATABASE_TABLE . ' (username, password, description) VALUES (:username, :password, :description)';
    
    $stmt = $pdo->prepare($query);
    
    $result = $stmt->execute([':username' => filter_input(INPUT_POST, username), ':password' => $password, ':description' => filter_input(INPUT_POST, description)]);
}
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Registration Tutorial</title>
        <link rel="stylesheet" href="lib/css/reset.css">
        <link rel="stylesheet" href="lib/css/grids.css">
        <link rel="stylesheet" href="lib/css/registerstyle.css">
    </head>
    <body>
        <div class="container">
            <form id="register" class="" action="register.php" method="post">
                <fieldset>
                    <legend>Registration Form</legend>
                    <label for="username">username</label>
                    <input id="username" type="text" name="username" value="" tabindex="1" autofocus>
                    <label for="password">password</label>
                    <input id="password" type="password" name="password" tabindex="2">
                    <label for="verify_password">verify password</label>
                    <input id="verify_password" type="password" name="verify_password" tabindex="3">
                    <label id="labelDescription" for="description">description</label>
                    <textarea id="description" name="description" placeholder="Enter description here...." tabindex="4"></textarea>
                    <input type="submit" name="submit" value="register" tabindex="5">
                </fieldset>
            </form>
        </div>
    </body>
</html>
You can fine more at my GitHub page -> https://github.com/Strider64/php_sandbox

Post Reply
  • Information
  • Who is online

    Users browsing this forum: Baidu [Spider] and 11 guests