deekay Posted August 16, 2010 Report Share Posted August 16, 2010 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. Quote Link to comment Share on other sites More sharing options...
Wickham Posted August 16, 2010 Report Share Posted August 16, 2010 (edited) 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 August 16, 2010 by Wickham Quote Link to comment Share on other sites More sharing options...
deekay Posted August 16, 2010 Author Report Share Posted August 16, 2010 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? Quote Link to comment Share on other sites More sharing options...
newseed Posted August 16, 2010 Report Share Posted August 16, 2010 You need to double check your code. You have apostrophes where you should have quotes. Quote Link to comment Share on other sites More sharing options...
deekay Posted August 16, 2010 Author Report Share Posted August 16, 2010 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" Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 16, 2010 Report Share Posted August 16, 2010 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? Quote Link to comment Share on other sites More sharing options...
deekay Posted August 16, 2010 Author Report Share Posted August 16, 2010 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... Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 16, 2010 Report Share Posted August 16, 2010 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. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 16, 2010 Report Share Posted August 16, 2010 One other alternative, if you can't get this working, is to try MAMP instead (my local server package of choice for Mac based dev work). But if the issue is with your query somehow, changing that won't make a difference. Quote Link to comment Share on other sites More sharing options...
deekay Posted August 16, 2010 Author Report Share Posted August 16, 2010 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. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 16, 2010 Report Share Posted August 16, 2010 I think you need to show us the code that you are using to run a query. Just because you run a query doesn't mean anything will change (visually) necessarily, you still have to output the results to the page. Quote Link to comment Share on other sites More sharing options...
deekay Posted August 16, 2010 Author Report Share Posted August 16, 2010 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. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 16, 2010 Report Share Posted August 16, 2010 You have an unnecessary "$" in front of "mysql_query". You've probably been staring at the code too long and missed it. Quote Link to comment Share on other sites More sharing options...
deekay Posted August 16, 2010 Author Report Share Posted August 16, 2010 I just saw one difference: i put a semicolon after MYSQL_ASSOC)). But i removed the semicolon, and nothing changed. Quote Link to comment Share on other sites More sharing options...
deekay Posted August 16, 2010 Author Report Share Posted August 16, 2010 I removed the "$" and now I get this: Connected succesfully No query results whatsoever Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 16, 2010 Report Share Posted August 16, 2010 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. Quote Link to comment Share on other sites More sharing options...
deekay Posted August 16, 2010 Author Report Share Posted August 16, 2010 OMG!!! It finally works! So I added echo $my_rows; And I finally got output from the database!! Thank you sooo much, I was dealing with this for 2 days now I can finally move on! Thanks alot! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted August 16, 2010 Report Share Posted August 16, 2010 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. Quote Link to comment Share on other sites More sharing options...
deekay Posted August 16, 2010 Author Report Share Posted August 16, 2010 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 So I have to pay attention to the details Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.