PHP - Sql Query To Select A Row By An Id Number "out Of" A Column?
The Script:
<?php include("connect.php"); ?> <?php echo "<h1>The Hashtag: </h1>"; if(isset($_GET['hashtag'])){ echo $_GET['hashtag']; } // Select the ID of the given hashtag from the "hashtags" table. $tqs = "SELECT `id` FROM `hashtags` WHERE `hashtag` = '" . $_GET['hashtag'] . "'"; $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc)); $row = mysqli_fetch_assoc($tqr); // This prints e.g.: // 100 echo "<br/><br/>"; print_r($row['id']); // Select the threads from the "thread" table which contains the hashtag ID number. $tqs_two = "SELECT * FROM `thread` WHERE `hashtag_id` IN ('" . $row['id'] . "')"; $tqr_two = mysqli_query($dbc, $tqs_two) or die(mysqli_error($dbc)); $row_two = mysqli_fetch_assoc($tqr_two); echo "<br/><br/>"; print_r($row_two); ?>The script should select the rows by that ID number of the hashtag. It should look in the "hashtag_id" column of the table and see if that ID number can be found there, if it can be found there, then it should select that row. The ID numbers are inside that "hashtag_id" column separated by commas. Example: 98, 99, 100My Question: How to do the SQL query so it selects the rows by the hashtag ID number? Edited by glassfish, 17 October 2014 - 10:35 AM. Similar TutorialsHello all,
Based on the suggestion of you wonderful folks here, I went away for a few days (to learn about PDO and Prepared Statements) in order to replace the MySQLi commands in my code. That's gone pretty well thus far...with me having learnt and successfully replaced most of my "bad" code with elegant, SQL-Injection-proof code (or so I hope).
The one-and-only problem I'm having (for now at least) is that I'm having trouble understanding how to execute an UPDATE query within the resultset of a SELECT query (using PDO and prepared statements, of course).
Let me explain (my scenario), and since a picture speaks a thousand words I've also inlcuded a screenshot to show you guys my setup:
In my table I have two columns (which are essentially flags i.e. Y/N), one for "items alreay purchased" and the other for "items to be purchased later". The first flag, if/when set ON (Y) will highlight row(s) in red...and the second flag will highlight row(s) in blue (when set ON).
I initially had four buttons, two each for setting the flags/columns to "Y", and another two to reverse the columns/flags to "N". That was when I had my delete functionality as a separate operation on a separate tab/list item, and that was fine.
Now that I've realized I can include both operations (update and delete) on just the one tab, I've also figured it would be better to pare down those four buttons (into just two), and set them up as a toggle feature i.e. if the value is currently "Y" then the button will set it to "N", and vice versa.
So, looking at my attached picture, if a person selects (using the checkboxes) the first four rows and clicks the first button (labeled "Toggle selected items as Purchased/Not Purchased") then the following must happen:
1. The purchased_flag for rows # 2 and 4 must be switched OFF (set to N)...so they will no longer be highlighted in red.
2. The purchased_flag for row # 3 must be switched ON (set to Y)...so that row will now be highlighted in red.
3. Nothing must be done to rows # 1 and 5 since: a) row 5 was not selected/checked to begin with, and b) row # 1 has its purchase_later_flag set ON (to Y), so it must be skipped over.
Looking at my code below, I'm guessing (and here's where I need the help) that there's something wrong in the code within the section that says "/*** loop through the results/collection of checked items ***/". I've probably made it more complex than it should be, and that's due to the fact that I have no idea what I'm doing (or rather, how I should be doing it), and this has driven me insane for the last 2 days...which prompted me to "throw in the towel" and seek the help of you very helpful and intellegent folks. BTW, I am a newbie at this, so if I could be provided the exact code, that would be most wonderful, and much highly appreciated.
Thanks to you folks, I'm feeling real good (with a great sense of achievement) after having come here and got the great advice to learn PDO and prepared statements.
Just this one nasty little hurdle is stopping me from getting to "end-of-job" on my very first WebApp. BTW, sorry about the long post...this is the best/only way I could clearly explaing my situation.
Cheers guys!
case "update-delete": if(isset($_POST['highlight-purchased'])) { // ****** Setup customized query to obtain only items that are checked ****** $sql = "SELECT * FROM shoplist WHERE"; for($i=0; $i < count($_POST['checkboxes']); $i++) { $sql=$sql . " idnumber=" . $_POST['checkboxes'][$i] . " or"; } $sql= rtrim($sql, "or"); $statement = $conn->prepare($sql); $statement->execute(); // *** fetch results for all checked items (1st query) *** // $result = $statement->fetchAll(); $statement->closeCursor(); // Setup query that will change the purchased flag to "N", if it's currently set to "Y" $sqlSetToN = "UPDATE shoplist SET purchased = 'N' WHERE purchased = 'Y'"; // Setup query that will change the purchased flag to "Y", if it's currently set to "N", "", or NULL $sqlSetToY = "UPDATE shoplist SET purchased = 'Y' WHERE purchased = 'N' OR purchased = '' OR purchased IS NULL"; $statementSetToN = $conn->prepare($sqlSetToN); $statementSetToY = $conn->prepare($sqlSetToY); /*** loop through the results/collection of checked items ***/ foreach($result as $row) { if ($row["purchased"] != "Y") { // *** fetch one row at a time pertaining to the 2nd query *** // $resultSetToY = $statementSetToY->fetch(); foreach($resultSetToY as $row) { $statementSetToY->execute(); } } else { // *** fetch one row at a time pertaining to the 2nd query *** // $resultSetToN = $statementSetToN->fetch(); foreach($resultSetToN as $row) { $statementSetToN->execute(); } } } break; }CRUD Queston.png 20.68KB 0 downloads Hi, I am new to PHP and I have this homework due Sunday, I got it on Friday btw I have complete the first part of it , but I can not complete the second part of it. This is the exercise: Largest Row and Column Write a 4x4 array and fill it with random 1's or 0's 0 0 1 1 0 1 0 0 1 0 1 1 1 0 0 1 Display which row and which column has the biggest number of 1's this is my code: <?php $num = array("0", "1"); $rows = rand(4, 4); //using rand() function to generate 4 different numbers, min & max=4 $cols = rand(4, 4); echo "<table>"; for($i = 1; $i <= $rows; $i++) { echo "<tr>"; for($j = 1; $j <= $cols; $j++) { $td_text = $num[rand(0,(count($num)-1))]; //number spacing echo '<td>'.$td_text.'</td>'; } echo "</tr>"; } ?> [edit : zane] Don't hesitate to use code tags next time ;), it's the <> button. For the section of code below, is there a way to say, "first time through the query print on A2, B2,etc, then each query following add increase the number besides column name to be A3, B3, etc, then A4, B4, etc"...? $result = mysqli_query($connection,"SELECT * FROM table") or die(mysqli_error($connection)); while($row = mysqli_fetch_array($result)) { //get all rows you want from the table $var1 = $row['var1']; $var2 = $row['var2']; $var3 = $row['var3']; $var4 = $row['var4']; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A2', $var1) ->setCellValue('B2', $var2) ->setCellValue('C2', $var3) ->setCellValue('D2', $var4); } This is for an excel spreadsheet. The code is currently printing/exporting one row only from the table. Thanks Hi everyone, Like the title says I need to count the amount of 1's in a column but I can't figure out how. Gr and thanx already Ryflex Please help me. I'm fairly new to php and have been looking for answers on Goog for about two weeks or so with not an answer. This is my first post on a forum of any type. I have a form on a php page that when submitted sends a value to a confirm php page Code: [Select] <form action='buyconfirmorder.php' method='post'> Purchase: <input type='text' name='ask_shares'><br /><br /> You Will Pay:  <input type='text' name='ask'><br /><br /> <input type='hidden' name='postcardname' value='$postcardname'> <input type='submit' name='submit' value='Order'> </form> I need to take the users input from the form and subtract each row from a mysql DB one at a time until the users input is down to a remainder and then I need the remainder (I'll be working with the remainder as well). Code: [Select] $query = mysql_query(" SELECT * FROM ask, search WHERE search.id = ask.card_search_id AND search.card_name = '$postcardname' ORDER BY ask_price ASC"); The array will be numbers only. So an example would be: User input from the form is 100.00 Code: [Select] while($rows = mysql_fetch_array($query)) { $ask_price = $rows['ask_price']; } Let's say the array is 50.50, 40.50, 35.00 I need some code to do: 100.00 - 50.50 = 49.50 49.50 - 40.50 = 9.00 9.00 - 35.00 = <0 does not do anything so remainder = 9.00 would it be something like Code: [Select] $new_ask_shares = $_POST['ask_shares']; //some sort of a loop or array or combo of both ($new_ask_shares - $ask_price); the numbers are just an example as the actual numbers will be different each time I query my DB. Thanks in advance hi, I want to count the total number of multiple select that user has selected instead of getting the value the user has selected How should I go about doing the counting part? Code: [Select] <form action="1.php" method="post"> <select name="test[]" multiple="multiple"> <option value="one">one</option> <option value="two">two</option> <option value="three">three</option> <option value="four">four</option> <option value="five">five</option> </select> <input type="submit" value="Send" /> </form> <?php $test=$_POST['test']; if ($test){ foreach ($test as $t){echo 'You selected ',$t,'<br />';} } ?> Hi, I currently have this code that selects all names from the table that begin with the letter 'A', does anybody know how to select all entries from the table that begin with a number? $query = "SELECT * FROM table WHERE name LIKE 'A%' Any help would be very much appreciated. is it possible to do something like Code: [Select] $today = date("Y-m-d"); $result = mysql_query("SELECT * FROM staff where date = '.$today.' "); also, is it normal for the first entry in the database not to be displayed? i have 6 entries in a table and only 2-6 are shown. when i changed the id for 1 to 7, it only displayed 3-7. I currently have a query that compiles a League Standings Table (the full code is below) and generate the ranking with Code: [Select] @rownum := @rownum+1 AS rank This works fine for the main standings page but I want to use the row numbers elsewhere. Each team has it's own page which uses 'team.team_id' as its 'recordID'. On these pages I would like to show that particular teams ranking in the standings. The two options I see would be to run the query filtering by 'team.team_id' but that would only return the one record so ranking would always be '1' or run the whole query and then somehow find which ranking relates to 'team.team_id' (something that may be possible with VIEWS but the database is running on MySQL4) but I cannot figure out how to get around this. For example, if the standings were Rank Team 1 Rovers 2 United 3 City 4 Rangers 5 Town 6 Athletic 7 Wanderers 8 Hotspur On the 'Rangers' page I would want it to show 'RANKING: 4' and on the 'Athletic' page it would show 'RANKING: 6'. On top of this I would want to show a small version of the rankings with one team above and one team below (it would actually be two teams but I will keep it simple until I understand it!) so one again given the two examples above I would get RANGERS PAGE Rank Team 3 City 4 Rangers 5 Town ATHLETIC PAGE Rank Team 5 Town 6 Athletic 7 Wanderers The query is $i = 1; $ht = "g.home_team = t.team_id"; $at = "g.away_team = t.team_id"; $hw = "g.home_goals > g.away_goals"; $aw = "g.home_goals < g.away_goals"; $d = "g.home_goals = g.away_goals"; $hg ="g.home_goals"; $ag ="g.away_goals"; $table = mysql_query("SELECT t.team_name as Tm, @rownum := @rownum+1 AS rank , (sum(CASE WHEN (".$ht." AND ".$hw.")OR(".$at." AND ".$aw.") THEN 3 ELSE 0 END) + sum(CASE WHEN (".$ht." OR ".$at.") AND ".$d." THEN 1 ELSE 0 END)) AS P , (sum(CASE WHEN (".$ht." AND ".$hw.") THEN 3 ELSE 0 END) + sum(CASE WHEN (".$ht.") AND ".$d." THEN 1 ELSE 0 END)) AS HP , (sum(CASE WHEN (".$at." AND ".$aw.") THEN 3 ELSE 0 END) + sum(CASE WHEN (".$at.") AND ".$d." THEN 1 ELSE 0 END)) AS AP , count(CASE WHEN (".$ht." OR ".$at.") THEN 1 ELSE 0 END) as GP , sum(CASE WHEN (".$ht." ) THEN 1 ELSE 0 END) as HGP , sum(CASE WHEN ".$at." THEN 1 ELSE 0 END) as AGP , sum(CASE WHEN (".$ht." AND ".$hw.") OR (".$at." AND ".$aw.") THEN 1 ELSE 0 END) AS W , sum(CASE WHEN (".$ht." AND ".$hw.") THEN 1 ELSE 0 END) AS HW , sum(CASE WHEN (".$at." AND ".$aw.") THEN 1 ELSE 0 END) AS AW , sum(CASE WHEN (".$ht." AND ".$d.") OR (".$at." AND ".$d.") THEN 1 ELSE 0 END) AS D , sum(CASE WHEN (".$ht." AND ".$d.") THEN 1 ELSE 0 END) AS HD , sum(CASE WHEN (".$at." AND ".$d.") THEN 1 ELSE 0 END) AS AD , sum(CASE WHEN (".$ht." AND ".$aw.") OR (".$at." AND ".$hw.") THEN 1 ELSE 0 END) AS L , sum(CASE WHEN (".$ht." AND ".$aw.") THEN 1 ELSE 0 END) AS HL , sum(CASE WHEN (".$at." AND ".$hw.") THEN 1 ELSE 0 END) AS AL , SUM(CASE WHEN (".$ht.") THEN ".$hg." WHEN (".$at.") THEN ".$ag." END) as GF , SUM(CASE WHEN (".$ht.") THEN ".$hg." END) as HGF , SUM(CASE WHEN (".$at.") THEN ".$ag." END) as AGF , SUM(CASE WHEN (".$ht.") THEN ".$ag." WHEN (".$at.") THEN ".$hg." END) as GA , SUM(CASE WHEN (".$ht.") THEN ".$ag." END) as HGA , SUM(CASE WHEN (".$at.") THEN ".$hg." END) as AGA , (SUM(CASE WHEN (".$ht.") THEN ".$hg." WHEN (".$at.") THEN ".$ag." END) - SUM(CASE WHEN (".$ht.") THEN ".$ag." WHEN (".$at.") THEN ".$hg." END)) as GD , (SUM(CASE WHEN (".$ht.") THEN ".$hg." END) - SUM(CASE WHEN (".$ht.") THEN ".$ag." END)) as HGD , (SUM(CASE WHEN (".$at.") THEN ".$ag." END) - SUM(CASE WHEN (".$at.") THEN ".$hg." END)) as AGD from teams t left join all_games g on t.team_id in (g.home_team,g.away_team) WHERE comp = '1' AND home_goals IS NOT NULL AND date BETWEEN '2010-07-01' AND '2011-06-31' GROUP BY t.team_id ORDER BY P desc, GD desc, GF desc The html is Code: [Select] <table width="" border="0" cellpadding="0" cellspacing="0" BORDER=1 RULES=ROWS FRAME=BOX> <tr> <td></td><td></td> <td colspan="9" align="center" bgcolor="#00FF99">ALL</td> <td colspan="9" align="center" >Home</td> <td colspan="9" align="center">Away</td> </tr> <tr> <td class="hdcell" >POS</td> <td class="hdcell" >Team</td> <td width="30" class="hdcell">P</td> <td width="30" class="hdcell">W</td> <td width="30" class="hdcell">D</td> <td width="30" class="hdcell">L</td> <td width="30" class="hdcell">F</td> <td width="30" class="hdcell">A</td> <td width="30" class="hdcell">GD</td> <td width="30" class="hdcell">Pts</td> <td width="30" class="hdcell"></td> <td></td> <td width="30" class="hdcell">P</td> <td width="30" class="hdcell">W</td> <td width="30" class="hdcell">D</td> <td width="30" class="hdcell">L</td> <td width="30" class="hdcell">F</td> <td width="30" class="hdcell">A</td> <td width="30" class="hdcell">GD</td> <td width="30" class="hdcell">Pts</td> <td></td> <td width="30" class="hdcell">P</td> <td width="30" class="hdcell">W</td> <td width="30" class="hdcell">D</td> <td width="30" class="hdcell">L</td> <td width="30" class="hdcell">F</td> <td width="30" class="hdcell">A</td> <td width="30" class="hdcell">GD</td> <td width="30" class="hdcell">Pts</td> </tr> <?php while ($row_table = mysql_fetch_assoc($table)){ echo '<tr> <td style="text-align:left" width="30">'.$i.'</td>'; echo '<td style="text-align:left">'.$row_table['Tm'].'</td> <td style="text-align:left">'.$row_table['GP'].'</td> <td style="text-align:left">'.$row_table['W'].'</td> <td style="text-align:left"> '.$row_table['D'].'</td> <td style="text-align:left"> '.$row_table['L']. '</td> <td style="text-align:left"> '.$row_table['GF']. '</td> <td style="text-align:left"> '.$row_table['GA']. '</td> <td style="text-align:left"> '.$row_table['GD']. '</td> <td style="text-align:left"> '.$row_table['P']. '</td> <td style="text-align:left"></td> <td style="text-align:left"></td> <td style="text-align:left">'.$row_table['HGP'].'</td> <td style="text-align:left">'.$row_table['HW'].'</td> <td style="text-align:left">'.$row_table['HD'].'</td> <td style="text-align:left"> '.$row_table['HL']. '</td> <td style="text-align:left"> '.$row_table['HGF']. '</td> <td style="text-align:left"> '.$row_table['HGA']. '</td> <td style="text-align:left"> '.$row_table['HGD']. '</td> <td style="text-align:left"> '.$row_table['HP']. '</td> <td style="text-align:left"></td> <td style="text-align:left">'.$row_table['AGP'].'</td> <td style="text-align:left">'.$row_table['AW'].'</td> <td style="text-align:left">'.$row_table['AD'].'</td> <td style="text-align:left"> '.$row_table['AL']. '</td> <td style="text-align:left"> '.$row_table['AGF']. '</td> <td style="text-align:left"> '.$row_table['AGA']. '</td> <td style="text-align:left"> '.$row_table['AGD']. '</td> <td style="text-align:left"> '.$row_table['AP']. '</td> </tr>'; $i++; } ?> </table> As always, thanks in advance for any tips or suggestions, even if they are telling me to scrap what I have so far and start again!! Steve PS. Can someone explain why @rownum := @rownum+1 AS rank causes $i to increment by one? I found the code while searching and do not understand why $i is necessary unless it is a reserved reference. The way the code looks, I should be able to output the ranking using 'rank' but that does not work. I have no problem with how it does things, it would just be nice to understand! Please I want get data from mysql; 1. The last 24 hours. 2 the last 7 days 3. the last Months 4. Last 12 hours please try and help me I need to write a query that will sum a column 'serv_cc_total' from the first day of the month to the date the record was saved to the db when the record for that date is viewed. Any help would be appreciated. Doug Hi, Im not sure whether this is a PHP or MySQL question, but im trying to get a select menu to only display the number of options that are defined in the mysql database.
For example
The code I have that retrieves the $quantity from the database.
Lets say that
$quantity = 3
Now I would like the option menu to only display three options, like this:
<select name="quantity"> <option value="<?php echo $quantity ?>"><?php echo 'Maximum of ' . $quantity . ' available'?></option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select>but for example, if the $quantity was equal to 10 then I would like the menu to be displayed as such <select name="quantity"> <option value="<?php echo $quantity ?>"><?php echo 'Maximum of ' . $quantity . ' available'?></option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select>How is it possible to do this? I am completely at a loss here. Many Thanks aquaman Ok basic setup is Table name = unit_data I have a field unit_paid_date colum type = DATE unit_paid_date = 2011-03-02 thats yyyy-mm-dd lets say for example the DueDate is the Second of every month I need to select all records from unit_data that are PAST DUE so if the last paid date is 2011-01-02 that record will pop up as PAST DUE BUT if that paid date is say 2011-03-10 it will not be shown because the invoice was paid Ahead of due date I had this almost working properly -- but can use any help you guys offer. I was attempting to use mysql WHERE queries and do checks against paid_date but failed Thanks I have searched this forum as well as over 200 other forums and have not found the answer that is specific to my question. I have shortened my code drastically to assist in resolving this quickly -
I have a search form that has criteria for the search criteria with "virtual" "columns" in an array but it's not working. If I search one column at a time it works just fine but when I try to search 8 columns with one select I get the following error: SELECT Error: Unknown column 'achievements' in 'where clause'.
When a user selects search in Achievements, I need it to look at all 8 columns that are associated with achievements and bring back the results that match - the same as if the user selects search in Associations, I need it to look at all 5 columns and bring back the results that match.
My shortened code is as follows:
<!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> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Search</title> </head> <body> <form name="search" action="" method="POST"> <p>Search:</p> <p> Achievements/Associations: <input type="text" name="find1" /> in <Select NAME="field1"> <Option VALUE="achievements">Achievements</option> <Option VALUE="associations">Associations</option> </Select> <br><br> Secondary Education: <input type="text" name="find2" /> in <Select NAME="field2"> <Option VALUE="edu1sectype">Highest Certificate Attained</option> <Option VALUE="edu1secname">Highest Grade Passed</option> <Option VALUE="edu1secinst">Name of High School</option> <Option VALUE="edu1secdate">Date Completed</option> <Option VALUE="edu1secinsttyp">Type of Institution</option> <Option VALUE="subjects">Subjects</option> </Select> <br><br> <input type="hidden" name="searching" value="yes" /> <input type="submit" name="search" value="Search" /> </p> </form> <?php $searching = $_POST['searching']; $find1 = $_POST['find1']; $field1 = $_POST['field1']; $find2 = $_POST['find2']; $field2 = $_POST['field2']; if ($searching =="yes") { echo "<br><b>Searched For:</b> $find1 $find2<br>"; echo "<br><h2>Results</h2><p>"; //If they did not enter a search term we give them an error // Otherwise we connect to our Database include_once "connect_to_mysql.php"; mysql_select_db("table_name") or die(mysql_error()); // We preform a bit of filtering $find = strtoupper($find); $find = strip_tags($find); $find = trim($find); $find = mysql_real_escape_string($find); $field = mysql_real_escape_string($field); $data = mysql_query("SELECT * FROM table_name WHERE upper(".$field1.") LIKE '%$find1%' AND upper(".$field2.") LIKE '%$find2%' ") or die("SELECT Error: ".mysql_error()); $result = mysql_query("SELECT * FROM table_name WHERE upper($field1) LIKE '%$find1%' AND upper($field2) LIKE '%$find2%' ") or die("SELECT Error: ".mysql_error()); $num_rows = mysql_num_rows($result); echo "There are $num_rows records:<br>"; echo '<center>'; echo "<table border='1' cellpadding='5' width='990'>"; // set table headers echo "<tr><th>Reference</th> <th>First Name</th> <th>Last Name</th> </tr>"; //get images and names in two arrays $name= $row["name"]; $surname= $row["surname"]; $achieve1 = $row["achieve1"]; $achieve2 = $row["achieve2"]; $achieve3 = $row["achieve3"]; $achieve4 = $row["achieve4"]; $achieve5 = $row["achieve5"]; $achieve6 = $row["achieve6"]; $achieve7 = $row["achieve7"]; $achieve8 = $row["achieve8"]; $assoc1 = $row["assoc1"]; $assoc2 = $row["assoc2"]; $assoc3 = $row["assoc3"]; $assoc4 = $row["assoc4"]; $assoc5 = $row["assoc5"]; $edu1sectype = $row["edu1sectype"]; $edu1secinst = $row["edu1secinst"]; $edu1secname = $row["edu1secname"]; $edu1secdate = $row["edu1secdate"]; $edu1secinsttyp = $row["edu1secinsttyp"]; $subject1 = $row["subject1"]; $subject2 = $row["subject2"]; $subject3 = $row["subject3"]; $subject4 = $row["subject4"]; $subject5 = $row["subject5"]; $subject6 = $row["subject6"]; $subject7 = $row["subject7"]; $subject8 = $row["subject8"]; $compsoft1name = $row["compsoft1name"]; $compsoft2name = $row["compsoft2name"]; $compsoft3name = $row["compsoft3name"]; $compsoft4name = $row["compsoft4name"]; $compsoft5name = $row["compsoft5name"]; $compsoft6name = $row["compsoft6name"]; $achievements = array('achieve1', 'achieve2', 'achieve3', 'achieve4', 'achieve5', 'achieve6', 'achieve7', 'achieve8'); $associations = array('assoc1', 'assoc2', 'assoc3', 'assoc4', 'assoc5'); $subjects = array('subject1', 'subject2', 'subject3', 'subject4', 'subject5', 'subject6', 'subject7', 'subject8' ); $compsoft = array('compsoft1name', 'compsoft2name', 'compsoft3name', 'compsoft4name', 'compsoft5name', 'compsoft6name'); while ($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td ALIGN=LEFT>" . $row['id'] . "</td>"; echo "<td ALIGN=LEFT>" . $row['name'] . "</td>"; echo "<td ALIGN=LEFT>" . $row['surname'] . "</td>"; echo "</tr>"; } echo "</table>"; //This counts the number or results - and if there wasn't any it gives them a little message explaining that $anymatches=mysql_num_rows($data); if ($anymatches == 0) { echo "Sorry, but we can not find an entry to match your query"; } } ?> </body> </html>Any assistance will be greatly appreciated as I have been working on this website for the past 4 months which has totalled over 150 pages and this is one of the last pages left to program and it's taken 6 days to get to this search page to this point. Hi Everyone, I am working on implementing a blog comment system using the query below to display comments. My question is what is the best way to do a row count for the id column with this query or do I need to do a second db query to accomplish this? The purpose of this is to echo out the number of comments for that post, before displaying them. Any help is appreciated. Thanks in advance, kaiman <?php // get url variables $post_id = mysql_real_escape_string($_GET['id']); include ("../../scripts/includes/nl2p.inc.php"); // connects to server and selects database include ("../../scripts/includes/dbconnect.inc.php"); // table name $tbl_name3="blog_comments"; // select info from comments database $result3 = mysql_query ("SELECT count(*) FROM $table_name3 WHERE id='$post_id' ORDER BY id DESC LIMIT 1") or trigger_error("A mysql error has occurred!"); if (mysql_num_rows($result3) > 0 ) { while($row = mysql_fetch_array($result3)) { extract($row); // display number of comments // display date $row_date = strtotime($row['date']); putenv("TZ=America/Denver"); echo "<p class=\post\">On ".date('F, jS, Y', $row_date)." "; // display commenter name if($row['url'] == "") { echo $row['name']." wrote:</p>\n"; } else { echo "<a href=\"".$row['url']."\" target=\"_blank\">".$row['name']."</a> wrote:</p>\n"; } // display content $comments = $row['comment']; echo nl2p($comments); } } else { echo "<p class=\"large_spacer\">No Comments</p>"; } ?> hello all! my query, when executed, generates an error i don't understand. i've seen similar entries, but i couldn't find one that related to this particular problem. either that, or i'm a retard and didn't see it. apologies if that's the case. here's the error: --> Database Query Failed: Unknown column 'client_id' in 'where clause' <-- i triple checked the database table and, YUP, the column is correctly named "client_id" and it is an INT column, so it doesn't need to be encapsulated with single quotes. here's the code that makes it happen: <CODE> $clientID = $_SESSION['clientID']; $query = "SELECT * "; $query .= "FROM client_data "; $query .= "WHERE client_id = {$clientID} "; $query .= "LIMIT 1"; $results = mysql_query($query); </CODE> what. the. heck. thanks in advance gang. WR! I have a database lets say punch_log The data in the table looks like: --------------------------------------------------------------------------- |punch_log_id | user_id | punch_id | punch_time | --------------------------------------------------------------------------- | 10010 | 21 | 1 | 2010-11-10 15:04:59| | 10011 | 21 | 2 | 2010-11-10 15:50:05| | 10010 | 21 | 1 | 2010-11-11 15:04:59| | 10011 | 21 | 2 | 2010-11-11 15:50:05| | 10010 | 21 | 1 | 2010-11-12 15:04:59| | 10011 | 21 | 2 | 2010-11-12 15:50:05| | 10010 | 21 | 1 | 2010-11-13 15:04:59| | 10011 | 21 | 2 | 2010-11-13 15:50:05| | 10010 | 21 | 1 | 2010-11-14 15:04:59| | 10011 | 21 | 2 | 2010-11-14 15:50:05| | 10010 | 21 | 1 | 2010-11-14 15:50:59| <-- this is why i need this. | 10011 | 21 | 2 | 2010-11-14 15:55:05| <-- this is why i need this. ---------------------------------------------------------------------------- Im currently using : $kust = $_POST['AKuu']; $kuni3 = $_POST['LKuu']; $valitudtootaja = $_POST['TNimi']; mysql_select_db($database, $con); $query2 = "SELECT *, SUM(punch_id) FROM punch_log WHERE user_id = '".$valitudtootaja."' AND punch_time BETWEEN '$kust' AND '$kuni3' AND punch_id ='1' "; $result2 = mysql_query($query2) or die(mysql_error()); while($row = mysql_fetch_array($result2)){ $X = $row['SUM(punch_id)']; When using the current query im getting Workers days at work = 6 it should be 5 but thats why i need some solution to group dates and then sum them. Does anybody have any ideas? This topic has been moved to MySQL Help. http://www.phpfreaks.com/forums/index.php?topic=349563.0 Hi. Maybe a tricky question? How do I reflect the content of a column from a database table in a roll down select menu in the browser? Let's say that the content of the table column is: Anna Michael These names should be reflected in this select menu like this: <select name="friends"> <option value="Choose a name">Choose a name</option> <option value="Anna">Anna</option> <option value="Michael">Michael</option> So visitors can choose a name, and thereby turn it into a variable, for reuse in the database. Best regards Morris I am trying to create 3 columns of data from a query. I have used this code before for 2 columns but and now wanting to use for three columns.
There are 5 records from a join query. The first two columns display the results, 2 results in the left column and 3 results in the center column. The right column displayw empyt text used in displaying the results, there is no data to populate and it seems to mimic the the number from the center, 3.
Here is the URL to see the problem
http://specialslocat...ignID=1IBu3992p
Here is the Code for the query and the 3 column results.
<?php $UN_URL = $_GET['un']; $mS_MGMTcampaignID_URL = $_GET['mS_MGMTcampaignID']; ?> <table width="944" border="1" align="center" cellpadding="0" cellspacing="0"> <?php $result = mysql_query( "SELECT * FROM manager_Specials_company_mgmt MSCM LEFT JOIN b2c_coupons B2CC ON (MSCM.mS_MGMT_id = B2CC.m_primeID )WHERE mS_MGMTcampaignID = '$mS_MGMTcampaignID_URL' ORDER BY mS_MGMT_m_company ASC" ); $num_results = mysql_num_rows($result); $numb_tb_rows= ceil($num_results/3) ?> <? if ( $numb_tb_rows > 0 ) { ?> <tr > <td colspan="4" valign="top" class="body_text_10_Center"><strong><?=$mS_campaignName_mS?> is proud to sponsor <?=$num_results?> <? if ( $num_results == 1 ) { ?> company:</strong> <? } elseif ( $num_results > 1 ) { ?> companies:</strong> <? } ?> </td> </tr> <tr class="body_text_10_Center"> <!-- Column 1 --> <td width="320" class="body_text_10_Center" valign="top"> <? // column #1 if($num_results>=1) { for($i=0; $i<$numb_tb_rows; $i++) { $row= mysql_fetch_array($result); ?> <?php if(!empty ($row["m_logo"])) { ?> <div align="center"> <img src="http://marketingteammates.com/_ZABP_merchants/_zcnsLogos/gick.php/<?=stripslashes($row["m_logo"])?>?resize(220)" border="0"></a><br /> </div> <?php } else { } ?> <h6><?=stripslashes($row["mS_MGMT_m_company"])?></h6> <?=stripslashes($row["b2c_m_address1"])?> <?=stripslashes($row["b2c_m_address2"])?><br /> <?=stripslashes($row["b2c_m_city"])?> <?=stripslashes($row["b2c_m_state"])?>, <?=stripslashes($row["b2c_m_zip"])?><br /> Phone: <?=stripslashes($row["b2c_m_phone"])?><br /> Fax: <?=stripslashes($row["b2c_m_fax"])?><br /> <a href="mailto:<?=stripslashes($row["b2c_email"])?>"><?=stripslashes($row["b2c_email"])?></a><br /><br /> Hours of Operation: <?=stripslashes($row["m_coupon_title"])?><br /> <?=stripslashes($row["m_coupon_desc"])?><br /><br /> <div align="center"> <img src="../images/print_image.gif" width="29" height="31" /><br /><br /> <hr width="80%" size="1" /> </div><br /><br /> <? // END column #1 } } ?> </td> <!-- Column 2 --> <td width="320" class="body_text_10_Center" valign="top"> <? // column #2 if($num_results>=2) { for($i=($numb_tb_rows); $i<$num_results; $i++) { $row= mysql_fetch_array($result); ?> <?php if(!empty ($row["m_logo"])) { ?> <div align="center"> <img src="http://marketingteammates.com/_ZABP_merchants/_zcnsLogos/gick.php/<?=stripslashes($row["m_logo"])?>?resize(220)" border="0"></a><br /> </div> <?php } else { } ?> <h6><?=stripslashes($row["mS_MGMT_m_company"])?></h6> <?=stripslashes($row["b2c_m_address1"])?> <?=stripslashes($row["b2c_m_address2"])?><br /> <?=stripslashes($row["b2c_m_city"])?> <?=stripslashes($row["b2c_m_state"])?>, <?=stripslashes($row["b2c_m_zip"])?><br /> Phone: <?=stripslashes($row["b2c_m_phone"])?><br /> Fax: <?=stripslashes($row["b2c_m_fax"])?><br /> <a href="mailto:<?=stripslashes($row["b2c_email"])?>"><?=stripslashes($row["b2c_email"])?></a><br /><br /> Hours of Operation: <?=stripslashes($row["m_coupon_title"])?><br /> <?=stripslashes($row["m_coupon_desc"])?><br /><br /> <div align="center"> <img src="../images/print_image.gif" width="29" height="31" /><br /><br /> <hr width="80%" size="1" /> </div><br /><br /> <? // END column #2 } } ?> </td> <!-- Column 3 --> <td width="320" class="body_text_10_Center" valign="top"> <? // column #3 if($num_results>=3) { for($i=($numb_tb_rows); $i<$num_results; $i++) { $row= mysql_fetch_array($result); ?> <?php if(!empty ($row["m_logo"])) { ?> <div align="center"> <img src="http://marketingteammates.com/_ZABP_merchants/_zcnsLogos/gick.php/<?=stripslashes($row["m_logo"])?>?resize(220)" border="0"></a><br /> </div> <?php } else { } ?> <h6><?=stripslashes($row["mS_MGMT_m_company"])?></h6> <?=stripslashes($row["b2c_m_address1"])?> <?=stripslashes($row["b2c_m_address2"])?><br /> <?=stripslashes($row["b2c_m_city"])?> <?=stripslashes($row["b2c_m_state"])?>, <?=stripslashes($row["b2c_m_zip"])?><br /> Phone: <?=stripslashes($row["b2c_m_phone"])?><br /> Fax: <?=stripslashes($row["b2c_m_fax"])?><br /> <a href="mailto:<?=stripslashes($row["b2c_email"])?>"><?=stripslashes($row["b2c_email"])?></a><br /><br /> Hours of Operation: <?=stripslashes($row["m_coupon_title"])?><br /> <?=stripslashes($row["m_coupon_desc"])?><br /><br /> <div align="center"> <img src="../images/print_image.gif" width="29" height="31" /><br /><br /> <hr width="80%" size="1" /> </div><br /><br /> <? // END column #3 } } ?> </td> </tr> <? } else { echo ""; } ?> </table>Thanks for any help in advance. Mike |