I have a problem with a mysql_query - YES, i know it's updated and i need to upgrade it to pdo or mysqli..
However, it only get one row from the database, where it needed to pull out 3 rows. It only take the first row it find.
$result = mysql_query("SELECT * FROM packages WHERE workerid = '$id' AND approved = '1'");
$packed = array();
while($row = mysql_fetch_assoc($result)){
$packed[] = $row;
return $packed;
}
Right now i just print_r($packed);, which only give me the first row of the table. Workerid and approved are checked on the other rows, and they should be able to get pulled out. I have a similar code in my functions, which work perfect, so i cant really see the error here.
could try
while($row = mysql_fetch_assoc($result)){
array_push($packed, $row);
}
print_r($packed);
return $packed;
or
while($row = mysql_fetch_assoc($result)){
$packed[] = $row;
}
print_r($packed);
return $packed;
This should push each row return onto the end of the array. I am using print_r so that you can see if the array contains all the values you are looking for.
Related
here is my problem.
function getSent($id) {
include 'dbh.php';
$data = array();
$sql = "SELECT * from message WHERE senderid='$id'";
$result = $con->query($sql);
$row = $result->fetch_assoc();
while($row = $result->fetch_assoc()) {
$data[] = $row;
}
return $data;
}
In the theory this method is picking all messages (in this case), but it always find one message less than in the db => If I drop the whole table, I will get an array with the size of 0.
How could I fix this?
Remove the first $row = $result->fetch_assoc();. You are fetching one row, then looping to fetch the rest of the data and you don't store the first fetch.
`
I am trying to return a json from a MySQL query to use it with xCode but I cannot get an array of several objects with multiple fields. I have read the documentation on php.net and over here, but I still can't get it.
1) Let's say I have a MySQL table with 3 rows (for example 3 people). Each row contains 3 fields (lastname, firstname, dateOfBirth):
$result = mysqli_query($mysqli, $sql) // where $sql = "SELECT * FROM tbl_syncList"
$resultArray = array();
while ($row = $result->fetch_array()) {
$resultArray[] = $row["lastname"];
}
echo json_encode($resultArray); // --> return "["Lastname1", "Lastname2"...] - that's okay, I understand I return the value of the key "lastname"
I don't know how to get an array with the entire rows (all the fields at once), like:
[["Firstname1", "Lastname1", "dateOfBirth1"], ["Firstname2", "Lastname2", "dateOfBirth2"],...]
I tried to replace
$resultArray[] = $row["lastname"];
with:
$resultArray[] = $row;
but it just gives the world...
"array"
...with no content. Does anyone have an idea?
Thanks a lot!
Are you sure the array is properly populated to begin with? print_r($resultArray) and see what you have there with = $row (which is correct).
Don't ask me why I need it, I've tried to find it on the web and have failed.
I need it to find rows of info on my database and per row it creates a array with one row and stores that array in an array and carries on with the rest of the rows.
I will then generate a new word doc per array found in the array of arrays( I can do this myself.)
I'm sorry I don't make much sense, I don't really know how to explain this...
Do you mean:
$sql = "SELECT * FROM tbl";
$query = mysql_query($query, $connection);
$rows = array();
while ($row = mysql_fetch_array($query)) {
$rows[] = $row;
}
// You now have an array of your DB rows in $rows
foreach ($rows as $row) {
createWordDoc($row);
}
If you don't actually need to cache the rows you could do this directly in your first loop of course:
$rows = array();
while ($row = mysql_fetch_array($query)) {
createWordDoc($row);
}
How can i get every row of a mysql table and put it in a php array? Do i need a multidimensional array for this? The purpose of all this is to display some points on a google map later on.
You need to get all the data that you want from the table. Something like this would work:
$SQLCommand = "SELECT someFieldName FROM yourTableName";
This line goes into your table and gets the data in 'someFieldName' from your table. You can add more field names where 'someFieldName' if you want to get more than one column.
$result = mysql_query($SQLCommand); // This line executes the MySQL query that you typed above
$yourArray = array(); // make a new array to hold all your data
$index = 0;
while($row = mysql_fetch_assoc($result)){ // loop to store the data in an associative array.
$yourArray[$index] = $row;
$index++;
}
The above loop goes through each row and stores it as an element in the new array you had made. Then you can do whatever you want with that info, like print it out to the screen:
echo $row[theRowYouWant][someFieldName];
So if $theRowYouWant is equal to 4, it would be the data(in this case, 'someFieldName') from the 5th row(remember, rows start at 0!).
$sql = "SELECT field1, field2, field3, .... FROM sometable";
$result = mysql_query($sql) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($result)) {
$array[] = $row;
}
echo $array[1]['field2']; // display field2 value from 2nd row of result set.
The other answers do work - however OP asked for all rows and if ALL fields are wanted as well it would much nicer to leave it generic instead of having to update the php when the database changes
$query="SELECT * FROM table_name";
Also to this point returning the data can be left generic too - I really like the JSON format as it will dynamically update, and can be easily extracted from any source.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo json_encode($row);
}
You can do it without a loop. Just use the fetch_all command
$sql = 'SELECT someFieldName FROM yourTableName';
$result = $db->query($sql);
$allRows = $result->fetch_all();
HERE IS YOUR CODE, USE IT. IT IS TESTED.
$select=" YOUR SQL QUERY GOOES HERE";
$queryResult= mysql_query($select);
//DECLARE YOUR ARRAY WHERE YOU WILL KEEP YOUR RECORD SETS
$data_array=array();
//STORE ALL THE RECORD SETS IN THAT ARRAY
while ($row = mysql_fetch_array($queryResult, MYSQL_ASSOC))
{
array_push($data_array,$row);
}
mysql_free_result($queryResult);
//TEST TO SEE THE RESULT OF THE ARRAY
echo '<pre>';
print_r($data_array);
echo '</pre>';
THANKS
I am getting only one row, can someone please tell me how to get all the data from the table column of my database table ?
public function getCategories(){
$result = $this->db->query('SELECT * FROM newscat');
$rows = array();
while($row = $result->fetch_assoc()){
$rows[] = $row;
return $rows;
}
}
You're returning from within the loop. That will break it in the first round.
return outside the loop.
do the minor change
public function getCategories(){
$result = $this->db->query('SELECT * FROM newscat');
$rows = array();
while($row = $result->fetch_assoc()){
$rows[] = $row;
}
return $rows;
}
Your problem is the return $rows;. It should reside after the while. The thing is that it will enter the while, put the first row in the array, and then immediately return it. What you want is to let the while do its thing, and after the it finished, return the array.
If you are using mysqli.
Then you can use its apiFfetch_all to get all the rows at once.
For example :
$array=$result->fetch_all(MYSQLI_ASSOC);
The above code will get all associated rows in the corresponding array.