Case insensitive query in PHP does not work - php

I have table: id, name(text, utf8_general_ci).
Simple data into table: 1, LOlKek_228666
Trying PHP:
$link = mysqli_connect($host, $user, $password, $database);
mysqli_set_charset('utf8',$link);
$q = mysqli_query($link, "SELECT * FROM `commands` WHERE LOWER(`name`) = LOWER('$nick') ORDER BY id DESC");
or simple:
$q = mysqli_query($link, "SELECT * FROM `commands` WHERE `name` = '$nick' ORDER BY id DESC");
It can't find any if use LolKek_228666 (to find LOlKek_228666) in query($nick).
But phpmyadmin find it. How to fix that?

As you are using a case-insensitive collation, you might just go with using the "LIKE" operator and drop the LOWER() functions.
WHERE LOWER(name) will just lowercase the fieldname, not the data value.

Related

PHP uniqid repeating on every iteration of while loop

I'm trying to populate a column in a mysql database with a unique identifier using a while loop, but uniqid is repeating the same output over and over again.
this is my code:
$dblink = mysqli_connect($host,$dbu,$dbp);
$dblink->set_charset("utf8");
$seldb = mysqli_select_db($dblink, $db);
$dblink = new mysqli($host, $dbu, $dbp, $db);
$result = $dblink->query(" SELECT * FROM `mytable` ");
while ($row = $result->fetch_assoc()) {
$uniquecode = uniqid();
$sql = mysqli_query($dblink,"UPDATE `mytable` SET `code`='$uniquecode' ");
}
What am I doing wrong?
You're updating every record in each iteration because your UPDATE is lacking a WHERE clause.
Normally you'd key this like:
UPDATE ... SET code=? WHERE id=?
Where that locks it to just the row matching id or whatever your ID column is called.
Note: You should avoid SELECT * unless you actually need all those columns. Here you don't, you just need ID, so just select that.

Trying to output the richest user in my database through php

I know this is a bit of a rookie question but I'm trying to output the richest user's name on my website.
So my table is called Users
I have Column 1 ('Name') containing the names of all the users, and Column 2 ('Bank') containing their account balance.
I would like to find the richest user and output them on my website.
This is what I've got so far.
while ($row2 = mysqli_fetch_array($sqldataGang, MYSQLI_ASSOC)) {
$sqlgetGang = 'select name from gangs where bank = (select max(bank) from gangs) order by bank;';
$sqldataGang = mysqli_query($dbcon, $sqlgetGang) or die('Connection could not be established');
$welthiestGang = $row2['name'];
}
I know that there is a connection to the database as I have other statistics from other tables working... I have no idea why this isn't working... Thanks for the help in advance :)
$sql = 'SELECT name FROM gangs ORDER BY bank DESC LIMIT 1';
That should do it.
You need to fetch a row from your $sqldataGang query object.
Add a line something like this to your program to fetch the result right after your call to mysqli_query()
$row2 = mysqli_fetch_array($sqldataGang, MYSQLI_ASSOC);
But, also, beware. A wise programmer always checks queries for errors. You can see how to do that in the examples here.
Use this query
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT name,bank FROM Users order by bank desc limit 1";
$result = mysqli_query($conn, $sql);
This will give you the richest person in your website

Display an int value from SQL database using PHP

I'm busy with a school project where I need to register users. I created the database and added the tables and can add users. What I just can't get right is to display the next available user id in the table.
I'm using php to retrieve the highest value but when I use echo the variable won't show. There is no error, there is no output at all, just the rest of the page.
Here is the code:
<?php
$db = mysqli_connect('localhost', 'root', '', 'design');
$query = "SELECT MAX(userid) AS userid FROM users" or
die(mysql_error());
$highest_id = mysqli_query($db, $query);
echo $highest_id;
?>
The code successfully connects to the database, the column is called userid, it contains int values and there are other columns as well.
All other code in the script runs perfectly, it's just this part that I can't get to work.
I have spent the last two days reading and searching for answers and I am at my wits end. Any help would be appreciated.
Thank you.
could be your table is User and not Userid
$query = "SELECT MAX(userid) AS userid FROM users"
Anyway for fetching you should use eg:
$result = mysqli_query($db, $query);
$row=mysqli_fetch_array($result,MYSQLI_NUM);
echo $row[0];
The mysqli_query returns a general object that contains the results array. You have to use the mysqli_fetch_row.
<?php
$db = mysqli_connect('localhost', 'root', '', 'design');
$query = "SELECT MAX(userid) AS userid FROM userid" or die(mysql_error());
$highest_id_query = mysqli_query($db, $query);
var_dump($highest_id_query); // so you could check the object attributes
//loop results from query
while($row=mysqli_fetch_row($highest_id_query)){
$highest_id = $row['userid'];
echo $highest_id;
}
?>
You could also use the sql statement: SELECT COUNT(*) FROM userid
Be sure to name your tables correctly! SELECT COUNT(*) FROM users

sql server ISNULL not working for my query

This is my second question about this problem. I would like get sum of column roll_sum
script:
($sum_number + (SELECT SUM(roll_sum) FROM table_name))
not work because collumn ROLL_SUM is NULL. But if try use replacement:
($sum_number + (SELECT SUM(ISNULL(roll_sum, 0)) FROM table_name))
not work aswell. But second script should replace NULL to 0?
Swap ISNULL() with SUM():
SELECT isnull(SUM(roll_sum), 0) FROM table_name;
I think there is no problem in your sql query but, you are calling sql query in php statement directly.
The below is sample code. please refer to it.
$db = mysql_connect("hostname", "username", "password");
mysql_select_db("dbname", $db) or die("connection failed");
$query = mysql_query("select sum(roll_sum) as sum from table_name", $db);
$query_row = mysql_fetch_array($query);
// to do something you want
$value = $sum_number + $query_row["sum"];
mysql_close($db);

MySQL Select statement

I have 2 values that I'm suppling my script - I want to search for any one of those datas. How do I write my query like this:
SELECT * FROM table WHERE id = '".$id."' or "name='".$name."';
my problem is escaping the quotes in the query.
Any help will be appreciated.
There are a few ways to do it, a lot of them frowned on but generally I would stick to using MySQLi and using the
mysqli_real_escape_string($id)
function or in OOP
$mysqli = new mysqli('host', 'user', 'pass', 'database');
$id = $mysqli -> real_escape_string($id);
$name = $mysqli -> real_escape_string($name);
$results = $mysqli -> query("SELECT * FROM table WHERE id = '{$id}' or "name='{$name}'");
You may use curly brackets to avoid confusion with escaping characters as follows:
$query = "SELECT * FROM table WHERE id = '{$id}' or name = '{$name}' ";
You may also consider using wildcards such as %$letter% to search for word anywhere in the name field as:
$query = "SELECT * FROM table WHERE id = '{$id}' or name LIKE '%{$name}%' ";
SUGGESTTION:
You should always use id fields as integer for better performance.
Use this fancy function, mayhaps? The examples have what you're looking for.
You've got an extra quote; if you want to stick with your original code (not recommended), try something like this:
$query = "SELECT * FROM table WHERE id = '".$id."' or name='".$name."'";
But really you should be using parameterised queries so that you avoid possible SQL injection security issues!
Write it as:
$name = mysql_real_escape_string($name);
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM table WHERE id = '$id' or name= '$name' ";
Because you started with double quotes the single quotes are part of the query and the $vars are expanded.

Categories