...

View Full Version : Error while trying code from w3schools.com



futurejp
05-23-2008, 02:26 AM
Hi,

I'm trying out some php/mysql code for the first time, and I'm getting errors while trying some example code from www.w3schools.com.

Here is the page I'm looking at:

http://www.w3schools.com/php/php_mysql_select.asp

and here is the code I'm trying:


<?php
$con = mysql_connect("mysql1.***************","a2080062_mpr","test!!");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("stest", $con);

$result = mysql_query("SELECT * FROM stest2 WHERE Song='Walk This Way'");

echo "<table border='1'>
<tr>
<th>Date</th>
<th>Song</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Song'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>



The error I get on my site says it's with the
while($row = mysql_fetch_array($result))
line, and it says:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a2080062/public_html/test.html on line 21



The table in the database I'm accessing has four columns: "Date," "Venue," "Location," "Song." So a row in this table might look like:

1975-04-19 Boston Garden Boston MA Walk This Way

and I'm trying to print out a table listing all dates that have a 'Walk This Way' in the Song column, with the actual string 'Walk This Way.'

Any ideas why this isn't working?
Thanks a lot.

Iszak
05-23-2008, 02:43 AM
It usually means that the query that you did was incorrect so the SQL being "SELECT * FROM stest2 WHERE Song='Walk This Way'" it could be than there is no table called that or no column called song.

_Aerospace_Eng_
05-23-2008, 02:57 AM
You need to do some error checking. You don't even know if your query is failing or not.

<?php
$con = mysql_connect("mysql1.***************","a2080062_mpr","test!!");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("stest", $con);

$result = mysql_query("SELECT * FROM stest2 WHERE Song='Walk This Way'") or die(mysql_error());

echo "<table border='1'>
<tr>
<th>Date</th>
<th>Song</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Song'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
What do you get when you run that?

futurejp
05-23-2008, 03:01 AM
Thanks. I checked those things, but I think they're right. Take a look at this screenshot of my work area:

http://people.brandeis.edu/~nono/stuff/problem.jpg

You can see at the top and the left that my database's name is 'stest', like in my code (I also tried switching it to a2080062_stest, it didn't make a difference) and that I have two tables, 'songs' and 'stest2.' Also, you can see my four column names, and that the last one is 'Song.' Any more ideas?

futurejp
05-23-2008, 03:07 AM
You need to do some error checking. You don't even know if your query is failing or not.

What do you get when you run that?


This solved it for me - thanks! I ran your code and got "no database selected" so I changed the database to a2080062_stest, which I previously said didn't make a difference. Thanks again :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum