Jump to content

how to connect php to mysql...


distracted

Recommended Posts

im just starting to try php and mysql...

 

i have the standard php, mysql, apache platform now...

i already configured my mysql according to some tutorials...

 

but when i try to connect php to my database an error always occur...

this message appear on the error...

 

Could not connect: Client does not support authentication protocol requested by server; consider upgrading MySQL client

 

i used this code to try to connect to my database...

 

<?php

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

if (!$link) {

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

}

echo 'Connected successfully';

 

mysql_close($link);

?>

 

i tried to upgrade mysql but same problem came out...

 

what should i do...

or is there sumthing im missin?

 

thnak you...

Link to comment
Share on other sites

Try this code

 

Note: the function take the sql statement and returns and indexed associative array, this way your get the data and close the db connection

and have a nice why of dealing the the data

 

so let say the customer table has ID, Name, Address and Phone fields

 

so in the example below  $queryResults[0]['Name'] would return the first record Name field value

 

<?

function selectSQL($theQuery) {
$openlink = mysql_connect($GLOBALS["dbhost"], $GLOBALS["dbuser"], $GLOBALS["dbpass"]) 	or die ("failed to connect to server: " . mysql_error());
mysql_select_db ($GLOBALS["db"]) or die ("failed to connect to database ($db): " . mysql_error());
$query = mysql_query($theQuery) or die ("Select query failed: " . mysql_error());

$array_counter = 0;
$return_array = array();
      while ($row = mysql_fetch_assoc ($query)) 
	{
	        foreach ($row as $key => $value) {
	            $return_array[$array_counter][$key] = $value;
	            }
	        $array_counter++;
	}
	mysql_free_result ($query);
      		return $return_array;

  mysql_close($openlink) or die ("couldn't clean up connection: " . mysql_error());
}

$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'password';
$db = 'database_name';


$queryResults = selectSQL("select * from customer");
print_r($queryResults);

?>

 

I find it so annoying to deal with intermingling database lookup and output logic. So that is why i included this big example

Link to comment
Share on other sites

Got this from MySQL forum, a simple workaround

MySQL 4.1 and up uses an authentication protocol based on a password hashing algorithm that is incompatible with that used by older clients. If you upgrade the server to 4.1, attempts to connect to it with an older client may fail with the following message:

 

shell> mysql

Client does not support authentication protocol requested by server; consider upgrading MySQL client

 

To solve this problem, you should use one of the following approaches:

 

* Upgrade all client programs to use a 4.1.1 or newer client library.

* When connecting to the server with a pre-4.1 client program, use an account that still has a pre-4.1-style password.

* Reset the password to pre-4.1 style for each user that needs to use a pre-4.1 client program. This can be done using the SET PASSWORD statement and the OLD_PASSWORD() function:

 

mysql> SET PASSWORD FOR some_user@some_host = OLD_PASSWORD('{your old password here}');

 

eg. SET PASSWORD FOR root@localhost = OLD_PASSWORD ('password'); worked perfectly

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.