Asked  7 Months ago    Answers:  5   Viewed   29 times

This is my code but it dosn't work:

$param = "%{$_POST['user']}%";
$stmt = $db->prepare("SELECT id,Username FROM users WHERE Username LIKE ?");
$stmt->bind_param("s", $param);

This code it doesn't seem to work. I have searched it a lot. Also it may return more than 1 row. So how can I get all the results even if it returns more than 1 row?



Here's how you properly fetch the result

$param = "%{$_POST['user']}%";
$stmt = $db->prepare("SELECT id,username FROM users WHERE username LIKE ?");
$stmt->bind_param("s", $param);

while ($stmt->fetch()) {
  echo "Id: {$id}, Username: {$username}";

or you can also do:

$param = "%{$_POST['user']}%";
$stmt = $db->prepare("SELECT id, username FROM users WHERE username LIKE ?");
$stmt->bind_param("s", $param);

$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
    echo "Id: {$row['id']}, Username: {$row['username']}";

I hope you realise I got the answer directly from the manual here and here, which is where you should've gone first.

Tuesday, June 1, 2021
answered 7 Months ago

SQLite2 internally stores every value as a string, regardless of its type.
Upgrading to SQLite3 will certainly shrink the database size since numbers and BLOBS get stored in their native formats, which could make things run faster.
Another big advantage in my opinion is that recent versions of sqlite, (starting from 3.6.23) support foreign keys.

Since you were using PHP, I would suggest that you look into PDO. It could prove helpful in case you need to change the DBMS for the application

Saturday, May 29, 2021
answered 7 Months ago

An index cannot safely be used in this case. A naive implementation would transform this:

... WHERE word LIKE 'search_string%'


... WHERE word >= 'search_string' AND word < 'search_strinh'

by incrementing the last character of the search string. The greater-than and less-than operators can use an index, where LIKE cannot.

Unfortunately, that won't work in the general case. The LIKE operator is case-insensitive, which means that 'a' LIKE 'A' is true. The above transformation would break any search string with capitalized letters.

In some cases, however, you know that case sensitivity is irrelevant for a particular column, and the above transformation is safe. In this case, you have two options.

  1. Use the NOCASE collating sequence on the index that covers this particular field.
  2. Change the behavior of the LIKE operator program-wide by running PRAGMA case_sensitive_like = ON;

Either of these behaviors will enable SQLite to transparently do the above transformation for you; you just keep using LIKE as always, and SQLite will rewrite the underlying query to use the index.

You can read more about "The LIKE Optimization" on the SQLite Query Optimizer Overview page.

Monday, September 6, 2021
answered 3 Months ago

You need to call setHasOptionsMenu(true) on your fragment before it can participate in the action bar.

Tuesday, September 14, 2021
answered 3 Months ago

enter image description here

You can draw an arrowhead at the end of line segment [p0,p1] like this:

  • calculate the angle from p0 to p1 using Math.atan2.

  • Each side of the arrowhead starts at p1, so calculate the 2 arrow endpoints using trigonometry.

  • draw the [p0,p1] line segment and the 2 arrowhead line segments.

Here's example code and a Demo:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

var p0={x:50,y:100};
var p1={x:250,y:50};


function drawLineWithArrowhead(p0,p1,headLength){

  // constants (could be declared as globals outside this function)
  var PI=Math.PI;
  var degreesInRadians225=225*PI/180;
  var degreesInRadians135=135*PI/180;

  // calc the angle of the line
  var dx=p1.x-p0.x;
  var dy=p1.y-p0.y;
  var angle=Math.atan2(dy,dx);

  // calc arrowhead points
  var x225=p1.x+headLength*Math.cos(angle+degreesInRadians225);
  var y225=p1.y+headLength*Math.sin(angle+degreesInRadians225);
  var x135=p1.x+headLength*Math.cos(angle+degreesInRadians135);
  var y135=p1.y+headLength*Math.sin(angle+degreesInRadians135);

  // draw line plus arrowhead
  // draw the line from p0 to p1
  // draw partial arrowhead at 225 degrees
  // draw partial arrowhead at 135 degrees
  // stroke the line and arrowhead
body{ background-color: ivory; }
canvas{border:1px solid red;}
<canvas id="canvas" width=300 height=300></canvas>
Saturday, November 13, 2021
answered 3 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :