0

Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?

I am looking for the result out of a query, but it keeps giving me resource id #3.

The following is my code.

$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$typeResult = mysql_query($type);

print_r($typeResult);

What step am I missing here?

Community
  • 1
  • 1
wowzuzz
  • 1,398
  • 11
  • 31
  • 51

4 Answers4

2

You need to fetch the result. All you're doing is sending the query.

Be aware that if you are writing new code, you should use mysqli_ or PDO functions as your query is vulnerable to SQL injection and mysql_ functions are being deprecated. Hesitantly, below is a sample for mysql_fetch_assoc.

<?php

$sql = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";

$result = mysql_query($sql);

if (mysql_num_rows($result) == 0) {
    echo "No rows found, nothing to print so am exiting";
    exit;
}

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
    echo $row[sellingid];
}

mysql_free_result($result);

?>

Reference

Kermit
  • 33,827
  • 13
  • 85
  • 121
  • Thank you for your detailed response. I assume php.net is the best resource for learning mysqli functions? – wowzuzz Oct 25 '12 at 19:26
  • 1
    @wowzuzz You can use php.net as a reference. There are plenty of resources and tutorials on `mysqli_` or `PDO` functions. – Kermit Oct 25 '12 at 19:33
0
$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$typeResult = mysql_query($type);
$row = mysql_fetch_array($typeResult);
print_r($row);
techtheatre
  • 5,678
  • 7
  • 31
  • 51
0

You are not actually fetching the results of your query. Below are two examples that use WHILE loops to fetch the results as rows. You can then grab the column values and work with them.

Incorrect and depreciated method, but working:

$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$typeResult = mysql_query($type);
// for each row
while ($row = mysql_fetch_array($typeResult)) {
    // grab the columns
    $value = $row['column_name'];
}

I would recommend using MySQLi or PDO like to following (MySQLi):

$mysqli_connection = new mysqli("hostname", "username", "password", "database");
$type = "SELECT `sellingid` FROM `ticket` WHERE `ticketid` = $_GET[ticketid]";
$res = $mysqli_connection->query($type);
while($row = $res->fetch_array(MYSQLI_ASSOC)) {
    $value = $row['column_name'];
}
$res->free();
$mysqli_connection->close();
burmat
  • 2,548
  • 1
  • 23
  • 28
0

More clear hint - use MySQLi class/functions, read this:

http://lt1.php.net/manual/en/mysqli-result.fetch-assoc.php

or if you like OOP approach more then

http://lt1.php.net/manual/en/mysqli-result.fetch-object.php