PHP & mySQL fine on own but won't work together

t.jenner

Registered
I've been spending the last couple of weeks installing PHP and mySQL on my G4 with the intention of developing some database driven websites and maybe hosting them from my machine. I'm running Mac OS X Panther and I installed both from installer packages at entropy.ch and mysql.com respectively.

Both seem to work fine alone. The machine will parse straight PHP commands such as those that display date and time fine. I can also log onto mySQL from the Terminal application and play around with tables there.

The problem comes when I try and access a database from PHP. I can execute the following commands:

$sql="show status";
$result = mysql_query($sql);

along with a loop to output a table of '$result' to get a long list of variables. I can also induce an error message at this point

'Error 1045: Access denied for user: 'root@localhost' (Using password: NO)'

By giving the PHP file incorrect user name and password details. Strangely though whatever user name I use the error message always reads 'root@localhost'. This may or may not be part of the problem, either way I find it strange.

Moving on, as soon as I try and access databases via PHP with code such as:

mysql_select_db($database);
$query = stripSlashes($query) ;
$result = mysql_query($query);

I am just presented with a blank screen. I can't seem to induce any error messages perhaps implying that the database is not being connected too at all. The code I've shown here comes from sample files that are provided with the book I am learning PHP and mySQL from so the problem is very likely to be with my installation. All the same I have posted the source code at

http://www.mr-jenner.co.uk/php_problem/mysql_up_code.html (This works so far as presenting a table of variables.

http://www.mr-jenner.co.uk/php_problem/mysql_send_code.html (This will not return queries or errors. All it does is show you the blank form again. Other attempts at returning database queries yield a blank web page.)

I am hoping this is a simple problem like I haven't configured PHP to connect to mySQL properly but after trying for three days I am unable to overcome it.

Regards,

Tom
 
It is my understanding that you need to connect to the mysql server first. I am just starting to get into this stuff my self and this is one thing i noticed.

check this link out:

http://us4.php.net/mysql_connect

This would help... unless you took out the rest of the code where you connected, if so this post is useless.
 
You need to make a connection first.

<?php

$host = "<host>";
$username = "<username>";
$password = "<password>";
mysql_connect("$host", "$username", "$password");

?>

Next, you want to query the database by using this command:

<?php

$query = mysql_query(" SELECT * FROM <TABLE> ");

while ($rs = mysql_fetch_array($query)) {
print $rs['<fieldname>'] . "<br />";
}

?>

Hopes this help.
 
Just another example, from one of my pages:

<?php

$host = 'localhost';
$username = 'username';
$password = 'password';

$database = mysql_pconnect($host, $username, $password);

mysql_select_db('database_name');

$query = "select * from tablename";

$result = mysql_query($query);
$counter = mysql_numrows($result);

for ($c = 0; $c < $counter; $c++) {
$row = mysql_fetch_array($result);
echo($row['column_name']);
}


?>
 
If he wasn't connected to the server, he wouldn't be getting that error though...

I think maybe you don't have the user accounts set up correctly. Try installing phpMyAdmin (www.phpmyadmin.net) and going to the privileges area.
 
yeah... most probably, jenner need to setup the account first. the error shows that jenner didn't have privileges to access the mysql.
 
Thanks for all the great suggestions guys. Been very busy with other projects for the last couple of days but will post some feedback when I get a chance to try out some of the above.
 
Setup a user account (the basics):

in mysql (as root):

grant all on *.* to username identified by 'password'; (all should normally be replaced with specific privileges for a user.)

Your error says "using password NO", which is usually what you would get if you tried to login with mysql -u root instead of mysql -u root -p.

What mysql distribution did you use? The one from the mysql website or some 3rd party package?
 
dlloyd said:
If he wasn't connected to the server, he wouldn't be getting that error though...

That error confuses me greatly. Using the instructions on page 304 of the mysql manual I have created a user 'monty' with password 'some_pass' in mysql from the terminal.

mysql> GRANT ALL PRIVILEGES ON *.* TO ’monty’@’localhost’ -> IDENTIFIED BY ’some_pass’ WITH GRANT OPTION;
mysql> GRANT ALL PRIVILEGES ON *.* TO ’monty’@’%’ -> IDENTIFIED BY ’some_pass’ WITH GRANT OPTION;​

So using the following php script I can bring up a table of variables.


<?php
$host="localhost";
$user="monty";
$password="some_pass";

mysql_connect($host,$user,$password);
$sql="show status";
$result = mysql_query($sql);
if ($result == 0)
echo("<b>Error " . mysql_errno() . ": " . mysql_error() . "</b>");
elseif (mysql_num_rows($result) == 0)
echo("<b>Query executed successfully!</b>");
else
{
?>
<!-- Table that displays the results -->
<table border="1">
<tr><td><b>Variable_name</b></td><td><b>Value</b></td></tr>
<?php
for ($i = 0; $i < mysql_num_rows($result); $i++) {
echo("<TR>");
$row_array = mysql_fetch_row($result);
for ($j = 0; $j < mysql_num_fields($result); $j++) {
echo("<TD>" . $row_array[$j] . "</td>");
}
echo("</tr>");
}
?>

However when I change the '$password' variable to 'wrong_pass' I am presented with the error 'error 1045: Access denied for user: 'root@localhost' (Using password: NO)'

Surely I am connecting with a password though? and why is the user still 'root@localhost'?
 
Back
Top