Selecting all record in mysqli - select

I want to select all record in mysqli, I am using the following syntax
$stmt->get_result();
but i am getting single data, i want to select all the data and then apply the while loop on the data.
Thanks In Advance

Wrap it in a while loop:
$stmt->get_result();
while ($row = $result->fetch_array(MYSQLI_NUM)) {
$rs[]=$row;
//Or just process it here.
}
Or use http://php.net/manual/en/mysqli-result.fetch-all.php

Related

Add a single field to model using raw SQL

I'm developing an extension for TYPO3 CMS 8.7.8. I'm using query->statement() to select all fields from a single table, plus 1 field from another table. I get a QueryResult with the proper models and I would like to have that 1 extra field added to them. Is that possible?
You can do SQL queries with the ->statement(...) method and in that, use normal JOIN commands
From the documentation
$result = $query->statement('SELECT * FROM tx_sjroffers_domain_model_offer
WHERE title LIKE ? AND organization IN ?', array('%climbing%', array(33,47)));
So you can do JOINs on whatever table you want to (also code from the documentation)
LEFT JOIN tx_blogexample_person
ON tx_blogexample_post.author = tx_blogexample_person.uid
But you will end up with the raw data from the mysql query. If you want to transform it into a object, use the Property Mapper
You can use JOIN in your sql statments like below.
$query = $this->createQuery();
$sql = 'SELECT single.*,another.field_anme AS fields_name
FROM
tx_single_table_name single
JOIN
tx_another_table_name another
ON
single.fields = another.uid
WHERE
O.deleted = 0
AND O.hidden=0
AND O.uid=' . $orderId;
return $query->statement($sql)->execute();

Multiple DB update query using Laravel 5

I'm new in Laravel and I'm trying to update mysql table's multiple rows using one query. I need to update for example 100 rows like this:
$q = \DB::table('exmaple')
->where('exampleID', $array)
->update(array('Viewed' => 1));
$array is array with ID-s.
I have try also to implode array to string, then execute query, but with no result. Can anyone help?
Does this work for you:
$q = \DB::table('example')
->whereIn('exampleID', $array)
->update(array('Viewed' => 1));
whereIn() accepts an array as a second parameter so all IDs in this array will be affected by the query.

get most actual row with zend's fetchRow

I'm very new to zend (1.12)..so please excuse my very basic question:
I want to fetch only one row from a database. Thatfore I want to use the fetchRow(..) function like this
$row = $db->fetchRow($db->select()->where("col1 = '".val1."' AND col2='".val2."'"));
The problem is, that there may be many rows that fit to the where-clause and I only want to get the one with the highest id. How can I do this?
The fetchRow() method returs only one row. If you want to choose row with the highest ID meeting other conditions, invoke it like this:
$select = $db->select()
->where('col1 = ?', $val1)
->where('col2 = ?', $val2)
->order('id DESC');
$row = $db->fetchRow($select);
Also, remember to pass values to SQL query in the way as in above code (to avoid SQL injection attack risk).

How to choose which comes first when specifying OR in MySQLi SELECT statement

When you retrieve data through a MySQLi statement such as the following:
$sqls = "SELECT * FROM course WHERE course='$product_id_array' OR course='Both' ORDER BY ...";
$sqlsresults = mysqli_query($db_conx,$sqls);
while($row = mysqli_fetch_assoc($sqlsresults)) {
$selectedContent = $row["content"];
$selectedTitle = $row["title"];
}
Is there a way to output the match for 'both' first? Would this be as simple as re-arranging the order of the WHERE portion of the statement?
Your ORDER BY explicitly sorts only by id. There is no way around this in the WHERE part of your query. To change how results are ordered, use ORDER BY.
SELECT *
FROM course
WHERE course=...
OR course='Both'
ORDER BY CASE course WHEN 'Both' THEN 0 ELSE 1 END ASC, id DESC
Unrelated note: if $product_id_array contains untrusted user input, the user can put things like '; DELETE FROM course; -- in there. Read up on parameterized queries to learn how to prevent that.

how do you insert multiple rows in a loop using doctrine 2

i want to insert multiple rows in a loop using doctrine 2..
i usually insert 1 record using this:
$Entity->setData($posted);
$this->_doctrine->persist($Entity);
$this->_doctrine->flush();
Simply persist all your objects and then call flush() after the loop.
$entityDataArray = array(); // let's assume this is an array containing data for each entity
foreach ($entityDataArray AS $entityData) {
$entity = new \Entity();
$entity->setData($entityData);
$this->_doctrine->persist($entity);
}
$this->_doctrine->flush();
If you're inserting a large number of objects you will want to batch insert (see http://www.doctrine-project.org/docs/orm/2.0/en/reference/batch-processing.html)
Inside your loop, you should be able to simply:
$entity1->setData($data1);
$this->_doctrine->persist($entity1);
$entity2->setData($data2);
$this->_doctrine->persist($entity2);
$this->_doctrine->flush();