MySQL Forums
Forum List  »  PHP

Re: Cannot connect to Database
Posted by: Patrick Rooney
Date: December 30, 2014 04:16PM

Hi, I'm running:
Windows 7
Binami WAMP Stack 5.4.35-0
MySQL database and Apache Web Server running
PHP MyAdmin to issue MySQL queries
Chrome Browser

I can display the three rows in my table using PHP MyAdmin, but my PHP script fails to connect to the database.

Results of the SHOW CREATE TABLE using PHP MyAdmin:

SQL result

Host: localhost3306
Database: csv_db
Generation Time: Dec 30, 2014 at 02:05 PM
Generated by: phpMyAdmin 4.2.12 / MySQL 5.5.40
SQL query: show create table test_cntl_match;
Rows: 1

Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available.
Table Create Table
test_cntl_match CREATE TABLE `test_cntl_match` (
`TEST_StoreNumber` int(3) NOT NULL DEFAULT '0',
`CNTL_StoreNumber` int(3) NOT NULL DEFAULT '0',
`TEST_LQS` int(2) DEFAULT NULL,
`CNTL_LQS` int(2) DEFAULT NULL,
`TEST_SalesFY13` decimal(4,1) DEFAULT NULL,
`CNTL_SalesFY13` decimal(4,1) DEFAULT NULL,
`TEST_CompRate` decimal(5,3) DEFAULT NULL,
`CNTL_CompRate` decimal(5,3) DEFAULT NULL,
`MatchDistance` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

The table itself, exported using PHP MYAdmin:

TEST_StoreNumber;CNTL_StoreNumber;TEST_LQS;CNTL_LQS;TEST_SalesFY13;CNTL_SalesFY13;TEST_CompRate;CNTL_CompRate;MatchDistance
1;220;10;10;189.0;188.2;0.017;-0.030;0.6422090000000001
2;426;6;5;38.5;35.7;-0.073;-0.081;8.840064
4;220;9;10;180.3;188.2;0.000;-0.030;63.410900000000005

The two PHP scripts that I used to display this table:

<?php // login.php
$db_hostname = 'localhost';
$db_database = 'csv_db';
$db_username = 'root';
$db_password = 'xxxxxxxxx';
//echo 'database: ' . $db_database; results in database: csv_db
?>

<?php //TestMySql.php
require_once 'login.php';
echo 'database host name: ' . $db_hostname . '<br>';
echo 'database: ' . $db_database . '<br>';
echo 'User: ' . $db_username . '<br>';
//echo 'pswrd: ' . $db_password . '<br>';

$db_server = mysql_connect($db_hostname, $db_username, $db_password);

// echo 'db server: ' . $db_server . '<br>' results in db server: Resource id #4;

// if ($db_server) echo 'Connected successfully' . '<br>' results in Connected successfully ;

if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());

mysql_select_db($db_database) or die("Unable to select database: " . mysql_error());
//displays
//result:
//Database access failed:

$query = "SELECT * FROM test_cntl_match";
$result = mysqli_query($query);
echo 'result: ' . $result . '<br>'; //result: Resource id #5
if (!$result) die ("Database access failed: " . mysql_error());

for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);

echo 'TestStoreNumber: ' . $row[0] . '<br>';
echo 'CntlStoreNumber: ' . $row[1] . '<br>';
echo 'Test LQS: ' . $row[2] . '<br>';
echo 'Cntl LQS: ' . $row[3] . '<br>';
echo 'TEST_SalesFY13: ' . $row[4] . '<br>';
echo 'Cntl_SalesFY13: ' . $row[5] . '<br>';
echo 'TEST_CompRate: ' . $row[6] . '<br>';
echo 'Cntl_CompRate: ' . $row[7] . '<br><br>';
}
mysql_close($db_server);
?>

When I point my browser to

localhost/TestMysql.php

it displays

database host name: localhost
database: csv_db
User: root
result:
Database access failed:

Options: ReplyQuote


Subject
Written By
Posted
December 10, 2014 06:24PM
December 10, 2014 09:39PM
January 06, 2015 02:08AM
December 20, 2014 02:28AM
December 20, 2014 10:04AM
Re: Cannot connect to Database
December 30, 2014 04:16PM
December 30, 2014 10:18PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.