released Posted June 3, 2006 Share Posted June 3, 2006 I have been trying for a while now to get a basic script to work between PHP and MySQL. I have read almost an entire 700 page book on the topic and countless online tutorials, understand the concepts but I am clearly doing something wrong. I am not sure if it is in the scripting or it is some other stupid problem. I recently installed FoxServ on my computer, which includes Apache, PHP and MySQL. The server works, and when I browse to localhost the default FoxServ page comes up. I drag the "connect.php" code to the "www" folder where the default apache site is. I then type [a href=\"http://localhost/connect.php\" target=\"_blank\"]http://localhost/connect.php[/a] to go to the page. It loads but I don't get any info from the MySQL database. The code is something like this:<?$connect= mysql_connect("localhost", "root", ""); //I have been using the root user$db= mysql_select_db("myDatabase");$query= "SELECT * FROM mp3";$result= mysql_query($db, $query);while($myrow= mysql_fetch_array($result)){ echo $myrow[mp3Title]; }?>It's very simple, and I've tried many variations, but I can't seem to get any info in or out of the database, and it's really holding me back. Help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/11105-phpmysql-not-working/ Share on other sites More sharing options...
Orio Posted June 3, 2006 Share Posted June 3, 2006 Instead of this:[code]$result= mysql_query($db, $query);[/code]Do this:[code]$result= mysql_query($query,$db);[/code]As said on: [a href=\"http://www.php.net/manual/en/function.mysql-query.php\" target=\"_blank\"]http://www.php.net/manual/en/function.mysql-query.php[/a]resource mysql_query ( string query [, resource link_identifier] )Orio. Quote Link to comment https://forums.phpfreaks.com/topic/11105-phpmysql-not-working/#findComment-41522 Share on other sites More sharing options...
wildteen88 Posted June 3, 2006 Share Posted June 3, 2006 I see nothing wrong with your code.But you might want to check through this list just incase for troubleshooting:[list][*]Does your setup PHP support short tags (<? ?>) try you script with normla php tags (<?php ?>)[*]Make sure PHP is making a succesful connection to your mysql database, so chnage this code:[code]$connect= mysql_connect("localhost", "root", ""); //I have been using the root user$db= mysql_select_db("myDatabase");[/code]to:[code]$connect= mysql_connect("localhost", "root", "") or die("Unable to connect to mysql: " . mysql_error());$db= mysql_select_db("myDatabase") or die("Unable to select database: " . mysql_error());[/code][*]Check that their is no errors with your query. SO change this:[code]$query= "SELECT * FROM mp3";$result= mysql_query($db, $query);[/code]to:[code]$query= "SELECT * FROM mp3";$result= mysql_query($db, $query) or die("Unable to perform query" . mysql_error());[/code][*]If none of those returned anything then see if your $myrow variable is being populated. So change this:[code]while($myrow= mysql_fetch_array($result)){echo $myrow[mp3Title];}[/code]with this:[code]while($myrow= mysql_fetch_array($result)){echo "<pre>" . print_r($myrow, true) . "</pre><hr />";}[/code][/list]One of those steps above should bring up some form of error which might help you to solve the problem Quote Link to comment https://forums.phpfreaks.com/topic/11105-phpmysql-not-working/#findComment-41525 Share on other sites More sharing options...
Buyocat Posted June 3, 2006 Share Posted June 3, 2006 Also you don't even need to specify which $db you want to use since you connected to only 1 before. You can just write $results = @mysql_query ($query_string); also if you want to get a better idea of what the error is with the MySQL remove the '@' and write:$result = mysql_query ($query_string) or die (mysql_error());I believe it can be written like that, if not then just throw the $result through an if clause like, if (!isset($result)). At any rate this will print on screen whatever mysql error was incurred. Remember to turn off this feature for a live site (by adding the @ back) Quote Link to comment https://forums.phpfreaks.com/topic/11105-phpmysql-not-working/#findComment-41533 Share on other sites More sharing options...
Orio Posted June 3, 2006 Share Posted June 3, 2006 But the way you are using it, the query is $db. Switch the places of $db and $query as I suggested in my first reply, and see if it works.Orio. Quote Link to comment https://forums.phpfreaks.com/topic/11105-phpmysql-not-working/#findComment-41541 Share on other sites More sharing options...
shortj75 Posted June 3, 2006 Share Posted June 3, 2006 the problem could also be that you need a password to connect to mysql and you are not useing one and the defualt password for the root user is usually 3306 or 3307 so try this and see if it helps [code] $connect= mysql_connect("localhost", "root", "3306") or die("Unable to connect to mysql: " . mysql_error());$db= mysql_select_db("myDatabase") or die("Unable to select database: " . mysql_error());[/code]or this[code]$connect= mysql_connect("localhost", "root", "3307") or die("Unable to connect to mysql: " . mysql_error());$db= mysql_select_db("myDatabase") or die("Unable to select database: " . mysql_error());[/code]or you could just set up your own user name and password by doing the following[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]Note: the following instruction are for windows operating system if you are using a different system the instructions will probably be different[!--colorc--][/span][!--/colorc--][!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]1. go to start menu then run2.after run opens type in explorer after explorer opens type %UserProfile% in the address bar and navagate to startmenu/programs/accessories3.then create a new folder and name it database 4.goto Command Prompt in the accessories folder highlight it and hit ctrl c5.now goto the database folder you just creater and open it once ther hit ctrl vwhich will past a command prompt file there now rename it mysql prompt6. now right click your new mysql prompt file and go to properties click on the shortcuts tab goto the start in box and type in "C:/Program Files/FoxServ/mysql/bin"7.next go to the options tab go to edit options and make sure quick edit mode and insert mode are checked8.next go to the layout tab go to the screen buffer size and chang the height to 2000 or higher then hit apply then ok9.now open the new mysql prompt file if you followed everthing correctly there should be a line like this C:/Program Files/FoxServ/mysql/bin>10. now type in net start mysql then hit enter and it should say services started 11.now type mysql -u root mysql then hit enter to start the mysql monitor now you should see a line that say mysql> 12.now to change the master user and password you type in update user set User='Newname' and Password=password('Newpass') where User='root';then hit enter it should say rows affected 113.now type in flush privileges; then hit enter it should say rows affected 114.now type in exit; then hit enter and it should say bye15.now your Mysql Username and password should be changed[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/11105-phpmysql-not-working/#findComment-41542 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.