PHP - Problems With Pdo And Order By
Is it possible to use the PDO with a statement that has ORDER BY tablename.
The program works fine but when I added the ORDER BY clause it outputted an error. Is it maybe because I set the fetch mode to PDO::FETCH_OBJ? Or is that irrelevant? This is the error that outputs: Code: [Select] Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BY task_position' at line 1' in /tasks.php(38): PDO->query('SELECT * FROM t...') #1 tasks.php(57): tasks->grab_tasks('2') #2 {main} thrown in tasks.php on line 38 It normally worked without the ORDER BY. Here is my MYSQL call: $grabber = $this->pdo->query('SELECT * FROM tasks WHERE user_id = ' . $userid . "ORDER BY 'task_position'"); $grabber->setFetchMode(PDO::FETCH_OBJ); Similar Tutorials
Hi everyone. I'm very new into self learning programming. Presently I'm trying to develop a simple basic Robot that would only Place a Market Order every seconds and it will cancel the Order every followed seconds. Using the following library: It would place Trade Order at a ( Price = ex.com api * Binance api Aggregate Trades Price) I have already wrote the api to call for xe.com exchange rate with php <?php $auth = base64_encode("username:password"); $context = stream_context_create([ "http" => [ "header" => "Authorization: Basic $auth" ] ]); $homepage = file_get_contents("https://xecdapi.xe.com/v1/convert_from?to=NGN&amount=1.195", false, $context ); $json = json_decode($homepage, TRUE); foreach ($json as $k=>$to){ echo $k; // etc }; ?> And also for the Binance Aggregate Price in JavaScript
<script> var burl = "https://api3.binance.com"; var query = '/api/v3/aggTrades'; query += '?symbol=BTCUSDT'; var url = burl + query; var ourRequest = new XMLHttpRequest(); ourRequest.open('GET',url,true); ourRequest.onload = function(){ console.log(ourRequest.responseText); } ourRequest.send(); </script>
My problem is how to handle these two api responds and also the functions to use them to place a trade and cancel it. I've got. I got this code from a previous thread: Code: [Select] mysql_query("SET @rows = 0;"); $res = mysql_query("SELECT @rows:=@rows+1 AS view_rank,COUNT(id) AS views, credit_members_id FROM vtp_tracking GROUP BY credit_members_id ORDER BY views DESC"); $n = array(1 => 'st', 2 => 'nd', 3 => 'rd'); while($row = mysql_fetch_row($res)) { if ( $row[2] != $members_id ) continue; if ( substr($row[0], -1) < 4 ) { $row[0] .= $n[$row[0]]; } else { $row[0] .= 'th'; } echo ' You are in ' . $row[0] . ' place with ' . number_format($row[1]) . ' views.'; break; } Everything seems ok except it orders by the "credit_members_id" and not "views" as entered. Can someone explain why, and how to fix this? Hello, I have classes in a database with no set UNIX date, just the day like Wednesday and in two other columns the start and end dates. I want to be able to order by the day first and then by end_time but php orders the day column by spelling and not the day it holds in chronological order. Is there anyway to change the query to order the day column as a date? See the query below? Code: [Select] $query = "SELECT * FROM zumba_timetable WHERE end_time>'$current_time' ORDER BY day, end_time ASC LIMIT 0,1"; Currently i have a query which at the end has this Code: [Select] order by (value/counter) desc"); which works fine, however i was wondering, if in the case that 2 rows have the same, is it possible to set a second value to order by? Hi there, just registered and in need of help, this looks a good place to start! I'm a php beginner so please bear with me I have a mysql database which holds 3 pieces of info id: match: 1: I'm trying to sort the results using ORDER BY match ASC but for some reason it's just not for having it... Can't work out if it's an error with my php code or my table. I can order by "ID" no problem but I when I try ordering alphabetically by "match" it won't. my code is: Code: [Select] mysql_connect("***.***.***") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); $query = "SELECT * FROM employees WHERE flag = 'tv' ORDER BY match ASC"; $result = mysql_query($query) or die ("Query failed"); $numrows = (mysql_num_rows ($result)); // loop to create rows if($numrows >0){ echo "<table width = 100% border = '0' cellspacing = '2' cellpadding = '0' >"; // loop to create columns $position = 1; while ($row = mysql_fetch_array($result)){ if($position == 1){echo "<tr>";} echo " <td align = 'center'><a href=\"http://www.website.eu/sport-stream/1/{$row['id']}.html\" target=_blank>{$row['match']} <br> <img src=\"{$row['8']}\" width=\"100\" height=\"70\" /> </a> </td> "; if($position == 4){echo "</tr> "; $position = 1;}else{ $position++;} }//while $end = ""; if($position != 1){ for($z=(4-$position); $z>0 ; $z--){ $end .= "<td></td>"; } $end .= "</tr>"; } echo $end."</table> "; }//if And here's a cap of my database: any help for a php n00b would be greatly appreciated! I need to make 0, which displays as POA appear at the ned of the list not at the begining, I currently just use order by price asc and 0 is at the beginning, i need to make 0 at the end class curl2{ private $curl_init; private $CURLOPT_URL; public function connect(){ $this->curl_init = curl_init(); } public function debug(){ curl_setopt($this->curl_init, CURLOPT_VERBOSE, TRUE); $fp = fopen("curl2.txt", "w"); curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE); } public function setUrl($url = null){ $this->CURLOPT_URL = $url; curl_setopt($this->curl_init, CURLOPT_URL, $this->CURLOPT_URL); } public function execute(){ $out = curl_exec($this->curl_init); curl_close($this->curl_init); return $out; } } $curl2 = new curl2; $curl2->connect(); $curl2->setUrl("http://www.linuxformat.co.uk"); $curl2->debug(); echo $curl2->execute(); It display a blank page like attachment result1.jpg, but if I move the $fp = fopen("curl2.txt", "w"); curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE); from function debug() and join it with function execute() like this: public function execute(){ $fp = fopen("curl2.txt", "w"); curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE); $out = curl_exec($this->curl_init); curl_close($this->curl_init); return $out; } it return me Linuxformat content ( expected result ) like result2.jpg below is the working code : class curl2{ private $curl_init; private $CURLOPT_URL; public function connect(){ $this->curl_init = curl_init(); } public function debug(){ curl_setopt($this->curl_init, CURLOPT_VERBOSE, TRUE); } public function setUrl($url = null){ $this->CURLOPT_URL = $url; curl_setopt($this->curl_init, CURLOPT_URL, $this->CURLOPT_URL); } public function execute(){ $fp = fopen("curl2.txt", "w"); curl_setopt($this->curl_init, CURLOPT_STDERR, $fp); curl_setopt($this->curl_init, CURLOPT_RETURNTRANSFER, TRUE); $out = curl_exec($this->curl_init); curl_close($this->curl_init); return $out; } } $curl2 = new curl2; $curl2->connect(); $curl2->setUrl("http://www.linuxformat.co.uk"); $curl2->debug(); echo $curl2->execute(); Why I couldn't split "CURLOPT_STDERR, CURLOPT_RETURNTRANSFER" with "curl_exec" I am attempting to order the results of a query using the get value but they wont order. here is the form: echo "<form name=\"order_form\" action=\"{$site_root}/index.php\" method=\"GET\">"; echo "<input type=\"hidden\" value=\"{$forum_id}\" name=\"forum\" />"; echo "<select name=\"order_by\" style=\"margin-left:5px;\">"; echo "<option value=\"asc\" name=\"order_option\" />Order By: Ascending</option>"; echo "<option value=\"desc\" name=\"order_option\" />Order By: Descending</option>"; echo "</select>"; echo "<select name=\"sort_by\" style=\"margin-left:5px;\">"; echo "<option value=\"topic_name\" name=\"sort_option\" />Sort By: Topic Name</option>"; echo "<option value=\"topic_poster\" name=\"sort_option\" />Sort By: Topic Author</option>"; echo "<option value=\"topic_time_posted\" name=\"sort_option\" />Sort By: Time Posted</option>"; echo "<option value=\"topic_views\" name=\"sort_option\" />Sort By: Topic Views</option>"; echo "<option value=\"topic_replies\" name=\"sort_option\" />Sort By: Topic Replies</option>"; echo "<option value=\"topic_last_poster\" name=\"sort_option\" />Sort By: Last Poster</option>"; echo "<option value=\"topic_last_post_time\" name=\"sort_option\" />Sort By: Last Post Time</option>"; echo "</select>"; echo "<input type=\"submit\" value=\"Order\" />"; echo "</form>"; here is the query: $topic_info_query = $db->query("SELECT f.forum_id, f.forum_name, m.user_id, m.user_username, m.user_group, t.thread_topic_id, t.topic_name, t.topic_poster, t.topic_time_posted, t.topic_views, t.topic_replies, t.topic_last_poster, t.topic_last_post_time, t.topic_locked, t.topic_sticky, t.topic_edited, t.topic_last_poster_id, t.topic_last_poster_group, t.topic_icon FROM ".DB_PREFIX."topics as t LEFT JOIN ".DB_PREFIX."members as m ON t.topic_poster = m.user_username LEFT JOIN ".DB_PREFIX."forums as f ON t.forum_id = f.forum_id WHERE t.forum_id = '$forum_id' '".$sort_by . ' ' . $order_by."'") and here is where $sort_by and $order_by are defined: if (isset($_GET['order_by'])) { $order_by = mysql_real_escape_string($_GET['order_by']); $order_by = strtoupper($order_by); } if (isset($_GET['sort_by'])) { $sort_by = 'ORDER BY t.'.mysql_real_escape_string($_GET['sort_by']); } when i echo '".$sort_by . ' ' . $order_by."' which is how it appears in the query i get: t.topic_last_post_time DESC which is exactly right. But the results are not being sorted. The echoed variables change as does the url but no sorting happens. any ideas? Script:
<?php $tqs_admin_flag = "SELECT * FROM `flags`"; $tqr_admin_flag = mysqli_query($dbc, $tqs_admin_flag) or die(mysqli_error($dbc)); while($row_admin_flag = mysqli_fetch_array($tqr_admin_flag)){ //print_r($row_admin_flag); $tqs_admin_flag_thread = "SELECT * FROM `thread` WHERE `id` = '" . $row_admin_flag['thread_id'] . "' ORDER BY `date_created` DESC"; $tqr_admin_flag_thread = mysqli_query($dbc, $tqs_admin_flag_thread) or die(mysqli_error($dbc)); while($row = mysqli_fetch_assoc($tqr_admin_flag_thread)){ include($_SERVER['DOCUMENT_ROOT'] . "/gallerysite/admin_flag_thread.php"); } } ?>This in comparison works: $tqs_admin_flag_thread = "SELECT * FROM `thread` ORDER BY `date_created` DESC";I would appreciate suggestions for which reasons the above script (the way it is) may not work. With the above script the querying and printing on screen does happen, though the "ordering by DESC" does not happen. Edited by glassfish, 24 October 2014 - 11:38 AM. By not using the order by function in SQL. Like I have $match_1 and $segment_1. They are two seperate tables so how would I order them like the ORDER BY in sql. Is there a way to do that? I'm just trying to see if the order of my logic is correct or if there's something I can do differently. I'm also curious to know at what point I should get the 2 passwords that exists in the database for the user. These are two different passwords. Code: [Select] function login_submit() { $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean'); $this->form_validation->set_rules('remember', 'Remember me', 'integer'); $user_id = $this->users->get_user_id_by_username($this->input->post('username')); if ($user_id !== 0) { if ($this->kow_auth->is_max_login_attempts_exceeded($user_id)) { echo json_encode(array('error' => 'yes', 'message' => 'Your account is currently locked, we appologize for the inconvienence. You must wait 10 minutes before you can login again!')); } else { $user_status = $this->users->get_user_status($user_id); if ($user_status == 1) { echo json_encode(array('error' => 'yes', 'message' => 'Sorry you must verify your account before logging in!')); } elseif ($user_status == 3) { echo json_encode(array('error' => 'yes', 'message' => 'Your account has been suspended!')); } elseif ($user_status == 4) { echo json_encode(array('error' => 'yes', 'message' => 'Your account is currently banned!')); } elseif ($user_status == 5) { echo json_encode(array('error' => 'yes', 'message' => 'Your account has been deleted!')); } else { if ($this->form_validation->run()) { if ($this->kow_auth->login($this->form_validation->set_value('username'), $this->form_validation->set_value('password'), $this->form_validation->set_value('remember'))) { redirect(''); } else { echo json_encode(array('error' => 'yes', 'message' => 'Incorrect username and password combination!')); } } } } } else { echo json_encode(array('error' => 'yes', 'message' => 'Incorrect username and password combination!')); } } Evening everyone I wonder if you can help me with the below code <table width="823" cellspacing="0" id="mytable" summary="User Transactions Summary"> <caption>Transaction Summary</caption> <tr> <th width="136" class="nobg" scope="col" abbr="Merchant Name">Merchant Name</th> <th width="154" scope="col" abbr="Order Value">Order Value</th> <th width="145" scope="col" abbr="Comm">Cash Back</th> <th width="278" scope="col" abbr="Status">Status</th> </tr> <?php $result = mysql_query("SELECT * FROM transactions WHERE u_id = '$u_id' ORDER BY order_date DESC LIMIT 10 "); echo mysql_error() ; while($row = mysql_fetch_assoc($result)){ $m_name = $row['m_name']; $orderval = $row['order_val']; $status = $row['order_status']; $m_affid = $row['m_affid']; $query = mysql_query(" SELECT percent FROM merchants WHERE m_affid = '$m_affid' LIMIT 1"); $num = mysql_num_rows($query); $i=0; while ($i < $num) { $per=mysql_result($query,$i,"percent"); $i++; } $com1 = $orderval / 100; $com2 = $com1 * $per; $u_comm = round($com2,2); ?> <tr> <th scope="row" abbr="<?php echo $m_name;?>" class="spec"><?php echo $m_name;?></th> <td class="alt">£<?php echo $orderval ;?></td> <td class="alt">£<?php echo $u_comm ;?></td> <td class="alt"><?php echo $status ;?></td> </tr> <?php }?> </table> Basically it is meant to simply show fields from a database in a table. If there is only 1 entry for a u_id it works fine, however if their is more than one then the $u_comm value shows the same for all results, which is for the last result/row found. Hope this makes sense! I want to get the top 5 rows with common keywords how can i do this? its like this Code: [Select] $query = "SELECT adder,min(title)FROM shsh group by adder order by mysql_num_rows($q)"; Really need a hand from someone mysql query is pretty basic. I have been ask to add a random order to this query. $query = "SELECT rd.user_id FROM {$regions_data_table} rd WHERE rd.field_id = {$regions_field_id} AND rd.value LIKE %s AND rd.user_id NOT IN (SELECT um.user_id FROM {$wpdb->base_prefix}usermeta um WHERE ((um.meta_key='wp_ul_disabled' AND um.meta_value=1) OR (um.meta_key='wp_ul_inactive' AND um.meta_value=1)) GROUP BY um.user_id)"; I have tried so many options but cannot seem to get it to work i am trying to insert order by rand(). Any help please? Hi, I've been trying to re-order an array and I can't seem to even do it let alone neatly. I've ran out of time and would appreciate any help! $array = array( '0' => array( 'filepath' => 'files/image_site1.png', 'title' => 'Website', ), '1' => array( 'filepath' => 'files/image_id.png', 'title' => 'Identitity', ), '2' => array( 'filepath' => 'files/image_site2.png', 'title' => 'Website', ) ) I'm looking to re-order the arrays within the parent array depending on whether the 'filepath's contain a sub string of '_id' or '_site'. I've been using: $type = '_site'; foreach ($array as $item){ if (preg_match("/$type/i", $item['filename'])){ //add this $item to the top of array, somehow... } } So with $type set to '_site', I'd like to re-arrange the array so both arrays containing _site1.png and _site2.png appear at the top of the parent array. I.e: $array('0' = array(...), '2' => array(...), '1' => array(...)); I hope I've explained it well enough. Thanks, I teach 6 different class groups, named "A" to "F", respectively. Hi All, I have a 'Newspaper' in a game I'm coding. My plan was to have an edition every week and on average 15 articles a week. I would set this out like so. . Code: [Select] <? $select_paper=mysql_query("SELECT * FROM paper WHERE id=1"); while($the=mysql_fetch_object($select_paper)){ ?> <table width=100% class=table> <tr> <td> <td width=50% style=border: none; background-color: transparent; valign=top> <table border=1 cellpadding=0 cellspacing=0 bordercolor=#000000 class='main' align=center> <tr> <td width=500 class='tableheading'><?php echo "$the->title"; ?><center> </center></td> </tr> <tr > <td class="profilerow" align=left><center><?php echo "$the->news"; ?> </td> </tr> <tr > <td class="subtableheader" align=left><center><?php echo "Article By $the->by - $the->date"; ?> </td> </tr> </table> <br> </td> <? } ?> With that repeated for 15 articles per edition. BUT! To save time, space and complication, I would rather do it using something like this. . Code: [Select] $select = mysql_query("SELECT * FROM paper WHERE edition=1 ORDER by id ASC"); $num = mysql_num_rows($select); Could someone tell me how to intergrate this with the layout tables as show in code #1? Thanks! by the order in which they were entered?
like i dont want to order by a specific row in my table, i want to list the data by which it was entered.
maybe i can add a row that enters column #s somehow??
I'm working on a simple ad server and would like to display the ads in order (not random). I'm fairly certain I can do something like this: Code: [Select] // Get the last ad id, or becomes 0 if not available $lastid = ($_GET['lastid'] == '') ? '0' : $_GET['lastid']; // Get the next ad in order, or get the first one available mysql_query("SELECT adcode FROM ads WHERE zone='$zoneid' AND id > $lastid ORDER BY id ASC LIMIT 1"); The above code would get the first ad or the next ad in order.. How do I go back to the beginning when I run out of ads to show? |