MDanz Posted September 1, 2009 Share Posted September 1, 2009 i get this error Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/ustackc1/public_html/example.php on line 5 <?php mysql_connect("localhost", "Master", pword"); mysql_select_db("db"); $time = mysql_fetch_assoc(mysql_query("SELECT * FROM Stacks ORDER BY id DESC Limit 1 WHERE keywords = 'nba'")); $posted = $time['posted']; echo $posted; ?> what i do wrong? Link to comment https://forums.phpfreaks.com/topic/172654-help-on-mysql_fetch_assoc/ Share on other sites More sharing options...
Garethp Posted September 1, 2009 Share Posted September 1, 2009 $time = "SELECT * FROM Stacks ORDER BY id DESC Limit 1 WHERE keywords = 'nba'"; if(!mysql_query($time)) { echo mysql_error(); } else { $time = mysql_fetch_assoc($time); } Link to comment https://forums.phpfreaks.com/topic/172654-help-on-mysql_fetch_assoc/#findComment-910075 Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2009 Share Posted September 1, 2009 To start with, the syntax in your query is not in the correct order. The following is the SELECT syntax prototype. The elements, when used, must be in the order shown (I highlighted the parts you are using) - SELECT [ALL | DISTINCT | DISTINCTROW ] [HIGH_PRIORITY] [sTRAIGHT_JOIN] [sql_SMALL_RESULT] [sql_BIG_RESULT] [sql_BUFFER_RESULT] [sql_CACHE | SQL_NO_CACHE] [sql_CALC_FOUND_ROWS] select_expr [, select_expr ...] [FROM table_references [WHERE where_condition] [GROUP BY {col_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]] [HAVING where_condition] [ORDER BY {col_name | expr | position} [ASC | DESC], ...] [LIMIT {[offset,] row_count | row_count OFFSET offset}] [PROCEDURE procedure_name(argument_list)] [iNTO OUTFILE 'file_name' export_options | INTO DUMPFILE 'file_name' | INTO var_name [, var_name]] [FOR UPDATE | LOCK IN SHARE MODE]] Link to comment https://forums.phpfreaks.com/topic/172654-help-on-mysql_fetch_assoc/#findComment-910078 Share on other sites More sharing options...
MDanz Posted September 1, 2009 Author Share Posted September 1, 2009 ok this is my revised code.. i'm getting the error now on line 13/? Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/ustackc1/public_html/example.php on line 13 <?php mysql_connect("localhost", "Master", "pword"); mysql_select_db("db"); $time = "SELECT * FROM Stacks WHERE keywords = 'nba' ORDER BY id DESC Limit 1"; if(!mysql_query($time)) { echo mysql_error(); } else { $time = mysql_fetch_assoc($time); $stackname = $time['posted']; echo $stackname; } ?> Link to comment https://forums.phpfreaks.com/topic/172654-help-on-mysql_fetch_assoc/#findComment-910093 Share on other sites More sharing options...
jamesxg1 Posted September 1, 2009 Share Posted September 1, 2009 Ok try this, mysql_connect("localhost", "Master", "pword"); mysql_select_db("db"); $sql = mysql_query("SELECT * FROM `Stacks` WHERE keywords = 'nba' ORDER BY `id` DESC Limit 1") or trigger_error('Query failed: ' . mysql_error(), E_USER_ERROR); while($time = mysql_fetch_array($sql)) { $stackname = $time['posted']; echo $stackname; } James. Link to comment https://forums.phpfreaks.com/topic/172654-help-on-mysql_fetch_assoc/#findComment-910207 Share on other sites More sharing options...
jamesxg1 Posted September 1, 2009 Share Posted September 1, 2009 what i do wrong? I am not the best at coding so if im wrong on this someone please correct me, But you cant have mysql_fetch_assoc before you have put the mysql_query because the mysql_fetch_assoc will be trying to get information from an un-executed query. James. Link to comment https://forums.phpfreaks.com/topic/172654-help-on-mysql_fetch_assoc/#findComment-910211 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.