Jump to content
Killersites Forums

mysql_connect not working


Recommended Posts

Hi,

 

Im a PHP noob. Have been following the tutorials and everything went great up to this point:

 

Right now i want to connect my localhost MySQL database with PHP with the following:

 

<?php

 

$my_connection = mysql_connect('localhost','root','');

if (!$my_connection) {

die('Could not connect: '. mysql_error());

 

 

}

echo 'Connected succesfully' . '<br><br>';

 

mysql_close($my_connection);

 

 

?>

 

However if i change the user into 'roaot', there is no error message. When I change the localhost or put a password I do get errormessages, but not when I change the user. But it's definitely not connected to the database, because I my queries don't work.

 

I've been searching in many forums but I can't find anything. The only thing I could find is that the php_mysql.dll was not loaded in the php.ini, but that is Windows related, I'm working on a Mac with Xampp.

 

Hope you guys can help me out.

Link to post
Share on other sites

I don't think you have selected a database.

 

When working locally with XAMPP (or WinAmp Server in Windows), first you have to connect to the database and then select a database. I have this for one of my databases;-

<?php
mysql_connect("localhost","root","") or die(mysql_error());

mysql_select_db("db-name") or die(mysql_error());

It uses " instead of ' but I don't think that's important.

 

When you upload to your host server you have to edit to

mysql_connect("localhost","your-username","your-password");
mysql_select_db("db-name") or die(mysql_error());

 

Have you actually set up a database and a table inside the XAMPP folder?

Edited by Wickham
Link to post
Share on other sites

Hi thanks for your reply!

Actually I did: I created a database in PHPmyadmin in Xampp called 'killer' with the table 'people' with the fields 'name' and 'id'.

 

so I added this line below the previous code:

 

mysql_select_db('killer') or die(mysql_error());;

 

But it is the exact same problem. If i put 'filler' instead of 'killer' I get no error message.

 

I've done some more research and it could be that the PHP MySQL extension is not installed/enabled. But how do I install that extension and where?

Link to post
Share on other sites

You need to double check your code. You have apostrophes where you should have quotes.

 

I have replaced all the single quotes with double quotes and it's the same thing. By the way, I did all the code exactly the same as the video tutorial I was following and that one was using single quotes too. But on that video the connection was also working.

 

The video tutorials is of this site: "PHP and MySQL: Connecting to MySQL with PHP: Part 3"

Link to post
Share on other sites

So this is the code you are working with at the moment?

<?php

$my_connection = mysql_connect('localhost','root','');
mysql_select_db('killer') or die(mysql_error());

if (!$my_connection) {
die('Could not connect: '. mysql_error());


}
echo 'Connected succesfully' . '<br><br>';

mysql_close($my_connection);


?>

 

And I would assume that you have placed the PHP file in the correct location on your computer so XAMMP can access the file, and XAMMP is currently running?

 

And, once XAMMP is running, you've pressed the buttons in the XAMMP control app that start Apache and MySQL?

Link to post
Share on other sites

Yep that's the code! To double check I attached a screenshot from my code on Dreamweaver and one from the vid and there is no difference!

 

Xampp is running (Apache, MySQL, FTP) and the php.ini file is located where it should be ../Aplications/XAMPP/Xamppfiles/etc/php.ini

 

I found this on another forum where a person had a similar problem:

 

"Yes, it means that the PHP MySQL extension is not installed/enabled. Note that as of PHP5.0 the MySQL extension is not installed by default (being replaced by the MySQLi extension). If you want/need to get the original extension to work, check out the info at:

 

Getting PHP5 to work with MySQL"

 

The last line was a link, but it doesnt work :(

 

So now I'm trying to find out how to install the PHP MySQL extension. But so far I can't find anything...

post-29443-027698700 1281988802_thumb.png

post-29443-001549600 1281988815_thumb.png

Link to post
Share on other sites

So, when you run the above code, what happens? You get a connected successfully message? But if you try to run a query, what happens? (and perhaps you could post a query that you are using? Perhaps the issue is not with XAMMP, but with an error in your query code?

 

I just downloaded XAMPP (brand new from the website) and tried out your code, and it worked fine for me. I set up a test database with some test data, and I could use a query to get the info from the db.

Link to post
Share on other sites

Wow, that's really strange.

 

Ok, so this is what's happening:

 

With the code I showed you guys, I got this from both Firefox, Safari and Chrome:

 

Connected succesfully

 

When i do a typo with 'localhost' like 'localhaost' I get this:

 

Warning: mysql_connect() [function.mysql-connect]: Unknown MySQL server host 'localhaost' (1) in /Applications/XAMPP/xamppfiles/htdocs/xampkiller/database.php on line 9

Could not connect: Unknown MySQL server host 'localhaost' (1)

 

BUT when I do a typo with the user 'root', like 'raoot' I get this:

 

Connected succesfully

 

And that's wrong, because there should be an error message with this.

 

And last, when I put a password I get the following:

 

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in /Applications/XAMPP/xamppfiles/htdocs/xampkiller/database.php on line 9

Could not connect: Access denied for user 'root'@'localhost' (using password: YES)

 

So it's responding well when the localhost or the password is typed wrong, but not with the user.

 

And when I a run a query I get this again:

 

b]Connected succesfully

[/b]

 

And only that. So that's really strange. So PHP and MySQL are definitely not working well with each other.

 

I downloaded the Xampp a couple of days ago and followed all the instructions. With the other PHP tutorials where I only worked with PHP it worked just fine. Working with MySQL on PHPmyAdmin works fine as well. The database is created with the table and the fields are there. I did everything as instructed, but for some reason this just doesn't work.

Link to post
Share on other sites

Ok. So this is the query I ran. I dunno what happened, but besides the echo statement I get this:

 

Connected succesfully

 

 

Fatal error: Function name must be a string in /Applications/XAMPP/xamppfiles/htdocs/xampkiller/database.php on line 20

 

But I attached a screenshot where you see my code on the left on Dreamweaver and on the right a screenshot from the vid. But call me crazy, but I really don't see a difference. But in the vid the code works. And if you look at line 20 it's the same as on the vid, so I don't understand.

post-29443-009314600 1281992103_thumb.png

Link to post
Share on other sites

Correct. As far as I can tell, you aren't displaying this code anywhere. The while loop just pulls the data from the database and adds it to the $myrows variable -- it doesn't do anything else with it. You'd need to echo out the $my_rows variable someplace in the page in order to get it to display.

 

And yes, the extra ";" was unnecessary and would have caused the loop to not work.

Link to post
Share on other sites

Glad to help. :) I'm not sure what video you were following along with, but that too should have done something with the $my_rows variable in order to get it to display. Just using the code that was in the screenshot you posted wouldn't have done anything by itself -- it's just storing the info from the database in a variable.

Link to post
Share on other sites

The code is on this vid: "PHP and MySQL: SQL Select Statements with PHP" at the PHP tutorial vids. And yes I overlooked the echo statement, cuz later on he does add it in a separate php block. Well, like I said, I'm a PHP noob :P So I have to pay attention to the details ;)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...