test connectivity to mysql

Hi
am new to PHP so please excuse easy question!

On my web server i have created new mysql db called xxxx_zzzz
password is Qwerty
username is xxxx_zzzz

in public_html dir i created dir called testphp and uploaded 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 created another file in the same dir called phpdbtestfile2 per below

<?php include_once “connect_to_phpdbtest.php”; echo “You are connected to your database”; ?>

then i goto google chrome and access website www.xxxx.com.au/testphp/phpdbtestfile2.php
and i get this error on the screen

the web page isnt working
and is currently unable to handle this request.
HTTP ERROR 500

and in the error log i see

[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

Throw the script out (What you’re using is outdated) and start new using mysqli or PDO (My Recommendation) on how to write to a MySQL database table. There’s a nice PDO tutorial here in the tutorial section or do a internet search (Google most likely).

Another thing even if it is just a test script never show the password, for that will lead into a bad programming practice.

Here’s a utilities.inc.php (configuration file that I use) :

[php]<?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);[/php]

I use constants in another file like so:

[php] define(‘DATABASE_NAME’, ‘Your Database Name Goes HERE!’);[/php]
HTH John

thanks Strider64 - I was using this method as i found a great guide on how to build a simple web based membership system…but i can’t seem to find anything like this using PDO??
Sue

Sponsor our Newsletter | Privacy Policy | Terms of Service