Codeigniter database retrieving both in uppercase, lowercase and mixed case - mysql

my code
// My code:
$this->db->select('*')
->from('tbl_login')
->where(['username'=>$this->input->post('username'),'password'=>$this->input->post('password')]);
$result = $this->db->get()->result();
if($result)
var_dump($result)
else
echo "not fount";
Doesn't matter what ever I input 'admin1' or 'ADMIN1' & 'password1' or 'PASSWORD1'
it retrieves data from the tables.
But what I actually want is data will be retrieve when case is matched correctly.
Could anyone help???...

Try this one
$query = $this->db->select("*")->from("tbl_login")->where("BINARY username= '".$this->input->post('username')."' and password = '".$this->input->post('password')."'");
$result = $this->db->get()->result();
if($result)
var_dump($result)
else
echo "not found";
Note: Basically use BINARY before your where conditions in Query.
Hope this will help!!

Related

Parse json object from table via php connected to phpmyadmin

I'm using a json-parser in Xcode to fetch a table from phpmyadmin. The parser gets (or should get) the json-formated document via a php-file uploaded on my ftp-server. The file is successfully parsed but it doesn't recognize any objects. I think this is because there are multiple arrays in the json-document.
When there's only one entry the document looks like this:
[{"id":"1","Name":"Eric","Message":"first from web"}]
but when i add an entry it looks like this:
[{"id":"1","Name":"Eric","Message":"first from web"}]
[{"id":"1","Name":"Eric","Message":"first from web"},{"id":"6","Name":"Claes","Message":"Hurrburr"}]
As you can see the old array (containing only the single entry) is still there in the second array with both entries.
I suspect the problem is that the old arrays are still there when i update the database because when i tried parsing the json document with only one entry (only one array) it worked.
So my question is first if thereĀ“s something i missed in my code, or if you know why the old arrays are still there when I update the database or how to remove all previous arrays when the document is updating.
Here is my .php-file:
<?php
$username = "perhaps not sharing this information";
$password = "or this";
$database = "nah";
mysql_connect("the server url",$username, $password);
#mysql_select_db($database) or die("Error here");
$query = "SELECT * FROM debug_db";
$result = mysql_query($query) or die(mysql_error());
$num = mysql_numrows($result);
mysql_close();
$rows = array();
while($r = mysql_fetch_assoc($result))
{
$rows[] = $r;
echo json_encode($rows);
}
?>
And check out the json-document at: http://app.levinnovation.se/getjson.php
Thank you!

mysql query run only for numbers

$username = $_POST['username'];
$password = $_POST['password'];
$passworda = $_POST['passworda'];
$email = $_POST['email'];
if ($password == $passworda){
echo 'this is the user name to be chacked:' .$username .'<br>';
$query = "SELECT username FROM mishta where username=$username";
$queryrun = mysql_query($query);
echo $queryrun .'<br>';
echo mysql_num_rows($queryrun) .'<br>';
if (mysql_num_rows($queryrun)!=1){
mysql_query("INSERT INTO mishta(id, username, password, email) VALUES ('', '$username','$password','$email')");
}
else{
echo 'user already exist';
}
Hello there,
I'm trying to make a simple registration form, I get useername from html form and I want to make sure it doesn't already exist in my database.
the thing is that whenever I run the username as a number (1,2,3...) it runs smoothly.
but, when I try to write anything with letter, like Bob or Dora I get the following error:
Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in...
Yet the information is still being stored my my data base, means it got false on the last if statement, and it will continue to get false (It will just insert duplicates if I try again).
Any help would be appreciated.
You forgot the quotes around the username:
$query = "SELECT username FROM mishta where username='$username'"
Quotes are used as string delimiter. Numbers are not string and it works if you use a number.

How to display the values returned by count in SQL

i keep having this error "mysql_fetch_array() expects parameter 1 to be resource, null given in" when i try to display the returned value of count in sql. heres my code.
$query="SELECT med_rec_ID, COUNT(med_rec_ID)
FROM med_issue
WHERE MONTH(issue_date) = MONTH('2013-02-05')
GROUP BY med_rec_ID";
$result= mysql_query($query);
while($count = mysql_fetch_array($display3)){
echo $count[0];
}
i have tried to run the query in sql alone it displays 2 columns (the med_rec_ID, and the COUNT). guys how do i display the count and fix the error too?
First of all, don't use mysql_* functions since they're deprecated. Use mysqli or PDO.
Secondly, look at what you're passing into the fetch_array function.
You probably want to do something like:
$link = mysqli_connect("localhost", "admin", "pass", "db_name");
$result = mysqli_query($link, $sql);
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$medIds[] = $row['med_rec_ID'];
...
}
Then fix the count by giving it an alias.
Please note that you should actually store how you access the DB in a more secure manner, but I use this only to illustrate the example. Here's a pretty good post: How to create global configuration file?
Is your query even executing? that error will happen if mysql_query doesnt return the resource, in case query fails
$query="SELECT med_rec_ID, COUNT(med_rec_ID) as C FROM med_issue where MONTH(issue_date) = MONTH('2013-02-05') GROUP BY med_rec_ID";
$result= mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result))
{
echo $row["C"];
}
Note: Please do not use mysql_* functions anymore
Give it an alias:
SELECT
med_rec_ID,
COUNT(med_rec_ID) TheCount
FROM med_issue
where MONTH(issue_date) = MONTH('2013-02-05') GROUP BY med_rec_ID
then you can select that column TheCount inside the while loop with $row['TheCount'], also use lope through the $result:
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo $row['TheCount'];
}

use a single return from a sql query

I'm using PHP to make a very specific sql query. For example sake, I have the user's ID number, but I need their name. So I do a sql query from that table with the ID number in order to return the name.
$result = mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db);
Now I want to use that. What's the most succinct way to go about making that result into a variable ths I can use?
edit:
I'm hoping that this is not the answer:
$rowCheck = mysql_num_rows($result);
if ($rowCheck > '0') {
while ($row = mysql_fetch_assoc($result)){
foreach ($row as $val){
$username = $val;
}
}
}
I have used something like this to keep it short in the past:
list($name) = mysql_fetch_row(mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db));
echo $name;
In my opinion, the best way to fetch any SQL result is through mysql_fetch_assoc(). To use it, you would do something like this:
$result = mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db);
while ($row = mysql_fetch_assoc($result)) {
echo $row['name']; // You get an array with each column returned from your query.
}
Still, MySQL extension has been replaced for MySQLi, which is acknowledged to be faster and more practical. It has both OOP and structural bindings, and takes more into account your server settings.
$result = mysql_query("SELECT name FROM users WHERE userID=$thisuserid",$db);
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$name = mysql_fetch_row($result)[0];
You should use MySQLi as bellow:
$db = new MySQLi($host,$user,$pass,$db);
$query = $db->query('SELECT name FROM users WHERE userID='.$thisuserid);
$result = $query->fetch_object();
echo $result->name;
If you use SELECT * so you also can access via $result->{field_name}

JSON encode comma delimited row

I'm trying to add an autocomplete tokenizer script to some form fields and one issue I'm having is if a person saves multiple values for the field the autocomplete suggestions come back with all of his values as one long value instead of them being single values delimited by the comma. I first tried to simply explode the value but it doesn't format it correctly in the JSON encode.
Here is my PHP file:
//connection information
$host = "localhost";
$user = "myuser";
$password = "mypass";
$database = "mydb";
$param = ($_GET["term"]);
//make connection
$server = mysql_connect($host, $user, $password);
$connection = mysql_select_db($database, $server);
//query the database
$query = mysql_query("SELECT cb_activities FROM jos_comprofiler WHERE cb_activities REGEXP '^$param'");
//build array of results
for ($x = 0, $numrows = mysql_num_rows($query); $x < $numrows; $x++) {
$row = mysql_fetch_assoc($query);
$activities[$x] = array(cb_activitiesterm => $row[cb_activities]);
}
//echo JSON to page
$response = $_GET["callback"] . "(" . json_encode($activities) . ")";
echo $response;
mysql_close($server);
This gives the output like this:
[{"cb_activities":"Kicking Cats,"},{"cb_activities":"baseball,hockey,"}]
but I need it to output like this:
[{"cb_activities":"Kicking Cats,"},{"cb_activities":"baseball,"},"cb_activities":"hockey,"}]
I also need to find a way to prevent duplicate entries from populating. For instance, the way it is now, say 10 people all have kicking cats selected as a value, it will display 10 times in the autocomplete suggestions.
How do I set this up to correctly delimit at the commas and then weed out duplicate values?
NM the duplicate issue, I just added select distinct instead of just select, this json thing has me overcomplicating things now lol. Now if I can just figure out how to delimit properly at the comma all will be good.