0

How do you save int, string, and blob all in the same php array. Here is what i have tried and it keeps giving me the same error -

Warning: mysql_fetch_array() expects parameter 1 to be resource,
boolean given in D:\Hosting\11116942\html\index.php on line 42 

I am connecting to the database like this:

$query = mysql_query("SELECT * FROM data WHERE index ='1'");
$data= mysql_fetch_array($query);
echo $data["title"]

Is there something wrong I am doing?

djf
  • 6,592
  • 6
  • 44
  • 62
user2438604
  • 91
  • 1
  • 3
  • 8

2 Answers2

0

The query you are executing is probably not returning any rows. Try this:

if (false !== ($query = mysql_query("SELECT * FROM data WHERE index ='1'")) {
    $data = mysql_fetch_array($query);
    echo $data['title'];
}
mproffitt
  • 2,409
  • 18
  • 24
  • no it is still giving me the error. i think it has something to do with the data types i have in that table row – user2438604 Jun 08 '13 at 17:45
  • It's more likely to be a problem with the query itself, PHP places no restrictions on retrieving different types in the same query. Does the query work if you run it directly? – mproffitt Jun 08 '13 at 17:57
0

From the manual:-

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

...

mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.

In your code the $query = false as mysql_query() is failing for some reason. Check you are connected to the database and that you have permission to access it.

Also check the datatype for index as you are querying it with a string and I suspect it is probably an int. Try:-

$query = mysql_query("SELECT * FROM `data` WHERE index = 1");

And check the return value as suggested by mplf.

Manuals can be useful on occasion and have been known to point people in the right direction when things go wrong.

Also, please, don't use mysql_* functions in new code. They are no longer maintained and the deprecation process has begun. Learn about prepared statements instead, and use PDO or MySQLi; this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
vascowhite
  • 18,120
  • 9
  • 61
  • 77