How do I fetch an integer value from the database?
$query = mysql_query('select number from table_name where UNIQUE_ID =SOME_UNIQUE_ID');
I am unable to access the integer value using the query above.
As many have commented, it's unclear what is the problem your are facing.
The $query variable you are obtaining is NOT the integer you want but a "query object" (or False if the query failed). You have to extract at least a row using (e.g.) $t=mysql_fetch_array($query). The wanted value will be in $t[0].
Keep in mind, anyway, that the result of a query is tipically a string, so you have to extract its integer value using intval($t[0]).
Related
In my system, I have plenty of instances of code like the following:
$value = mysqli_fetch_array(mysqli_query($con, "SELECT SUM(price) as total from items WHERE a=1 AND b=2"));
Then $value is used later in the code and has the total price calculated from the array
I'm trying to replicate this in a new piece of code as follows:
$recent_sale_id = mysqli_fetch_array(mysqli_query($con, "SELECT id as id FROM items where item_code='$item_code' and status='BULK-ITEM-SALE' order by sold_at DESC LIMIT 1"));
The query works when run directly against the database (of course replacing $item_code with the item code). But $recent_sale_id then just has the value 'Array'.
I'm wondering two things:
What am I doing wrong? My code seems to be exactly the same as the other code that works correctly.
Is there a simpler way to get a value from a query into a field, without using a function that seems like it will create an array? Is there a more suitable mysqli_fetch* function?
There's no way you could access $value as a number because it is also an array. In the first case you would need to use either $value[0] or $value['total'] to get the result; in the second either $recent_sale_id[0] or $recent_sale_id['id']. You can use either form because mysqli_fetch_array by default returns arrays indexed both by column number and column name.
Unfortunately the MySQLi interface does not have an equivalent to PDO's fetchColumn which allows you to directly fetch the value of a single column from a row in a result set.
If $recent_sale_id is returning a array in this case, that means it is of array type and in terms of getting data from mysql query often it comes in array format though you have added a limit 1.
One thing you can do to get the value is try $recent_sale_id[0] for the actual output.
In pg_select function the third argument is a array used to specify the column name and its value.
It works like an and condition if we give more than one key => value.
I have id column in my table I want to fetch the rows which has id value more than 1000.
How to give value in associative array in pg_select function for the above requirement.
I need answer without using pg_query function.
Unfortunately, pg_select is too simple for this. You have to write the full query one way or another.
you can using SQL:
pg_query("select * from table_name where id <1000 ");
I am trying to something in MySQL that I do in JavaScript/PHP all the time. I need to concatenate the value of a field in MySQL with a value that is passed into a query from PHP. For example, let's say I have a field called favourites with a value of 27 and I have this query:
UPDATE useraccs SET favourites = favourites + ',30' WHERE id='10'
My desired new value for favourites would be 27,30, but I'm getting 57, where clearly SQL is adding them numerically. I have set the data type for this column as TEXT and was hoping that would force SQL to treat it as a string all the time, but that doesn't seem to be the case.
In my research I read about the CONCAT() function, and I tried this:
UPDATE useraccs SET favourites = CONCAT(favourites,',30') WHERE id='10'
That results in a failed query. The logic feels right but that is obviously not how that function is meant to be used.
I acknowledge that in theory, I could just grab the original value of favourites and concatenate it with the new value in the PHP itself and then send it to MySQL, but I feel like there MUST be a way to do this in one query...if I'm wrong about that so be it, but I'm sure there must be a way.
Use the following to create '27,30':
CONVERT(favourites,char) + ',30'
I am trying to improve an old system (written ages ago) where every mysql queries are glued from string. So example of that query looks like
"SELECT * FROM User WHERE id > '3'"
Id column is of course bigint and PK.
What does mysql do with '3' in query where id should be a int value? I assume it is treated as a string (due to '') so this value is casted into int during analyze/optimize process by mysql. Am I right?
//UPDATE
I probably asked wrong way. There are two way to handle it.
(Fast) Mysql automatically detects that id should be int and rewrite/cast a query to
SELECT * FROM User WHERE id > 3
before send it to DB engine
(Unbelievable) Mysql does
SELECT * FROM
then in loop apply condition WHERE id > '3' and cast it for EVERY row
I just want to be sure that second option is impossible.
MySQL will always cast the string to a number for comparing, which in this case this is the right thing to do (it will use the index on the column to find the values).
If your column is a string and you compare it to an integer constant MySQL will cast the column to an integer and not use the index.
MySQL will automatically cast it into the correct column type. If it cannot for some reason, it will throw an error.
Make sure to use prepared statements with PDO or MySQLi in any case where the parameter may come from an unsafe source (the user, an external API).
select * from Table where data=2.6
After running the above query, db returned an empty result set. But Seen from table, there are many rows matching data=2.6 criterion.
Here the type of 'data' column is FLOAT. If changing the value to an integer like 'where data=2', the matched result sets will be returned.
Please let me know if anyone see the error on the query? Many thanks!
FLOAT is "approximate numeric data type" (http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html), you should use NUMERIC instead.