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 ");
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.
I want to count all record from table without specify any condition :
now, i am doing by this way
$result['cms'] = Cms::find()->where([])->count();
and it will give me result,but i don't want to use where clause.
So how to count all records without where clause.
Thank you
You can see this doc http://www.yiiframework.com/doc-2.0/yii-db-activequery.html
simply using
count(): returns the result of a COUNT query.
Cms::find()->count();
all(): returns an array of rows with each row being an associative array of name-value pairs.
Cms::find()->all();
see this guide for more http://www.yiiframework.com/doc-2.0/guide-db-query-builder.html
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]).
Within a MySQL query, is it possible to put a PHP function around a field name in order to change/replace its value?
SELECT * FROM my_table WHERE url_title(field_name) = '$variable'
The reason for asking is that the '$variable' in my URL which the query will use is an edited version (using Codeigniter's url_title function) of its true value eg. $variable = 'dangelo' whereas its true value in field name is "D'Angelo".
You can implement the url_title function as a MySQL stored procedure, which would require translating the PHP code of that function to a MySQL stored procedure.
Note that the performance would not be great due to having to call this function for every row on each query.
The more typical solution is to have another field for the slug, pre-generate the values for each article's slug. On the query, you would compare $variable to the new field.
Yes it's possible. But not at all recommended because it wouldn't be able to make use of the index. Create another column with the actual value you want to find and add an index to that column.
I want to know is this practically possible in sql(using php as server-side), where in you have an array of values(numbers), and you try to retrieve data based on values inside that array..
I have a sql table:
posts(
id int(11) primary key,
posts varchar(140),
user_id int(11) foreign key
);
I write a query now to retrieve 'posts':
$query="SELECT * FROM posts WHERE user_id=(**Array of select user_id's**)";
Is there any sql in-built function to check values inside an array? or should I be using PHP for this?
I am actually trying to implement the twitter model of showing posts of people whom we follow.
Yes, this is easily possible. You need to look at MySQL's IN function
Your query would be something like
SELECT * FROM posts WHERE user_id IN (1,2,3,4,5,6)
You can build the bit in between the parentheses in PHP using implode()
SQL can't parse PHP arrays. Try this:
$query="SELECT * FROM posts WHERE user_id IN ({implode(',', $userIDarray)})";
Take a look at this page : WHERE ... IN. You can use the IN operator to check if a certain value exists within an list of values.
Yea you will have to you use following syntax
SELECT 'value'
IN (array)