-2

Everything worked fine when using a localhost (data insertion and data retrieval worked well) I hosted our website today to develop coding for email activation and so on.

This site is for a computer store, there's a price list of several components categorized neatly. After the site was hosted I get the error:

"Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/a3270569/public_html/products.php on line 92"

I'm not sure why this happens.

Here's my code to retrieve data from one table:

<?php
$con = mysql_connect("hostname","username","pass"); //i changed these ;)
if (!$con){
die("Can not connect: ".mysql_error());
}
mysql_select_db("users",$con);
$sql = "SELECT * FROM intel";
$myData = mysql_query($sql,$con);

echo "<table id=test1 border=1 bgcolor=white>
<tr bgcolor=green>
<th>Processors</th> 
<th>Price</th> 
<th>Warranty</th> 
</tr>";
while ($record = mysql_fetch_array($myData)){       //this is line 92 in my code
echo "<tr>";
echo "<td>" . $record['name'] . "</td>";
echo "<td>" . $record['price'] . "</td>";
echo "<td>" . $record['war'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);

?>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Nytzruze
  • 7
  • 2
  • You must pass your `$conn` variable to the fetch function – Toumash Dec 06 '14 at 11:18
  • 4
    They're teaching you `mysql_` and not `mysqli_` (or PDO)? Ask your tutor why they're teaching an out of date database extension! – worldofjr Dec 06 '14 at 11:21
  • This is [a very popular question](https://stackoverflow.com/search?q=supplied+argument+is+not+a+valid+MySQL+result+resource) - please don't forget to search for error messages (here or in a search engine) prior to asking `:)`. – halfer Dec 06 '14 at 11:24
  • 1
    Is your database really named `users` but your table named `intel`? – h2ooooooo Dec 06 '14 at 11:26

1 Answers1

0

The query probably returns an error.

Add this below $myData = mysql_query($sql,$con);:

if (!$myData){
    die(mysql_error());
}

This will display the error if the query is invalid.

Edit:

In order to also display errors from mysql_select_db(), replace mysql_select_db("users",$con) by this:

if (!mysql_select_db("users",$con)) {
    die(mysql_error());
}
Jeroen Noten
  • 3,574
  • 1
  • 17
  • 25