-2

I am trying to build a function to query and it is giving me a Resource id #6 when I echoed the function, it gave me no error.

This is the function:

$connect = mysql_connect($dbinfo['location'], $dbinfo['user'], $dbinfo['pass'], $dbinfo['database']);
$select_db = mysql_select_db($dbinfo['database']) or die('could not connect to database ' . $dbinfo['database']);
if(!$connect)
{
    die('could not connect to database: ' . mysql_error());
}
function query($sql)
{
    $result = mysql_query($sql);
    if (!$result) {
        echo "DB Error, could not query the database\n";
        echo 'MySQL Error: ' . mysql_error();
        exit;
    }
    return $result;
}

Here is how I called it:

<?php
require_once '_core/init.php';

echo query("SELECT username FROM users WHERE user_id = 1");
?>

Any help would be much appreciated. Thanks!

Carefree4
  • 33
  • 1
  • 1
  • 5
  • And what do you expect? – u_mulder Mar 08 '14 at 19:28
  • I expect it to echo the `username` of the user with the `user_id` of `1` – Carefree4 Mar 08 '14 at 19:29
  • HEy. mysql extension is deprecated in php and will be removed in the new php versions. so Use either Mysqli extenstion or PDO. – Mubo Mar 08 '14 at 19:29
  • Im building an app for an old system, have been told to use mysql. – Carefree4 Mar 08 '14 at 19:30
  • 1
    Have you read the manual of `mysql_query`? If you did, you would have known that it returns a resource to fetch the data from, rather than the data itself. You would also have known not to use `mysql_query` at all, since it is deprecated and will be removed in a future version of PHP. There's a difference between using MySQL the database and using this particular outdated MySQL connection module. – GolezTrol Mar 08 '14 at 19:30

4 Answers4

2

Read the manual

Return Values

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

So what you are seeing is expected behaviour.

If you're trying to get the results of your query you need to use one of the many functions available such as mysql_fetch_assoc().

FYI, you shouldn't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? 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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
John Conde
  • 217,595
  • 99
  • 455
  • 496
0

You should use mysql_fetch_array or mysql_fetch_assoc functions on that resource in order to retrieve an indexed or associative array of the MySQL rows returned.

Rápli András
  • 3,869
  • 1
  • 35
  • 55
0

RETURN VALUES: See DOC

(Dont use mysql_* functions because It will be deprecated in fututure versions. Use mysqli_* or PDO)

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

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

kpmDev
  • 1,330
  • 1
  • 10
  • 28
0

THe $result is the array of array having your table rows(which are stored as associative array, with table column name as the key). So you need to fetch each row by the following code:

while($row =mysqli_fetch_array($result)){
   echo $row['yourColumnNameHere'];
}
Omsai Jadhav
  • 134
  • 10