Click here to Skip to main content
16,016,501 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

i downloaded the PHP 5 and APACHE 2. i was able to configure PHP5 and APACHE2 to work together and run .PHP files.

i just also installed MySQL 5.6 on my local machine, during installation it asked me what type of installation should i choose.

here are the option:
1. Development
2. Server
3. Dedicated

i selected server as i would like the machine to serve as a server on my local network. everything was going well, the installation came with MySql Workbench and i am able to connect to MySql DB with workbench. i created a database 'dbsample' for testing, added some tables and dummy data and everything works.

Now, my dilema is that i can't seem to connect PHP5 to MySql.
i created a sample .php file with code like this.

i have given 'user' all privilege to access the database.

PHP
<?php
error_reporting (E_ALL);
ini_set('display_errors', 1);

// mysqli
$host="localhost";
$port="3306";
$user="user";
$password="password";
$dbname="dbtimereport";

$mysqli = new mysqli_connect($host, $user, $password, $dbname, $port)
if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";

//Close Connection
$mysqli->close();

?>


i went to my PHP directoty and modified php.ini and enabled below lines to support mysql.
but still no Luck. i dont' know what or where could the error be coming from? as the page does not show any error message.

PHP
extension=php_mysql.dll
extension=php_mysqli.dll
extension=php_pdo_mysql.dll


below are some info from the MySql WorkBench

Name: Local MySql Instance
Host: localhost
Port: 3306
Server: MySQL Community Server (GPL)
Version: 5.6.19-log
Login User: user
Current User: user@localhost


Thanks in advance for all the help.
Posted

1 solution

I tried your problem and got it to work...

Here's some commands you can execute in an SQL file or at the MySQL command line or in the MySQL Workbench query builder to create the user:

SQL
CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL ON *.* TO 'user'@'localhost' IDENTIFIED BY 'password';


Here's some SQL code to create a 'persons' table in your database (make sure to backup your database first if you use it, it will be dropped first!...or you could always change the names):

SQL
SET NAMES utf8;
SET time_zone = '+00:00';
SET foreign_key_checks = 0;
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';

DROP DATABASE IF EXISTS `dbtimereport`;
CREATE DATABASE `dbtimereport` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `dbtimereport`;

DROP TABLE IF EXISTS `persons`;
CREATE TABLE `persons` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `FirstName` varchar(50) DEFAULT NULL,
  `LastName` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `persons` (`ID`, `FirstName`, `LastName`) VALUES
(1, 'Joe',  'Bloe'),
(2, 'Jane', 'Doe'),
(3, 'Larry',    'Smith');



Here's a PHP script to dump it:

PHP
<?php

$host="localhost";
$user="user";
$password="password";
$dbname="dbtimereport";

$con=mysqli_connect($host, $user, $password, $dbname);
// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM persons");

while($row = mysqli_fetch_array($result))
{
    echo $row['FirstName'] . " " . $row['LastName'];
    echo "<br>";
}

mysqli_close($con);

?>



...good luck! :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900