views:

337

answers:

4

This is my php code (I already have a connection to the db):

$result = mysql_query("SHOW TABLE STATUS FROM mydb LIKE 'mytable';");
     while ($array = mysql_fetch_array($result)) {
          $updatetime = $array['Update_time'];
     }

echo $updatetime;

I get:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

I am running MySQL 5.0.89 and PHP5.

I do not want to add a new field to the table... I want to use the table status...

Any help?

Thanks!

+2  A: 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

You did not supply the right resource to mysql_fetch_array. Also, I believe you have your $array and $result variables mixed up, and note the my_database reference which I'll explain below.

$array = mysql_query("SHOW TABLE STATUS FROM my_database;"); <--here
     while ($array = mysql_fetch_array($result)) { <--$result is undefined.

Should be instead

$result = mysql_query("SHOW TABLE STATUS FROM my_database;");
     while ($array = mysql_fetch_array($result)) {
          $updatetime = $array['Update_time'];
     }

echo $updatetime;

As of now, your MySQL syntax is not correct in regards to SHOW TABLE STATUS you need to reference your database after your FROM clause.

Anthony Forloney
I was retyping it instead of copy/paste, but I do have it the way you pointed it out here. Same result.
Tribalcomm
Could you edit your question and post the code you are using?
Anthony Forloney
It is updated to reflect your advice and correct coding
Tribalcomm
A: 

The show table status from should be followed by database name not table name:

SHOW TABLE STATUS FROM <DATABASE NAME>

Also every time you execute a query, you should always check its return value before you go ahead and start using the result object.

$res= mysql_query("SHOW TABLE STATUS FROM DB;");
if(! $res) {
 die('Query failed'.mysql_error());
}
codaddict
He is using database name.
Jackson Miller
A: 

Table update time must be taken from table field only.

Col. Shrapnel
A: 

Figured it out. Not sure if this is the perfect way, but it works...

$result = mysql_query("SHOW TABLE STATUS FROM mydb LIKE 'mytable';");
foreach ($result as $data) {
    $updatetime = $data->Update_time;
}

echo $updatetime;

You can then parse the raw timestamp with substr.

P.S. As I was doing this for Wordpress, I am including the code specifically for working in the Wordpress DB...

global $wpdb;
define('FOO_TABLE', $table_prefix."footable");

$result = $wpdb->get_results("SHOW TABLE STATUS LIKE '".FOO_TABLE."';");
foreach ($result as $data) {
  $updatetime = $data->Update_time;
}
Tribalcomm