/    Sign up×
Community /Pin to ProfileBookmark

Cant connect to database on localhost

I am trying to so a simple CRUD tutorial from this page.
[URL=”http://www.codeofaninja.com/2012/01/phpmysqli-connect-to-database.html”]http://www.codeofaninja.com/2012/01/phpmysqli-connect-to-database.html[/URL]
I created in PHPMyAdmin the database and table structure but i get a database not found error.

Since the default “localhost” parameter did not connect, I changed that parameter to “localhost:8080”. Now when I hit load the page just hangs and eventually times out. I am assuming that is the wrong $host?
My URL in Firefox for WAMP is [url]http://localhost:8080/NINJA/[/url]

[CODE]<?php
//set connection variables
$host = “localhost:8080”;
$username = “root”;
$password = “”;
$db_name = “codeofaninja”; //database name

//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
echo “Error: Could not connect to database.”;
exit;
}
?>[/CODE]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@DasherMar 28.2012 — Don't you need to use mysqli_connect($host, $username, $password, $db_name);
Copy linkTweet thisAlerts:
@Schmit43authorMar 28.2012 — I have never done this before. I am trying to explicitly follow that tutorial. Seems like I create the database and tables then it says I cannot connect to it.

How would you re-write my db_connect.php file?
Copy linkTweet thisAlerts:
@Schmit43authorMar 28.2012 — I altered this line:
[CODE]
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
[/CODE]

And changed it to:

//connect to mysql server

$mysqli = new mysqli_connect($host, $username, $password, $db_name);

I get the error:

Fatal error: Class 'mysqli_connect' not found in C:wampwwwNINJAdb_connect.php on line 9
Copy linkTweet thisAlerts:
@criterion9Mar 28.2012 — The OP is using the OOP version of mysqli rather than the antiquated mysql_* procedural functions.


$host = "localhost:8080";
[/quote]

Should probably be
[code=php]
$host = "127.0.0.1";
[/code]

or
[code=php]
$host = "localhost";
[/code]

The database server doesn't run on the same port as the web server. Also check to make sure you have started the database server.
Copy linkTweet thisAlerts:
@Schmit43authorMar 28.2012 — 1) I have properly added this database and table structure to PHPMyAdmin

2) I have restarted all WAMP services

Here is my DB_CONNECT CODE
[CODE]<?php
//set connection variables
$host = "localhost";
$username = "brian";
$password = "300game";
$db_name = "codeofaninja"; //database name

//connect to mysql server
$mysqli = new mysqli_connect($host, $username, $password, $db_name);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
echo "Error: Could not connect to database.";
exit;
}
?>[/CODE]


when I try to run it I get this error:

http://i1101.photobucket.com/albums/g427/Brian_Schmit/error-1.jpg
Copy linkTweet thisAlerts:
@criterion9Mar 28.2012 — I cannot visit that domain from work. Can you copy and paste the error message?
Copy linkTweet thisAlerts:
@Schmit43authorMar 28.2012 — ( ! ) Fatal error: Class 'mysqli_connect' not found in C:wampwwwNINJAdb_connect.php on line 9

Call Stack

# Time Memory Function Location


1 0.0011 374016 {main}( ) ..index.php:0

2 0.0019 377856 include( 'C:wampwwwNINJAdb_connect.php' ) ..index.php:10
Copy linkTweet thisAlerts:
@criterion9Mar 28.2012 — You need to change that line back to the way you had it before:

$mysqli = new mysqli($host, $username, $password, $db_name);
[/quote]
Copy linkTweet thisAlerts:
@Schmit43authorMar 28.2012 — Still get the error. Now my PHPMyAdmin is broken..SHOOT ME NOW!!
Copy linkTweet thisAlerts:
@DasherMar 29.2012 — I ran this on my WAMP system, and it connects fine;

[code=php]

//set connection variables
$host = "localhost";
$username = "root";
$password = "";
$db_name = "test"; //database name

echo "Hello<br>";

//connect to mysql server
$mysqli = mysqli_connect($host, $username, $password, $db_name);

//check if any connection error was encountered
if(mysqli_connect_errno()) {
echo "Error: Could not connect to database.";

}else {echo "Connected to DB";}

[/code]


If I add "new" in front of mysqli_connect I get this error;

Fatal error: Class 'mysqli_connect' not found in C:wampwwwmysqli.php on line 19
Copy linkTweet thisAlerts:
@criterion9Mar 29.2012 — Still get the error. Now my PHPMyAdmin is broken..SHOOT ME NOW!![/QUOTE]

What do you mean it is broken? Do you get an error message?
×

Success!

Help @Schmit43 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...