2

I have this function

function query($query)
{
     return mysql_query($query) or die(mysql_error())
}

but when I call it like

$last = query("SELECT * FROM jubox ORDER BY id desc LIMIT 45");

$last returns 1

 echo $last; //1

What am I doing wrong?

EDIT:

I want to return RESOURCE id NOT DATA

Community
  • 1
  • 1
genesis
  • 50,477
  • 20
  • 96
  • 125
  • It doesn't seem like you can get the resource ID (http://www.php.net/manual/en/language.types.resource.php). Why would you want to do that? – Matthieu Napoli Jul 05 '11 at 11:46
  • beause mysql_fetch_assoc then gives me Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in /var/www/jukebox/fronta.... and i was just debugging (by echo – genesis Jul 05 '11 at 11:47

5 Answers5

3

Currently, you are trying to echo out a resource, which returns 1 if valid (if echoing a resource, it does indeed echo 1)

You need to use a mysql_fetch_* function to get the data

Jan Dragsbaek
  • 8,078
  • 2
  • 26
  • 46
3

Don't mix return and x OR y

<?php
$mysql = mysql_connect('localhost', 'localonly', 'localonly') or die(mysql_error());
mysql_select_db('test', $mysql) or die(mysql_error($mysql));
mysql_query('CREATE TEMPORARY TABLE jubox_so (id int auto_increment, primary key(id))') or die(mysql_error($mysql));

$last = query("SELECT * FROM jubox_so ORDER BY id desc LIMIT 45");
echo $last;

function query($query)
{
    $v = mysql_query($query);
    if ( !$v ) {
        die(myql_error());
    }
    return $v;
}

prints (something like) Resource id #6 as expected instead of 1 (which was caused by the bool->string conversion for echo)

VolkerK
  • 95,432
  • 20
  • 163
  • 226
1

Look into mysql_fetch_* functions. They return actual rows for you. The mysql_query returns a link_identifier that can be used to fetch the results. See http://php.net/manual/en/book.mysql.php for the different functions available.

toomasr
  • 4,731
  • 2
  • 33
  • 36
0

You're echoing the result right? In case of a select this is a mysql result object and not true or false (like with update or insert). What you get is a result resource. You need to use functions as fetch_assoc to explore that result.

hoppa
  • 3,011
  • 18
  • 21
  • I think you didn't get me. I'm not amateur in PHP but this is very strange case when it does return bool instead of resource id ... – genesis Jul 05 '11 at 11:39
0

i think the reason why you print out '1', is because your query result of an error and then it is the returned value of the die() that you get back. Check you query, copy and paste it in phpMyAdmin for exemple, and look for an error