Page 1 of 1

Problem setting up connection to MySQL

Posted: Mon Mar 17, 2014 3:41 am
by alexstarbuck
Hey guys and gals,

I am a noobie to programming and these boards so first greetings :) to everyone. I started learning html/css/php/mysql a couple of weeks ago and think I am progressing nicely but as I got into databases I started having some set up problems. A couple of stats:

Mac OSX Mountain Lion
XAMPP 1.8.3-3
PHP 5.5.9.

I am learning from TheNewBoston tutorials and all the code from their videos there worked fine - untill I tried connecting to a database with mysql_connect function. Sometimes it worked, sometimes it didnt but after a while I learned about the deprecated functions and the new mysqli class. This is what I am trying to do - I have a file called "connect.inc.php" where I have my connection stored (is this neccessary at all?) and another one called "databases.php" where I experiment with SQL querries. I created my test database with PHP MyAdmin.

Connect.inc.php

Code: Select all

<?php

$host = "localhost";
$user = "root";
$pass = "";
$db = "probna";

$con = new mysqli_connect($host, $user, $pass, $db);

// Check connection
if (mysqli_connect_errno($con)) {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
	echo "Connected to database ".$db.".";
}
?>
Databases.php

Code: Select all

<?php
include "connect.inc.php" or die();
?>
THE PROBLEM
First time I created and saved this, it worked (in Chrome); the message displayed was "Connected to database probna." Then I decided to play around with values stored in variables user, pass and host to see if I can produce some errors and if it's really connected to where I want it to connect. I changed the "root" to "skjdhsjdhf" and this is the message I got upon trying to load "connection.inc.php":

Fatal error: Class 'mysqli_connect' not found in /Applications/XAMPP/xamppfiles/htdocs/probniphp/connect.inc.php on line 9

When I try to load "databases.php" I get two messages:

Warning: include(1): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/probniphp/databases.php on line 21

Warning: include(): Failed opening '1' for inclusion (include_path='.:/Applications/XAMPP/xamppfiles/lib/php') in /Applications/XAMPP/xamppfiles/htdocs/probniphp/databases.php on line 21


I believe this is something trivial but I am just starting out so please dont be harsh on me :). I'd like to move on to more coding but IM stuck hee since yesterday
Cheers all!

Re: Problem setting up connection to MySQL

Posted: Mon Mar 17, 2014 6:01 am
by Celauran
mysqli_connect is a function, not a class, so you cannot instantiate it using new. Either use "new mysqli" or use mysqli_connect.

Code: Select all

$sql = new mysqli('localhost', 'user', 'password', 'dbname')
Will return a MySQLi object

Code: Select all

$sql = mysqli_connect('localhost', 'user', 'password', 'dbname');
is the procedural equivalent.

Re: Problem setting up connection to MySQL

Posted: Mon Mar 17, 2014 6:21 am
by alexstarbuck
THANKS MAN!!! You saved my life ;).

Re: Problem setting up connection to MySQL

Posted: Mon Mar 17, 2014 10:10 am
by alexstarbuck
Mmmm, having some other issues :(. I found more up to date tutorials by the same man who did the NewBoston ones (he is on Youtube under PHP Academy) and I am following to the T. But,

Code: Select all

<?php

error_reporting(E_ALL);
require 'includes/conn.php';

$result = $conn->query("SELECT * FROM users");
print_r('$result');

?>
Returns this:

Successfully connected to database.
$result


In the connection file I have instantiated new object $conn from the class mysqli. I have put this document in a subdolfer of my test site folder called "includes" and am requiring it with "require" function. THis part works, but when I want to print ->query property of the $conn object, I only get the message above. Pulling my hair a bit here :)

Re: Problem setting up connection to MySQL

Posted: Mon Mar 17, 2014 10:37 am
by Celauran
Lose the single quotes. The way you have the code now, you're asking it to print a string, which it is happily doing.

Re: Problem setting up connection to MySQL

Posted: Mon Mar 17, 2014 12:31 pm
by alexstarbuck
Could I have really been so stupid to overlook such a basic thing? I am embarrased now.

Thanks a million!
Cheers
Alex