matthew9090 Posted May 26, 2010 Share Posted May 26, 2010 Help i don't under stand why this doen't work it says: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in (file location) on line 14 This is the script: <?php $con = mysql_connect("localhost","usr","Pass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("db_name", $con); $result = mysql_query("SELECT * FROM tablename"); while($row = mysql_fetch_array($result)) { echo $row['row'] . " " . $row['email']; echo "<br />"; } mysql_close($con); ?> while($row = mysql_fetch_array($result)) is line 14 Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2010 Share Posted May 26, 2010 Your query failed due to some error and returned a FALSE value. If you echo mysql_error() on the next line after the mysql_query() statement, it will tell you why it failed. Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063710 Share on other sites More sharing options...
matthew9090 Posted May 26, 2010 Author Share Posted May 26, 2010 i have added the line now it says: Parse error: syntax error, unexpected T_WHILE, expecting ',' or ';' in (location) on line 15 Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063711 Share on other sites More sharing options...
kenrbnsn Posted May 26, 2010 Share Posted May 26, 2010 Sounds like you didn't terminate the line you put in with a semi-colon. Ken Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063713 Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2010 Share Posted May 26, 2010 That would indicate that you introduced a php syntax error into the code. Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063714 Share on other sites More sharing options...
matthew9090 Posted May 26, 2010 Author Share Posted May 26, 2010 i got this code from w3schools.com and just changed the values. can someone please show me the code with the correct syntax. Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063717 Share on other sites More sharing options...
matthew9090 Posted May 26, 2010 Author Share Posted May 26, 2010 i added the ; at the end of the line but it still says the same thing Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063719 Share on other sites More sharing options...
jordz Posted May 26, 2010 Share Posted May 26, 2010 Take out the curly braces. See what happens? Jordan Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063720 Share on other sites More sharing options...
matthew9090 Posted May 26, 2010 Author Share Posted May 26, 2010 it still says the same thing Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063722 Share on other sites More sharing options...
PFMaBiSmAd Posted May 26, 2010 Share Posted May 26, 2010 The only way someone can tell you what is wrong with your current code is if you post it. Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063726 Share on other sites More sharing options...
matthew9090 Posted May 26, 2010 Author Share Posted May 26, 2010 problem solved!!!! i used a different script of the internet Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063727 Share on other sites More sharing options...
jordz Posted May 26, 2010 Share Posted May 26, 2010 Okay starting from scratch this is how I would do it of the top of my head: $con = mysql_connect('localhost','user','pass,') or die (mysql_error)); mysql_select_db('db_name'); $query = "SELECT * FROM tablename"; $mysql = mysql_query($query); while($row = mysql_fetch_array($mysql, MYSQL_ASSOC)) echo $row['field']; endwhile; I don't usually close the connection, no point really. Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063728 Share on other sites More sharing options...
matthew9090 Posted May 26, 2010 Author Share Posted May 26, 2010 i used this example of the internet first: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> Then i used this one: <?php // Make a MySQL Connection mysql_connect("localhost", "admin", "1admin") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); // Get all the data from the "example" table $result = mysql_query("SELECT * FROM example") or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Name</th> <th>Age</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['name']; echo "</td><td>"; echo $row['age']; echo "</td></tr>"; } echo "</table>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/202992-mysql_fetch_array-error/#findComment-1063731 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.