Jump to content


Photo

PHP/MySQL not working


  • Please log in to reply
5 replies to this topic

#1 released

released
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 03 June 2006 - 06:59 PM

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.

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 03 June 2006 - 07:05 PM

Instead of this:
$result= mysql_query($db, $query);

Do this:
$result= mysql_query($query,$db);

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.
Think you're smarty?

(Gone until 20 to November)

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 June 2006 - 07:13 PM

I see nothing wrong with your code.

But you might want to check through this list just incase for troubleshooting:
  • 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:
    $connect= mysql_connect("localhost", "root", ""); //I have been using the root user
    $db= mysql_select_db("myDatabase");
    to:
    $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());
  • Check that their is no errors with your query. SO change this:
    $query= "SELECT * FROM mp3";
    $result= mysql_query($db, $query);
    to:
    $query= "SELECT * FROM mp3";
    $result= mysql_query($db, $query) or die("Unable to perform query" . mysql_error());
  • If none of those returned anything then see if your $myrow variable is being populated. So change this:
    while($myrow= mysql_fetch_array($result)){
    echo $myrow[mp3Title];
    }
    with this:
    while($myrow= mysql_fetch_array($result)){
    echo "<pre>" . print_r($myrow, true) . "</pre><hr />";
    }
One of those steps above should bring up some form of error which might help you to solve the problem

#4 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 03 June 2006 - 07:26 PM

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)
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#5 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 03 June 2006 - 07:37 PM

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.
Think you're smarty?

(Gone until 20 to November)

#6 shortj75

shortj75
  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 03 June 2006 - 07:38 PM

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


$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());


or this


$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());


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 run

2.after run opens type in explorer after explorer opens type %UserProfile% in the
address bar and navagate to startmenu/programs/accessories

3.then create a new folder and name it database

4.goto Command Prompt in the accessories folder highlight it and hit ctrl c

5.now goto the database folder you just creater and open it once ther hit ctrl v
which will past a command prompt file there now rename it mysql prompt

6. 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 checked

8.next go to the layout tab go to the screen buffer size and chang the height to 2000
or higher then hit apply then ok

9.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 1

13.now type in flush privileges; then hit enter it should say rows affected 1

14.now type in exit; then hit enter and it should say bye

15.now your Mysql Username and password should be changed
[/quote]
The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users