MarkMan5 Posted March 3, 2006 Share Posted March 3, 2006 Below is my code snippet, I'm sure there is an easy fix but I am stumped. Any help is appreciated :-) -thanks!It returns "Resource id #6" and I've tried tweaking the query many, many times with no luck.[code] function grabData($column, $table, $index) { /* connect to db */ mysql_connect('localhost','DBName,'PW'); $dbcnx=mysql_select_db('TableName'); if (!$dbcnx) { exit("Unable to connect to the database server at this time."); } /* construct and run the query */ $query = "SELECT $column FROM $table WHERE ItemNumber = $index"; $result=mysql_query($query); echo $result; return; }[/code] Link to comment https://forums.phpfreaks.com/topic/3988-resource-id-6-error-when-querying-mysql-via-php/ Share on other sites More sharing options...
kenrbnsn Posted March 3, 2006 Share Posted March 3, 2006 In these lines of code:[code]<?php $query = "SELECT $column FROM $table WHERE ItemNumber = $index"; $result=mysql_query($query); echo $result;?>[/code]The variable $result now contains a resource (or a pointer to the results). You're not using the function to actually get the data.[code]<?php $row = mysql_fetch_assoc($result); ?>[/code]Now the variable $row is an array containing all the fields, to see what it contains, you can do[code]<?php echo '<pre>'.print_r($row,true).'</pre>'; ?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/3988-resource-id-6-error-when-querying-mysql-via-php/#findComment-13883 Share on other sites More sharing options...
MarkMan5 Posted March 3, 2006 Author Share Posted March 3, 2006 Thanks Ken! That got me closer to where I wanted to be (printing just the one value)I replaced your print_r statement with the code below, and it prints just the value. Please advise if there is a better or cleaner way to do this. Many thanks again.[code]/* print the result */foreach ($row as $value){print_r ($value);}[/code] Link to comment https://forums.phpfreaks.com/topic/3988-resource-id-6-error-when-querying-mysql-via-php/#findComment-13961 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.