954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Assitance On Php & Mysql test.

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 :

Configuring PHP to work with MySQL:
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'] . '';
      echo 'Name: ' . $field['name'] . '';
      }

      ?>
   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'] . '';
echo 'Name: ' . $field['name'] . '';
}

?>


replacing the " password " with my password?


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

Mr.Kazaam
Newbie Poster
3 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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?

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

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"...

Mr.Kazaam
Newbie Poster
3 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

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 ";
   }

?>

obviously change the password to your password.

Will Gresham
Master Poster
755 posts since May 2008
Reputation Points: 96
Solved Threads: 125
 

Okay I did as you said. In return I got " name ".

Mr.Kazaam
Newbie Poster
3 posts since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Hi Guys...


This is problem..

$sql = "SELECT * FROM test";


change table name .. "name" (=

Riukeen
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Andrieux
Junior Poster in Training
61 posts since Jan 2009
Reputation Points: 10
Solved Threads: 4
 

Hi Guys...

This is problem..

$sql = "SELECT * FROM test";

change table name .. "name" (=


$sql = "SELECT * FROMname";

Riukeen
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

<?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['id'];
$name = $field['name'];

echo 'ID: ' . $field['id'] . '
';
echo 'Name: ' . $field['name'] . '

';
}

?>


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

prabakar..
Newbie Poster
1 post since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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());
  	}
Hypalink
Newbie Poster
16 posts since Apr 2010
Reputation Points: 10
Solved Threads: 2
 

thanks guys. I have same problem. It ´s help me. :)

R1Programmer
Newbie Poster
5 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You