Jump to content

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource


tboyer

Recommended Posts

I know you folks have seen this topic before.  I've found a dozen solutions to the problem, and none of them apply to me.  I've been staring at the code for three days now, and I KNOW it's something stupid - I just can't see where. 

 

I've put in error reporting; I've simplified the query so that it's just getting everything; I've eliminated the loop in the hope of getting back just one row.  Nothing works.  Assistance greatly appreciated.

 

PHP 5.1.6 on a RHEL5.2 system.  I'm trying to do something very, very simple:

 

[tim@melbourne www.denmantire.com]$ cat test.php

#! /usr/bin/php

 

<?php

 

  $db_user="apache";

  $db_name="shipto";

  $db_pass="REDACTED";

 

  $conn = mysql_connect("localhost", $db_user, $db_pass);

  if (!$conn) {

    die('Not connected : ' . mysql_error());

  }

 

  $db_selected=mysql_select_db($db_name, $conn);

  if (!$db_selected) {

    die ('Can\'t use $db_name : ' . mysql_error());

  } 

 

  $query = "SELECT * FROM shipto_data_2;";

  $result = "mysql_query($query, $conn)";

 

  if (!$result) {

    die ('Could not successfully run query ($result) from DB: ' . mysql_error());

  }

 

  if ($row = mysql_fetch_assoc($result)) {

      return $row;

  } else {

      die ('Could not getch row from DB:' . mysql_error());

  }

 

?>

 

 

[tim@melbourne www.denmantire.com]$ ./test.php 

 

PHP Warning:  mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/www/www.denmantire.com/test.php on line 28

Could not getch row from DB:

 

The mysql commands work manually:

 

[tim@melbourne www.denmantire.com]$ mysql --database shipto -pREDACTED -u apache

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 99

Server version: 5.0.45 Source distribution

 

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

 

mysql> SELECT * FROM shipto_data_2;

...

+---------------+--------------------------------+-----------------+--------------+------------+------------------+-----------------+

673 rows in set (0.00 sec)

 

 

You never actually execute your query. All you do is save the string "mysql_query($query, $conn)" into a variable called $result.

 

Change this...

 

$result = "mysql_query($query, $conn)";

 

to....

 

$result = mysql_query($query, $conn);

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.