Jump to content

Help on mysql_fetch_assoc


MDanz

Recommended Posts

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

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]]

 

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;
}


?>

 

:wtf:

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.

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.

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.