Jump to content

Echo output from a search


Go to solution Solved by osyrys14,

Recommended Posts

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

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

Edited by Joshua F

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>

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>";
  • Solution

:-\ 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.

Edited by osyrys14
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.