Jump to content

Stumped while trying to read MySql database.


Dark57

Recommended Posts

Ok, just so you all know I'm very new to this whole PHP thing and apologize for my sloppy code and ignorance.  My problem is I'm trying to read from a database and put it into a html table. Its a fairly simple concept and I'm just trying to get it to work for learning purposes.

 

What the whole ordeal is supposed to do is when you click the submit button below the stat it will go to train.php and conduct a math formula then edit the stat in the train_db database.  Of course, if I can't even get it to read the stats from a database before I start then there's no point in me trying to write the train.php file yet.

 

This is script that is showing me an error message:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\DataBase Project\read.php on line 22

<html>
<body>

<?php
$con = mysql_connect("127.0.0.1","xxxxx","xxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("train_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'>
<tr>
<th>Strength</th>
<th>Defense</th>
<th>Speed</th>
</tr>";

$row = mysql_fetch_array($result);
echo "<tr>";
echo "<td>" . $row['str'] . "</td>";
echo "<td>" . $row['def'] . "</td>";
echo "<td>" . $row['spd'] . "</td>";
echo "</tr>";


echo "<tr>
<td><input type='submit' name='full_strength' value='All Strength'></td>
<td><input type='submit' name='full_defense' value='All Defense'></td>
<td><input type='submit' name='full_speed' value='All Speed'></td></tr>";

echo "</table>";

?>

</body>
</html>

 

I created the table using this:

<html>
<body>

<?php
$con = mysql_connect("127.0.0.1","xxxx","xxxxxxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Create database
if (mysql_query("CREATE DATABASE train_db",$con))
  {
  echo "Database created";
  }
else
  {
  echo "Error creating database: " . mysql_error();
  }

// Create table
mysql_select_db("train_db", $con);
$sql = "CREATE TABLE Persons
(
id int,
str int,
def int,
spd int,
)";

// Execute query
mysql_query($sql,$con);
echo "<br/>Table Created.";

$sql="INSERT INTO Persons (id, str, def, spd)
VALUES
('1','10','10','10')";


mysql_close($con);

?>

</body>
</html>

 

And that's all I've got so far, I don't know why I have this error and from what I've been trying to look up online doesn't seem to explain the error to me. Maybe its just because I've been starring at my own code for so long I am unable to see my errors.

 

If I'm doing something wrong please point it out and and tips would be appreciated.

The query is failing and returning a boolean FALSE. This usually indicates a syntax error in the query, if the connection is known to be good. If you have phpMyAdmin, paste the query into the SQL tab and see what the result is, or try it from a mysql> command line.

Recently I changed the password to my MySQL, so when I try to use phpMyAdmin through WAMPserver it gives me #1045 - Access denied for user 'root'@'localhost' (using password: NO).

 

I'm not sure how I'm supposed to set it to use a password, I'm pretty new to all of this so I went through php.ini and tried a few things but that didn't work. How do I set the password for phpMyAdmin?

You should be prompted for username/password when you access phpMyAdmin via the URL. I don't know what path it is installed to under WinD'ohs, but if you go to http://localhost do you get the WAMP information page and a link (somewhere on the page) to phpMyAdmin? What happens if you use that link?

Actually like 6 beers later I discovered my problem.

 

// Create table

mysql_select_db("train_db", $con);

$sql = "CREATE TABLE Persons

(

id int,

str int,

def int,

spd int,

)";

 

That comma wasn't supposed to be there so it was causing a syntax error while trying to write to the database.

In order to be able to use phpMyAdmin, (and it is a handy thing to be able to use) it looks like you'll have to edit the phpmyadmin config file to reflect the fact that you changed the password. The closest thing I can find to instructions is here:

http://rudyegenias.wordpress.com/2006/09/15/enabling-phpmyadmin-once-you-change-permission-or-privileges-on-mysql-database/

This seems to match the problem you're having . . .

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.