Unexpected field in Mysql fetch data - mysql

I have a query in which the data is lifted from the database.
Eg: I have Category as a field which has values like Doctors, Pathologists etc which is taken from the database.
Category
--------
Doctors
Pathologists
Pediatrics/Child Specialist --->> Unwanted data
The problem is there is a data(value) in 'Category' field which is unexpectedly showing in the list which is not in my database. I even tried to drop the table entirely and reload it but that field is still showing.
Can anyone tell me why is this happening?
Additional Info
function getTierCategory()
{
echo "</br>";
$result = mysql_query("SELECT DISTINCT Category FROM doctors order by Category asc")
or die(mysql_error());
while($tier = mysql_fetch_array( $result ))
{
echo '<option value="'.$tier['Category'].'">'.$tier['Category'].'</option>';
}
}
I have tried renaming the field but how can I if this value is not even in my database.
My Database looks like
Category | Qualification | State | District
--------------------------------------------
and so on

Log in to your database in PHPmyadmin or something like that and check results of your mysql query.
In some complex queries you can receive redundant fields (on example indexes when you have counting variable in PHP) or NULL values.
mysql_fetch_array() returns results in two ways:
number indexed array ($array[0] etc)
associative array ($array['field_name'] etc)
Try with mysql_fetch_assoc or mysql_fetch_row instead. Also PDO_MYSQL API is recommended to use for MySQL querying. It has more convenient form.

Related

Laravel mongodb where array field contains value

I use mongodb and mysql together with Laravel.
Fields table in mysql database can have more options in mysql database.
fields table in mysql: id, name
options table in mysql: id, field_id, name
so in mongodb database I have adverts table and this adverts table have field and field equal to options
something like this for example {field_id_1: [option_id, option_id]}. When I use it like this there is no problem when querying the database. It is simple like this $advert->whereIn('field_id_1', $request->options).
but then I decided why not to keep this options as an array in mongodb and without field_id like this {options: [option_id, option_id, option_id]}. So there is the question now I somehow want query the database where this options field contains requests value something reverse of $adverts->whereIn('field', $options)
I know a way but not sure
$adverts->where('options', 'LIKE', '%"'.$option_id.'"%');
I don't know if this answer will work for you :
$result = $adverts->where("options", "all", [$option_id])->get();
or :
$result = $adverts->where("options", "all", "$options_ids")->get();
where $options_ids are an array of options

SQL query to retrieve exactly matching record with or without LIKE

From a rest API URL, I'm receiving a user ID that looks like this: 58988e75c918f5bd5804afd6.
The database has the user name stored in the name field in the format:
58988e75c918f5bd5804afd6 John.
My current SQL query to fetch this record is:
$sql = 'SELECT * FROM users WHERE name LIKE :term';
$result = db_query($sql, array(':term' => db_like($userid)));
$existingUser = $result->fetchObject();
With the current SQL query, even if I supply the partial user ID(58988e75c918f5bd5804), it seems to fetch the John row, which is not something I expect to happen. How to fix the query so that it exactly matches the user ID containing John?
Expected result: No records to be retrieved since the user id did not match exactly.
Update: I fetch the records and then do a substring replace to update the record in the Drupal table.
Note: Feel free to modify the sql query to get the expected result, neednt use LIKE
Use a space in your like term:
Include space in mysql like search
$sql = 'SELECT * FROM users WHERE name LIKE ":term "';
$result = db_query($sql, array(':term' => db_like($userid)));
$existingUser = $result->fetchObject();
Better validate the string size with PHP and then perform mysql like. As you are using same encryption style you will get same number of characters encrypted so.

MySQL Select* returns everything but the id column

I have a database with 2 tables I wish to pull data from both and display it in an html table. When I execute the below select statement I seem to get everything but the auto_increment column "id"
>mysql connection strings here.
$query = "SELECT Hotels.*,surfcup_Rates.*
FROM Hotels
LEFT JOIN surfcup_Rates
ON Hotels.id=surfcup_Rates.hotelID";
Then I use load the query into $result.
Later I load it into mysql_fetch_array and loop it into an html table.
while ($row = mysql_fetch_array($result)) {
echo "</td><td>";
echo 'Delete';
echo "</td><td>";
echo $row['id'];
echo "</td><td>";
echo $row['preferred'];
>and so on...........
Everything works: the join, the select statement and data loads into the table
perfectly. All except the id column from the first table. I wish to use the id to allow the user to delete a record. The user will be adding data to the tables from a password protected "back-end" for data entry.
Is there something wrong with my select statement? Or am I missing something. Any help will be greatly appreciated.
Thank You,
Dave.
Assuming that your surfcup_Rates table has an id column (you don't say that explicitly, but it seems likely), then you either need to "rename" one of the id columns from the two tables so they don't collide when mysql_fetch_array figures out what to return for each row, or I believe you can just swap the two parts of the SELECT output selectors, given that you don't seem to care about the id from the surfcup_Rates table:
$query = "SELECT surfcup_Rates.*,Hotels.*
FROM Hotels
LEFT JOIN surfcup_Rates
ON Hotels.id=surfcup_Rates.hotelID";
In this case, the id from Hotels will mask the one from surfcup_Rates, not the other way around as you have it.
Try
$query = "SELECT *
FROM Hotels
LEFT JOIN surfcup_Rates
ON Hotels.id=surfcup_Rates.hotelID";
NOTE: I would probably do all the fields in the select
SELECT Hotels.id as HotelId, surfcup_Rates.id as SurfCupId,....
[others here] ... FROM Hotels
LEFT JOIN surfcup_Rates
ON Hotels.id=surfcup_Rates.hotelID
Does the surfcup_Rates table also have a field called id? If so, mySQL won't know whatid to use, hotel or surfcup_Rates.
In this case you have a couple of options,
changing the table structure, but do so at your own risk as other things could break
use an alias (best method)
DISCLAIMER
I have never used the following method in PHP myself, I have seen it in SAP and Zimbra, as this isn't a discussion forum on these aspects, instead of a debate I thought I would put a disclaimer that it may not work but as mentioned above and in my comment below I have seen it work
call hotels.id
I hope this helps

Selecting fields using variables for the fields

I have created a dropdown list where the user can select the fields they wish to have displayed. I can create a variable, say $field_list which then has 3 (user selected) fields, say title, first_name,last_name. So my SELECT statement would read (as a php statement)
$sql=" SELECT $field_list from my_table ";
which operates as
"SELECT title, first_name,last_name from my_table ";
The problem then arises when trying to display the data from each of these fields. I am trying to use the stucture as follows
$result=mysql_query("$sql");
while ($myrow = mysql_fetch_array($result))
{
echo "the data is $myrow["x"];
}
Howvere, I want to be able to use different values for x which are extracted from the $field_list using substr(). Normaly I would do this by using $x and changing the values as appropriate, but this syntax does not work. Any help would be appreciated.
Careful with quoting. Try echo "the data is " . $myrow[$x]; or echo "the data is {$myrow[$x]}";

Mysql is not counting the rows of dates from my php script

I am trying to count the number of dates that occur for a specific mem_id, however, my count output is always "0"... heres my code:
$datechecker = $postdate;
//$postdate is a date variable that is posted via a form into mysql, stored as a DATE
$sql = mysql_query("SELECT * FROM members WHERE mem_id='$id' AND postdate='$datechecker'");
$Counter = mysql_num_rows($sql);
if($Counter >= 0) {
echo "$datechecker $Counter";
exit();
}
This is the output I get: 2011-01-01 0
Even though I have about 10 occurrences of 2011-01-01, so why does my count say "0"? Can anyone help me solve this, or provide an alternate?
echo the string before you mysql_query it, this will validate if the query is what you expect it to be. You can also use count(*) within the query, instead of using php's mysql_num_rows()
I also hope that you're sanitising your input before you're querying that!
1) You didn't clean $postdate, who knows if it conforms to MySQL's definition of DATE?
2) You didn't show us how your members table looks like. Is mem_id primary key? If it is, then of course you're going to get 1 row out of it.
3) You aren't checking whether your query succeeds or not, you immediately pass the resource_id to the mysql_num_rows table - which is a wrong way to perform counting anyway because MySQL (like any other relational database) has inbuilt mechanisms of counting rows based on criteria.
My guess is that your query is failing, seeing there's not $id specified.