I was following this tut. on how to install Apache, PHP, Mysql, and PHPmyadmin. I need a bit of help! I get a "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\mysql_test.php on line 15" everytime I test the mysql thing.

What ive done so far is :

[U]Configuring PHP to work with MySQL:[/U]
Now that both PHP and MySQL are installed, we have to configure them to work together.

   1. Open up your php.ini file (C:/WINDOWS/php.ini) and find the line:

      ;extension=php_mysql.dll

      To enable the MySQL extension, delete the semi-colon at the beginning of that line.

   2. Next we must add the PHP directory to the Windows PATH. To do this, click: Start > My Computer > Properties > Advanced > Environment Variables. Under the second list (System Variables), there will be a variable called "Path". Select it and click "Edit". Add ";C:\php" to the very end of the string and click "OK".

   3. Restart your computer for the changes to take effect.

   4. Create a new file in your "htdocs" directory called "mysql_test.php".

   5. Copy the following code into "mysql_test.php" and click save. (Make sure to replace the MYSQL_PASS constant with the MySQL Password you specified during the MySQL installation).

      <?php

      # Define MySQL Settings
      define("MYSQL_HOST", "localhost");
      define("MYSQL_USER", "root");
      define("MYSQL_PASS", "password");
      define("MYSQL_DB", "test");

      $conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());
      mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());

      $sql = "SELECT * FROM test";
      $res = mysql_query($sql);

      while ($field = mysql_fetch_array($res))
      {
      $id = $field['id'];
      $name = $field['name'];

      echo 'ID: ' . $field['id'] . '<br />';
      echo 'Name: ' . $field['name'] . '<br /><br />';
      }

      ?>
   6. Open up Internet Explorer and type in "http://localhost/mysql_test.php". If the "mysql_test.php" page returns something similiar to:

      ID: 1
      Name: John

The author of the tutorial told me to write and save this in the htdocs.

<?php

# Define MySQL Settings
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "root");
define("MYSQL_PASS", "rashad");
define("MYSQL_DB", "test");

$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());
mysql_select_db("".MYSQL_DB."",$conn) or die(mysql_error());

$sql = "SELECT * FROM test";
$res = mysql_query($sql);

while ($field = mysql_fetch_array($res))
{
$id = $field['id'];
$name = $field['name'];

echo 'ID: ' . $field['id'] . '<br />';
echo 'Name: ' . $field['name'] . '<br /><br />';
}

?>

replacing the " password " with my password?


The tutorial link is here : http://www.bicubica.com/apache-php-mysql/index.php

Do you have a table called 'test' within the database 'test'?

change $res = mysql_query($sql); to $res = mysql_query($sql) or die(mysql_error()); Does this return an error?

Yes. I have the test table. I did what the instructions said and did the use test; thing. Whenever I try the code you gave me, it says "Table 'test.test' doesn't exist"...

can you use this code and let us know what it returns:

<?php

   mysql_connect("localhost","root","password");

   $tables = mysql_list_tables("test");

   while (list($table) = mysql_fetch_row($tables)) {
      echo "$table <br />";
   }

?>

obviously change the password to your password.

Hi Guys...


This is problem..

$sql = "SELECT * FROM test";


change table name .. "name" (=

The problem is that your database is titled test, but the table inside that databse is named "name".

So when you use $sql = "SELECT * FROM test"; , it's trying to find the table "test" within the database that you connected to at the top of your file. (By the way, that's not a great way to connect to a database via PHP.

//define variables
$dbhost = "localhost";
$dbuser = "andrieux";
$dbpass = "pass";
$dbdatabase = "andrieux";

//connect
$db = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($db, $dbdatabase);

You don't need error fallbacks for something as simple as connecting to localhost, so you have alot of unneeded code.

Hi Guys...


This is problem..

$sql = "SELECT * FROM test";


change table name .. "name" (=

$sql = "SELECT * FROM name";

<?php

# Define MySQL Settings
define("MYSQL_HOST", "localhost");
define("MYSQL_USER", "root");
define("MYSQL_PASS", "password");
define("MYSQL_DB", "test");

$conn = mysql_connect("".MYSQL_HOST."", "".MYSQL_USER."", "".MYSQL_PASS."") or die(mysql_error());
mysql_select_db($db,"".MYSQL_DB."") or die(mysql_error());

$sql = "SELECT * FROM test";
$res = mysql_query($sql);

while ($field = mysql_fetch_array($res))
{
$id = $field;
$name = $field;

echo 'ID: ' . $field . '<br />';
echo 'Name: ' . $field . '<br /><br />';
}

?>


In the 9th line fatal error begins,
help me.. wat i want to do..

Replace your code in the beginning to this; in otherwords don't define - just declare them as variables. It's just simpler i think.

$dbhost = "your host";
	$dbuser = "your username";
	$dbpassword = "your password";
	$dbdatabase = "your database";

	$con = mysql_connect("$dbhost","$dbuser","$dbpassword");
	if (!$con)
  	{
  	die('Could not connect: ' . mysql_error());
  	}
This article has been dead for over six months. Start a new discussion instead.