osyrys14 Posted March 27, 2013 Share Posted March 27, 2013 I'm using the following code to query and echo data from a search, and I'm trying to figure out how to use more of the fields in the output, including adding one of the columns together for a total, any help would be greatly appreciated. $query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE $db_tb_atr_name like '%".$query."%'"); echo "<h2>Search Results</h2><ol>"; while($data_fetch=mysql_fetch_array($query_for_result)) { echo "<li>"; echo substr($data_fetch[$db_tb_atr_name], 0,160); echo "</li><hr/>"; } echo "</ol>"; Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/ Share on other sites More sharing options...
Joshua F Posted March 27, 2013 Share Posted March 27, 2013 I think changing mysql_fetch_array() to mysql_fetch_assoc() would do it. Someone correct me if I'm wrong. mysql_fetch_array() is more generic than mysql_fetch_assoc(), although it can generate the exact same results using proper parameters. However, if you only need an associative array, I would advise you to use the latter, as the former makes the code more ambiguous or heavy, depending on how you use it. Source: http://stackoverflow.com/a/5291403 Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/#findComment-1421316 Share on other sites More sharing options...
osyrys14 Posted March 27, 2013 Author Share Posted March 27, 2013 Now I am back to getting nothing when I put something in the search box, and returning all rows when nothing is entered. I do appreciate the help! Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/#findComment-1421317 Share on other sites More sharing options...
Joshua F Posted March 27, 2013 Share Posted March 27, 2013 Where does $query come from? Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/#findComment-1421319 Share on other sites More sharing options...
osyrys14 Posted March 27, 2013 Author Share Posted March 27, 2013 I take that back, I'm getting the same results with both mysql_fetch_array() and mysql_fetch_assoc()... Here's the whole page I have... There are about 20 fields in the DB to return and manipulate/calculate, and it's only returning one... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Search</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form method="get" action=""> <label>Search For: </label><input type="text" name="query" /> <input type="submit" name="submit" value="Start Search" /> <input type="reset" value="Reset" </form> <?php if(isset($_GET['submit'])){ $db_host="localhost"; $db_username="root"; $db_password="qwe123"; $db_name="cdrdata"; $db_tb_name="cdr"; $db_tb_atr_name="8"; mysql_connect("$db_host","$db_username","$db_password"); mysql_select_db("$db_name"); $query=mysql_real_escape_string($_GET['query']); $query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE `$db_tb_atr_name` like '%".$query."%'"); echo "<h2>Search Results</h2><ol>"; while($data_fetch=mysql_fetch_assoc($query_for_result)) { echo "<li>"; echo substr($data_fetch[$db_tb_atr_name], 0,160); echo "</li><hr/>"; } echo "</ol>"; mysql_close(); } ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/#findComment-1421320 Share on other sites More sharing options...
Joshua F Posted March 27, 2013 Share Posted March 27, 2013 Try echo mysql_num_rows($query_for_result); under the actual query and see what number it displays. Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/#findComment-1421322 Share on other sites More sharing options...
osyrys14 Posted March 27, 2013 Author Share Posted March 27, 2013 I'm getting results now, but still just the one field, and I'm trying to manipulate the data pulled out of the DB, only getting the one field, which I'm guessing has something to do with this part of the code, just can't get anything to work to show the rest, and total one of the columns. $query=mysql_real_escape_string($_GET['query']); $query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE `$db_tb_atr_name` like '%".$query."%'"); echo "<h2>Search Results</h2><ol>"; while($data_fetch=mysql_fetch_assoc($query_for_result)) { echo "<li>"; echo substr($data_fetch[$db_tb_atr_name], 0,160); echo "</li><hr/>"; } echo "</ol>"; Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/#findComment-1421335 Share on other sites More sharing options...
Joshua F Posted March 27, 2013 Share Posted March 27, 2013 $data_fetch[$db_tb_atr_name] would actually be the same as $data_fetch['8'] which would make it only display data from the column named 8. Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/#findComment-1421336 Share on other sites More sharing options...
osyrys14 Posted March 27, 2013 Author Share Posted March 27, 2013 Right, and I've tried adding more $db_tb_atr_namex="columnname" and several other options to the code, and either blows up the code, or just doesn't echo anything... Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/#findComment-1421337 Share on other sites More sharing options...
osyrys14 Posted March 27, 2013 Author Share Posted March 27, 2013 I found what I was doing wrong... I needed to add a named variable above, and another instance of echo substr($data_fetch[$db_tb_atr_name], 0,160) below... Thanks so much for the help, you got me closer to where I need to be! Next will be adding it all together. Link to comment https://forums.phpfreaks.com/topic/276205-echo-output-from-a-search/#findComment-1421338 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.