-
Posts
24,566 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Regarding CASE statements, yes you can use them in an insert. BUT whether the one you propose is feasible is impossible to say without knowing what you are trying to accomplish. Where do "home_id", "away_id" and "l_team_id" come from in that statement?
-
You shouldn't be putting $userid in the query string. It looks as though the actual query being executed is SELECT COUNT(udid) AS `rank` FROM myTable WHERE score > ( SELECT score from myTable WHERE udid = myLongUdidStringHere Because "myLongUdidStringHere" isn't in quotes, sql treats it as a column name. Try function getUserRanks($udid, $conn) { $sql = "SELECT COUNT(udid) AS `rank` FROM myTable WHERE score > ( SELECT score from myTable WHERE udid = :udid )"; $stmt = $conn->prepare($sql); $stmt->execute( [ ':udid' => $udid ] ); $ranks = $stmt->fetchObject(); return $ranks->rank; }
-
Here's a replacemnt progress_bar function function progress_bar($width, $height, $calories, $target) { $bar = "<svg width='$width' height='$height' view_box='0 0 $width $height'>\n <rect x='0' y='0' width='$width' height='$height' fill='#CCC' />\n"; // calc width of bar for calories already burned if ($target==0) { $target = 1; } if ($calories > 0) { $cal_width = $calories * $width / $target; $bar .= "<rect x='0' y='0' width='$cal_width' height='$height' stroke='#ccc' fill='#3C3' />\n"; } else { $bar .= "<text x='5' y='16' font-size='9pt'>No data for user</text>\n"; } $bar .= "</svg>\n"; return $bar; }
-
mysql construct to apply multiple fields to a single like
Barand replied to fatkatie's topic in MySQL Help
If you are doing wildcard searches, using LIKE, you are stuck with the long version I'm afraid. Alternative is FULLTEXT index search. (Moving to MySql forum) -
Prefix the table name with the database name $sql = "SELECT ID, FirstName, Last Name, Email, Mobile, PromoCode FROM database1.table1 WHERE (Email LIKE ?) OR (Mobile LIKE ?) OR (PromoCode LIKE ?) UNION SELECT ID, FirstName, Last Name, Email, Mobile, PromoCode FROM database2.table1 WHERE (Email LIKE ?) OR (Mobile LIKE ?) OR (PromoCode LIKE ?) "; Databases must be on the same server.
-
I created some test data. Try this session_start(); $_SESSION['userid'] = 2; // your's is probably set already $con=mysqli_connect($host,$username,$password,$dbname); if (mysqli_connect_errno()) { die("Failed to connect to MySQL: " . mysqli_connect_error()); } $bar_length = 400; $bar_height = 20; $sql = "SELECT SUM(caloriesburned) as calories_burned , CASE g.weightunit WHEN 'lbs' THEN 3500 ELSE 7700 END * weightlost as target_calories FROM goal g INNER JOIN tracklog t USING (userid) WHERE userid = ?"; $stmt = $con->prepare($sql); $stmt->bind_param('i', $_SESSION['userid']); $stmt->execute(); $stmt->bind_result($calories, $target); $res = $stmt->fetch(); function progress_bar($width, $height, $calories, $target) { $bar = "<svg width='$width' height='$height' view_box='0 0 $width $height'>\n <rect x='0' y='0' width='$width' height='$height' fill='#CCC' />\n"; // calc width of bar for calories already burned $cal_width = $calories * $width / $target; $bar .= "<rect x='0' y='0' width='$cal_width' height='$height' stroke='#ccc' fill='#3C3' />\n"; $bar .= "</svg>\n"; return $bar; } ?> <!DOCTYPE html> <html> <head> <title>Sample Progress Bar</title> </head> <body> Calories burned to date : <?=$calories?><br> Target calories : <?=$target?><br> <?= progress_bar($bar_length, $bar_height, $calories, $target) ?> </body> </html>
-
Keep it simple $inside = []; while($row = $result->fetch_array()) { $hour = date('G', strtotime($row['time'])); $inside[$hour] = $row['temp']; }
-
My code assumed a PDO connection. I have changed it to mysqli using a prepared statement. $con=mysqli_connect("localhost","root","password","registration"); if (mysqli_connect_errno()) { die("Failed to connect to MySQL: " . mysqli_connect_error()); } $bar_length = 400; $bar_height = 20; $sql = "SELECT SUM(caloriesburned) as calories_burned , CASE g.weightunit WHEN 'lbs' THEN 3500 ELSE 7700 END * weightlost as target_calories FROM goal g INNER JOIN tracklog t USING (userid) WHERE userid = ?"; $stmt = $con->prepare($sql); $stmt->bind_param('i', $_SESSION[userid]); $stmt->execute(); $stmt->bind_result($calories, $target); $res = $stmt->fetch(); echo progress_bar($bar_length, $bar_height, $calories, $target);
-
Something like this $userid = somevalue; // however you decide which user $bar_length = 400; $bar_height = 20; $sql = "SELECT SUM(caloriesburned) as calories_burned , CASE g.weightunit WHEN 'lbs' THEN 3500 ELSE 7700 END * weightlost as target_calories FROM goal g INNER JOIN tracklog t USING (userid) WHERE userid = ?"; $stmt = $pdo->prepare($sql); $stmt->execute( [$userid] ); $res = $stmt->fetch(); echo progress_bar($bar_length, $bar_height, $res['calories_burned'], $res['target_calories']);
-
Given the target calories and calories to date, then this will give a simple progress bar $bar_length = 400; $bar_height = 20; $target = 1000; // target calories need to burned off $calories = 650; // calculated total burned off so far echo progress_bar($bar_length, $bar_height, $calories, $target); // output progress bar function progress_bar($width, $height, $calories, $target) { $bar = "<svg width='$width' height='$height' view_box='0 0 $width $height'>\n <rect x='0' y='0' width='$width' height='$height' fill='#CCC' />\n"; // calc width of bar for calories already burned $cal_width = $calories * $width / $target; $bar .= "<rect x='0' y='0' width='$cal_width' height='$height' stroke='#ccc' fill='#3C3' />\n"; $bar .= "</svg>\n"; return $bar; }
-
Your final ")" is in the wrong place. It needs to be in the position marked below echo $this->date->add($this->set_interval($del_tim_holder_1[count($del_tim_holder_1)] * 7))->format("d.m.Y"); ^
-
try foreach (range('A','Z') as $alpha) { echo "<li data-text='$alpha'>$alpha</li>\n"; }
-
$date = '16.02.2017'; $days = 21; // create dateTime object $dt = new DateTime($date); // create a dateInterval $di = new DateInterval("P{$days}D"); // add interval to the date echo $dt->add($di)->format('d.m.Y'); //--> 09.03.2017
-
Do you have php error reporting switched on? You certainly aren't checking if there was a mysql error.
-
Looks like the "space" is some other whitespace character. What does SELECT HEX(price) as hexprice; give you?
-
That gives mysql> SELECT price -> , CAST(REPLACE(price,' ','') AS signed) as number -> FROM test -> ORDER BY CAST(REPLACE(price,' ','') AS signed); +--------+--------+ | price | number | +--------+--------+ | 8 082 | 8082 | | 8 791 | 8791 | | 8 791 | 8791 | | 9 374 | 9374 | | 9 823 | 9823 | | 10 186 | 10186 | | 12 257 | 12257 | | 12 698 | 12698 | | 13 959 | 13959 | | 14 463 | 14463 | | 14 920 | 14920 | | 15 132 | 15132 | | 16 023 | 16023 | | 16 117 | 16117 | | 16 606 | 16606 | +--------+--------+ So, except for "table" being a reserved word and not to be used as a table name, I see no problem. edit: But why are you storing prices as text? Why not just use DECIMAL.