PHP - Displaying Results From A Mysql Database
Hi
I have got results being displayed after clicking the search button in a form on my home page but it brings up all the results which is ok but how do I get onlt the results a user searches for for example a location or property type etc as its for a property website The coding is below for the results page Also sorry how do I add a background image to the php page, I tried using css but wouldn't work Code: [Select] <style type="text/css"> body {background-image:url('images/greybgone.png');} </style> <?php mysql_connect ("2up2downhomes.com.mysql", "2up2downhomes_c","mD8GsJKQ") or die (mysql_error()); mysql_select_db ("2up2downhomes_c"); echo $_POST['term']; $sql = mysql_query("select * from properties where typeProperty like '%$term%' or location like '%$term%'"); while ($row = mysql_fetch_array($sql)){ echo 'Type of Property: '.$row['typeProperty']; echo '<br/> Number of Bedrooms: '.$row['bedrooms']; echo '<br/> Number of Bathrooms: '.$row['bathrooms']; echo '<br/> Garden: '.$row['garden']; echo '<br/> Description: '.$row['description']; echo '<br/> Price: '.$row['price']; echo '<br/> Location: '.$row['location']; echo '<br/> Image: '.$row['image']; echo '<br/><br/>'; } ?> Similar TutorialsHi, This has been baffling me for a couple hours now and i cant seem to figure it out. I have some code which creates an array and gets info from a mysql database and then displays in a list. This works great but after adding more and more rows to my database the list is now becoming quite large and doesnt look great on my site. Is it possible to split the list into multiple columns of about 25 and if possible once 3 or 4 columns have been created start another column underneath. To help explain i would be looking at a layout as follows: Code: [Select] line 1 line 1 line 1 line 2 line 2 line 2 ... ... ... line 25 line 25 line 25 line 1 line 1 line 1 line 2 line 2 line 2 ... ... ... line 25 line 25 line 25Im guessing there should be some sort of if statement to check how many items are being displayed and to create a new column if necessary. Is this correct? Thanks, Alex Hey all, First off, if this is in the wrong section I apologize. I wasn't sure if it should be here or the mySQL section. What's going on is, I'm in the process of learning the Prepared Statement way of doing things and am changing / updating my code to reflect the changes. Everything was going fine until I attempted to do what I could do using old MySQL methods and that is display the queried results on the same page. I can place a query and display the results as they should be displayed if I only use one block of code. However, if I try to do any additional queries on the same page, they get killed and do not display anything even though I know the query is fine because I can test the exact same syntax below one a different page and it works. Here's a code snippet for an example: Code: [Select] Code: <table> <tr> <td> // The below code will display a selection box containing various strings such as "hello world", "great to be here", "Wowserz", "this is mind blowing" etc. that are stored in the database. <?php echo "<select = \"SpecialConditions\">"; if($stmt->num_rows == NULL){ echo "No results found."; }else{ while($stmt->fetch()){ echo "<option value=\"$specialId\">$specialcondition</option>"; } } echo "</select>"; ?> </td> <td> // If I place another fetch query below the above fetch() query, this one will not show up. This one is supposed to display values 1 - 20 that have been stored in the DB. <?php echo "<select = \"NumberSets\">"; if($stmt->num_rows == NULL){ echo "No results found."; }else{ while($stmt->fetch()){ echo "<option value=\"".$numbers."\">".$numbers."</option>"; } } echo "</select>"; ?> </td> </tr> </table> What am I doing wrong with this? When I use regular SQL queries I can display multiple results on the same page. The results are being pulled from two separate joined tables but I don't think that's the issue. Hello everyone, So what I'm trying to do is have a dropdown menu displaying a number of <options> for people to select and to update that selection to the database, easy enough right? But I want that option to be displayed as the "selected" option when the page is revisited or refreshed and I just can't figure it out!!! (Permission to bang head on desk?) It would seem like it sould be a really basic thing to do but it's got me completely and a lot of menus around the site are going to rely on this so I came to you guys for help. A simple example would be like the facebook edit profile page, the user selects whether they are Male or Female, the database gets updated and when you return the option you selected before is the one that appears as if selected="selected" had been done. I've tried everything I can think of (all be it from a learners perspective) with no joy, ive managed to get the database connection sorted, the tables done, the login with unique id $_SESSION, logout etc... so then when I got to this I thought... easy LOL yeah right. Some of this probably doesnt even make sense but I'll show you the kind of things I've tried... <select name="gender" size="1" id="gender"> <option value="male" <?php if ($gender == "male") {echo 'selected="selected"';} ;?>>Male</option> <option value="female" <?php if ($gender == "female") {echo 'selected="selected"';} ;?>>Female</option> </select> OR <select name="gender" id="gender"> <option value="" selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option> <option value="male" selected="<?php if ($gender == "male") {echo "selected";} else {echo "";} ;?>">Male</option> <option value="female" selected="<?php if ($gender == "female") {echo "selected";} else {echo "";} ;?>">Female</option> </select> OR <select name="gender" size="1" id="gender"> <option selected="<?php if (!isset($gender)) {echo "selected";} ;?>">Select</option> <option value="<?php if ($gender == "Male") {echo "selected";} else {echo "male";} ;?>">Male</option> <option value="<?php if ($gender == "Female") {echo "selected";} else {echo "female";} ;?>">Female</option> </select> OR <select name="gender" id="gender"> <option value="male"><?php if ($gender == "male") {echo "Male";} ;?></option> <option value="female"><?php if ($gender == "female") {echo "Female";} ;?></option> </select> Honestly man, I've got no idea. The other thing is, I have more than 1 dropdown menu in the same form (5 in total) and if I use 2 or more selecting different options as I go I get a blank screen. And one more, if I have selected Male and it updates the users row and I resubmit Male again it's blank screen time again, lol. Any help would be tremendous and greatly appreciated. Thanks very much, Learner P.S Man! Hey guys, Having a slight problem with part of the code in my index.php file Code: [Select] mysql_select_db('db_name', $con); $result = mysql_query("SELECT * FROM spy ORDER BY id desc limit 25"); $resulto = mysql_query("SELECT * FROM spy ORDER BY id desc"); $count = mysql_num_rows($resulto); while($row = mysql_fetch_array($result)) { ?> <div class="contentDiv">Someone is looking at <?=$row[title];?> Stats for "<a href="/<?=$row[type];?>/<?=$row[code];?>/<?=$row[city];?>"><?=$row[code];?> <?=$row[city];?></a>"</div> <?}?> </div> <div id="login"></div> <? include("footer.php"); ?> </div> </body> </html> I'm getting the following error when viewing the file Code: [Select] Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in ~path to file/index.php on line 70 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in ~path to file/index.php on line 71 where lines 70 and 71 are $count = mysql_num_rows($resulto); while($row = mysql_fetch_array($result)) Any ideas on how to fix this? Hello, I have a quick question about methods for retrieving records from a mysql table and displaying them as a links For example, imagine I have three tables called countries, cities and city_info. I'd like to be able to select a country and have a list of that country's city names returned as links. I'd then like to be able to click on the link for London, say, and that would trigger a mysql query to retrieve the entry in city_info about London. Are there any functions that allow this? If anyone could point me in the right direction for further research I'd be grateful. Thanks. Hi Everyone, Well I have a bit of a problem... I have created a search page that I would like to return results from my MySQL database. However for some reason I can not get my page to display the results. Instead I am getting an error page. Below is the code I am using: In addition to this, should I wish for a user to be able to edit the returned results by clicking a link, how would I do that? <?php session_start(); ?> <link rel="stylesheet" type="text/css" href="css/layout.css"/> <html> <?php $record = $_POST['record']; echo "<p>Search results for: $record<br><BR>"; $host = "localhost"; $login_name = "root"; $password = "P@ssword"; //Connecting to MYSQL MySQL_connect("$host","$login_name","$password"); //Select the database we want to use mysql_select_db("schedules_2010") or die("Could not find database"); $result = mysql_query("SELECT * FROM schedule_september_2010 WHERE champ LIKE '%$record%' ") or die(mysql_error()); // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row echo "<br><p>Your Schedule<BR></p><br>"; echo "<table border=1>\n"; echo "<tr> <td bgcolor=#444444 align=center><p><b>Champ</p></td> <td bgcolor=#444444 align=center><p><b>Date</p></td> <td bgcolor=#444444 align=center><p><b>Start Time</p></td> <td bgcolor=#444444 align=center><p><b>End Time</p></td> <td bgcolor=#444444 align=center><p><b>Department</p></td> <td bgcolor=#444444 align=center><p><b>First Break</p></td> <td bgcolor=#444444 align=center><p><b>Second Break</p></td> <td bgcolor=#444444 align=center><p><b>Login ID</p></td> </tr>\n"; do { printf("<tr> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> <td><p>%s</p></td> </tr>\n" , $row["champ"] , $row["date_time"] , $row["start_time"] , $row["end_time"] , $row["department"] , $row["first_break"] , $row["second_break"] , $row["login_id"] ); } while ($row = mysql_fetch_array($result)); echo "</table>\n"; } else { echo "$champ No Records Found"; } mysql_free_result($result); mysql_close($con); ?> </html> Say a user puts in a support request, and for every request it generates a unqiue string, and enters it into the database. Ok, now say there is a text field, when the user enters their unique string and it finds a match, it displays the data along with it. How can I accomplish this? Im kind of new to mysql, but I know basic SQL. Would be great if somebody could point me in the right direction! Thanks Hi, I have a website where users can log on and edit their profile pic, name, biography etc. I was wondering about the correct way to:- Add data to the database through forms (Register.php) Display the data on a page Using mysql escape sting, however, the way I am currently using will display a '\' before any ' symbol. So it's >> it\'s ... Here is a snippet of the code I am using... Code: [Select] //insert data $about1 = mysql_real_escape_string($_POST['about']); //get $query = mysql_query("SELECT * FROM `staff` WHERE username='$username'"); $row = mysql_fetch_array($query); $about = $row['about']; echo $about; Not really sure how to get the images I have stored in MySQL into a html form. I can call-up the text fields from the database but it cannot seem to find the index for the images. Here is my code:- <?php session_start(); mysql_connect("localhost","root","abc") or die ("Error! Cannot connect to database"); mysql_select_db("theimageworks") or die ("Cannot find database"); $query = "SELECT * FROM jobs"; $result = mysql_query($query) or die (mysql_error()); ?> <?php //display data in html table echo "<table>"; echo "<tr><td>Username</td><td align='center'>Message</td><td>Product Image</td></tr>"; while($row = mysql_fetch_array($result)) { echo "</td><td>"; echo $row['username']; echo "</td><td>"; echo $row['message']; echo "</td></tr>"; echo $row['image']; } echo "</table>"; ?> The error message I get is "Notice: Undefined index: image in....." Thanks in advance! This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=308347.0 need a little help guys! I use the script below to display profile images, trouble is it shows 1 on top of the other, and i need it to double up 2 profile images on top of 2 profile images ect any ideas how i can do this. require("./include/mysqldb.php"); $con = mysql_connect("$dbhost","$dbuser","$dbpass"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("$dbame", $con); $result = mysql_query("SELECT * FROM Search_profiles_up WHERE upgrade_one ='1' ORDER BY RAND() LIMIT 40"); print "<table width=\"293\" height=\"111\" border=\"0\"> <tr>\n"; while($row = mysql_fetch_array($result)) { print "<td width=\"142\"><img src=" . $row['search_small_image'] . " width=\"144\" height=\"169\" /></td>\n"; print " </tr>\n"; print " <tr> \n"; print "<td>" . $row['star'] . "</td>\n"; print " </tr>\n"; print " <tr>\n"; print "<td>" . $row['username_search'] . "</td>\n"; print " </tr>\n"; print " <tr> \n"; print "<td>" . $row['phone_search'] . "</td>\n"; print " </tr> \n"; } print "</table>"; mysql_close($con); ?> Need some help I have 2 tables in a database and I need to search the first table and use the results from that search, to search another table, can this be done? and if it can how would you recommend that I go about it? Thanks For Your Help Guys! I have a PHP while loop that pulls from an SQL database and displays the contents in a table with two columns.
// Check Connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } // Select Data Table $result = mysqli_query($con,"SELECT * FROM Recommendations") or die(mysqli_error); // Split Data $mid = ceil(mysqli_num_rows($result)/2); // Display Content while ($rows = mysqli_fetch_array($result)) { $Name = $rows['Name']; $Author = $rows['Author']; $Cover = $rows['Link to Cover']; $Link = $rows['Link to Profile']; echo "<table><tr><td> <a href='" . $Link . "' >$Name</a> <br /> $Author <br /> <a href='" . $Link . "' ><img src='" . $Cover . "' /></a> </td> <td> <a href='" . $Link . "' >$Name</a> <br /> $Author <br /> <a href='" . $Link . "' ><img src='" . $Cover . "' /></a> </td></tr></table>"; } ?>I want to be able to display the looped results side by side in columns of two. Example: 1 2 3 4 5 6 I've tried using pseudo classes to display only the even and odd results in the different table columns, but honestly have no idea how to do this. I'm new to PHP, so my apologies if the results are really obvious. Thanks in advance! Hello, Im trying to display some results from mysql database, however none display. Can anyone tell me where im going wrong please? Code: [Select] </head><body> <div id="listhold"> <div class="list"> <a href="Restaurants.html">Restaurants</a><br /> <?php mysql_connect("","",""); mysql_select_db("") or die("Unable to select database"); $result = mysql_query("SELECT name FROM business WHERE type ='restaurant' ORDER BY name"); $number_of_results = mysql_num_rows($result); $results_counter = 0; if ($number_of_results != 0) {while ($array = mysql_fetch_array($result)) $results_counter++; if ($results_counter >= $number_of_results);} ?> </div> I'm realitivly new to PHP and was hoping somebody could help. I have a mysql database that stores information about books. I am currently using the code below to query the database and extract the 3 most recent entries and showing them in a dynamic list: Code: [Select] $sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 3"); $productCount = mysql_num_rows($sql); if ($productCount > 0) { // ensure a book exists while($row = mysql_fetch_array($sql)){ $id = $row["id"]; $title = $row["title"]; $author = $row["author"]; $price = $row["price"]; $date_added = strftime("%b %d, %Y", strtotime($row["date_added"])); $dynamiclist .= //My table showing the products } } else { $dynamicList = "There are currently no Books listed in this store"; } This works well when showing the most recent books 1 below the other. However, I would like to show these products side by side, horizontally across the page. Can somebody please point me in the right direction? Many thanks Dear all, I am new in this forum. This is my code $query = " SELECT webdb.id, webdb.writer, writer.picLoc, webdb.title FROM webdb, writer WHERE webdb.writer=writer.name and category = 'Researchworks' and language = 'Farsi' ORDER BY writer DESC"; $resultaat = mysql_query($query, $LinkID); $column_count = mysql_num_fields($resultaat) or die (mysql_error()."<br>Couldn't execute query: $SQLquery"); $counter=1; echo "<table border=\"0\" width=\"700\" border color=white><tr>"; while ($row = mysql_fetch_row($resultaat)) { if ($author !== $row[1]) { $author = $row[1]; echo "<td align=right width=220 valign=top style=\"margin: 5px; float: right border-bottom-color:#000; border-left-color:#000;\">"; echo "<img width=\"50\" height=\"80\" src=\"admin/writers/$row[2]\" border =\"0\"><br>".$row[1]."<br>"; echo "<a href=\"poems.php?writer=$row[1]\">".$row[3]."</a><br>"; echo "</td>"; if($counter%3==0) { echo"</tr><tr>"; } $counter++; } } echo"</tr></table>"; i have authors with different articles on a certain topic. What i want is, displaying the name of the author only once and all his titles under his name. I also want a dynamic table where i display three authors in each row and soon as there a fourth author a new row must start. My problem now is is the title is also being filtered and i can only display one title. Thanks in advance I'm trying to create a list that groups information by username. Only part of it is working. The first query ($get_item_sql) is grouping the information perfectly but the second query ($get_sold) is lumping the $item_price and $item_amount_due as one total for each one and outputting the same amounts into every username. I'm stuck on this and would appreciate your help. For example: Username item fees image fees item sales item price total due Jim 2 $0.40 $100.00 $3.00 $3.40 Kelly 5 $1.00 $100.00 $3.00 $4.00 This example shows the columns in red as being the problem where Kelly didn't sell anything so her "item sales" and "item price" should be $0.00 but is carrying Jim's totals into hers. Hope this helps! Thank you! $get_item_sql = mysql_query("SELECT id, username, date, ROUND(price,2) AS price, SUM(item_fee) AS fee, item_fee, SUM(sold) AS sales, SUM(ROUND(price,2)) AS total FROM product WHERE MONTH(date) = MONTH(DATE_ADD(CURDATE(),INTERVAL -1 MONTH)) GROUP BY username" ) or die(mysql_error()); if (mysql_num_rows($get_item_sql) < 1) { //invalid item $display_block .= "<p><em>Invalid item selection.</em></p>"; } else { //valid item, get info while ($item_info = mysql_fetch_array($get_item_sql)) { $item_username = $item_info['username']; $item_date = $item_info['date']; $item_price = $item_info['price']; $item_fee = $item_info['fee']; $image_fees = $item_fee * .20; $item_sold = $item_info['sales']; $get_sold = mysql_query("SELECT SUM(ROUND(price,2)) AS total, SUM(ROUND(sold,2)) AS sales, date, username FROM product WHERE sold = '1' AND MONTH(date) = MONTH(DATE_ADD(CURDATE(),INTERVAL -1 MONTH)) GROUP BY username") or die(mysql_error()); if (mysql_num_rows($get_sold) < 1) { //invalid item $display_block .= "<p><em>Invalid item selection.</em></p>"; } else { //valid item, get info while ($item_sold2 = mysql_fetch_array($get_sold)) { $item_sales = $item_sold2['total']; $item_price = ($item_sold2['total']) * .03; $item_amount_due = $image_fees + $item_price; $content .= "<form action=\"add_artist.php\" method=\"post\"><table class=\"anotherfont\" width=\"670\" border=\"0\"> <tr><td width=\"201\">{$item_username}</td> <td width=\"109\">{$item_fee}</td> <td width=\"109\">{$image_fees}</td> <td width=\"109\"> {$item_sales}</td> <td width=\"109\"> {$item_price}</td> <td width=\"109\"><input name=\"balance_due\" type=\"text\" value=\"{$item_amount_due}\" /></td> </tr><br /></table></form>"; } } } } Hi everyone. I'm stuck on the following query. I need to display all the fields listed below on a page, but linked via communications.CommID. I'd appreciate any assistance you can provide. thank you. Code: [Select] <?php $result = mysql_query("SELECT records.NameFirst_1, records.NameLast_1, records.CompanyName, records.CompanyBranch, records.CompanyReferenceNumber, records.CaseOwnerSelect, communications.ConversionType, communications.Contact, communications.ContactFrom, communications.CommID, communications.ContactPosition, communications.ContactTelephone, communications.ContactEmail, communications.ContactFax, communications.CallDate, communications.CallTime, communications.ActionTextField FROM records INNER JOIN communications ON records.IDNumber = '$IDNumber'") or die(mysql_error()); $row = mysql_fetch_array($result); ?> |