airpirate Posted May 27, 2008 Share Posted May 27, 2008 I am learning PHP and have a project that I need help with. I am trying to print results of a Mysql query that has variables involved. For an example I have a database that has 14 fields in it. My users if they fill in the first page then it fills in five of the fields, if they fill in the second page then it fills in the rest of the of the fields. I want to build an admin page that that I can search for the partial rows or full rows in a date range specified by the admin. I think I have figured out how to get the query to work, but I can't get it to print. I just need the rows to print one line at a time like, name, email, phone, etc... name2, email2, phone2, etc... name3, email3, phone3, etc... Here is the script that I have tried, I know the print won't format like I want it. But right now I can't even get it to print anything on the results page. <?php //Variables $type = addslashes($_POST['type']); $fday = addslashes($_POST['from_day']); $fmonth = addslashes($_POST['from_month']); $fyear = addslashes($_POST['from_year']); $tday = addslashes($_POST['to_day']); $tmonth = addslashes($_POST['to_month']); $tyear = addslashes($_POST['to_year']); // Databse Connection @$pfw_host = "localhost"; @$pfw_user = "user"; @$pfw_pw = "pass"; @$pfw_db = "database"; $pfw_link = mysql_connect($pfw_host, $pfw_user, $pfw_pw); if (!$pfw_link) { die('Could not connect: ' . mysql_error()); } $pfw_db_selected = mysql_select_db($pfw_db, $pfw_link); if (!$pfw_db_selected) { die ('Can not use $pfw_db : ' . mysql_error()); } $query = sprintf("SELECT name, email, phone, own_biz, current_income, desired_income, serious, willingtostart, best_time FROM form WHERE q1='%s' AND date BETWEEN '%s-%s-%s' and '%s-%s-%s'", mysql_real_escape_string($type), mysql_real_escape_string($from_day), mysql_real_escape_string($from_month), mysql_real_escape_string($from_year), mysql_real_escape_string($to_day), mysql_real_escape_string($to_month), mysql_real_escape_string($to_year)); $result = mysql_query($query); if (!$result) { $message = 'Invalid query: ' . mysql_error() . "\n"; $message .= 'Search Results ' . $query; die($message); } while ($row = mysql_fetch_row($result)) { echo $row['name']; echo $row['email']; echo $row['phone']; echo $row['q1']; echo $row['q2']; echo $row['q3']; echo $row['q4']; echo $row['q5']; echo $row['q6]; } mysql_close($pfw_link); ?> I am using PHP5 and MySql 5.0.32. Any help would greatly be appreciated. Link to comment https://forums.phpfreaks.com/topic/107390-php-mysql-query-print-results/ Share on other sites More sharing options...
Psycho Posted May 27, 2008 Share Posted May 27, 2008 mysql_fetch_row() returns th results in an enumerated array (i.e. $row[0], $row[1], $row[2], etc.). You want to use mysql_fetch_assoc() to have the results returned in an associative array (i.e. $row['name'], $row['email'], $row['phone'], etc.) Link to comment https://forums.phpfreaks.com/topic/107390-php-mysql-query-print-results/#findComment-550566 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.