Hello in my php code mysql only fetches 1 result which is in the last rather than all please help me why its doing like this here is my code
$requesthost = "SELECT * FROM tblvhost";
$reshost = mysql_query($requesthost);
while($rowhost=mysql_fetch_array($reshost)) {
$hostvid = $rowhost['vh_id'];
$hostname = $rowhost['vh_name'];
}
You’re overriding the data within each loop iteration:
while($rowhost=mysql_fetch_array($reshost)) {
$hostvid = $rowhost['vh_id']; // overwritten
$hostname = $rowhost['vh_name']; // overwritten
}
To collect all the data you probably want to append them to an array:
$hostvid = array();
$hostname = array();
while($rowhost=mysql_fetch_array($reshost)) {
$hostvid[] = $rowhost['vh_id']; // insert into array
$hostname[] = $rowhost['vh_name']; // insert into array
}
You are overwrite your variables in your loop. Store them into an array.
while ($rowhost = mysql_fetch_array($reshost)) {
$row[] = array(
'hostvid' => $rowhost['vh_id'],
'hostname' => $rowhost['vh_name']
);
}
var_dump($row);
this is a good question plus its a learning curve for the ones that are new that run into these types of issues
you're code
$requesthost = "SELECT * FROM tblvhost";
$reshost = mysql_query($requesthost);
while($rowhost=mysql_fetch_array($reshost)) {
$hostvid = $rowhost['vh_id'];
$hostname = $rowhost['vh_name'];
}
looks good, but what you are doing with or how are you applying hostvid? or hostname? you can just do
while($rowhost=mysql_fetch_array($reshost)) {
$rowhost['vh_id']; //do something with the var that's getting mysql field data
$rowhost['vh_name']; //same applies here. using $rowhost will continue to to provide data depending on what you have in the db. so you're OK.
}
if you want to store the data in an array for later use outside of the loop you can follow what #mudasobwa posted. good answer.
Related
I need to be able to check if certain values are present in title_id, but as title_id's are index's it won't allow me to do this. Is there a way to store the values of the index without storing the array itself?
I have been searching for this answer for a long time but anything I search pulls up answers to different questions.
$selects = (array) null;
$res = mysqli_query($datab, "SELECT title_id FROM selects") or die($datab->error);
while ($datab = mysqli_fetch_assoc($res)) {
$selects[] = $datab;
}
///////////// part of a later query
if (!in_array($thisid, $selects)) {
$title[] = $datab;
$j++;
}
$selects is array of arrays, not array of title_id.
Try something like that:
while ($datab = mysqli_fetch_assoc($res)) {
$selects[] = $datab['title_id'];
}
i try to list a overview for a gallary by using 'list' and 'foreach' , but i always get just one (the oldest) result from my database.
i use this
while($row = mysql_fetch_array($result)){
$galpath1 = "galerie/";
$gal_title = $row['gal_title'];
$gal_path= $galpath1.$row['uploads'];
$nickname = $row['nickname'];
$added = $row['format_date'];
$galshow = [
[$gal_path, $gal_title, $added],
];
}
if(!empty($gal_path))
{
var_dump($galshow);
foreach ($galshow as list($gal_pathes, $gal_titles, $addeds)) {
List of galerys
}
how can i bring it to work so that i can list all gallerys that matches the select in my database?
Thank you so much cdhowie
you saved my day. that was exactly what i did wrong.
so thanks for the quick response. :)
Every iteration of the while loop you overwrite the value in $galshow with a new array containing one element. As a result, this array will only ever contain one element, and it will be the last row fetched from the database.
Instead, create a new array outside of the loop and append to it inside the loop:
$galshow = array();
while($row = mysql_fetch_array($result)){
$galpath1 = "galerie/";
$gal_title = $row['gal_title'];
$gal_path = $galpath1.$row['uploads'];
$nickname = $row['nickname'];
$added = $row['format_date'];
// This syntax will append a new array to the end of the $galshow array.
$galshow[] = [$gal_path, $gal_title, $added];
}
I have an array returned from MYSQL query with 2 LEFT JOINs.
Question is: "Is there another way for writing the code below?". I got the code but I want a more clear way of it just to understand what happens inthere.
CODE:
$result = array();
while ($resultArr = mysqli_fetch_assoc($booksAndAuthors)) {
$result[$resultArr['book_id']] ['book_name'] = $resultArr['book_title'];
$result[$resultArr['book_id']] ['author'][] = $resultArr['author_name'];
print_r($result);
}
With the extract() function you can make the data in the result into variables. I have put the example below inside a function, so they will be local variables.
function getBooksAndAuthors($queryResult)
{
while ($data = mysqli_fetch_assoc($queryResult))
{
extract($data);
$BooksAndAuthors[$book_id] = array('book_name' => $book_title,
'author' => $author_name);
}
return $BooksAndAuthors;
}
This makes the code a lot more readable. You will, of course, have to know which columns there are in your database table. I also left out the extra '[]' for the author.
Here's how I recommend writing it, because I don't think you should depend on automatic creation of intermediate arrays.
$result = array();
while ($row = mysqli_fetch_assoc($booksAndAuthors) {
$bookid = $row['book_id'];
if (!isset($result[$bookid])) {
# First time we see this book, create a result for it
$result[$bookid] = array('book_name' => $row['book_title'],
'author' => array());
}
# add this author to the result entry for the book
$result[$bookid]['author'][] = $row['author_name'];
}
It's essentially equivalent, but I think it also makes the logic clearer.
I'm using the Twilio API to send sms. I'm trying to change it so that I can send to a list of recipients from mysql results.
The example code given is:
$people = array(
"+14155551212" => "First Lastname",
);
My code is:
$people = array(
while($res = mysql_fetch_array($usersphone)) {
$people[$res['UserMobile']] = $res['UserFirstName'];
}
);
The syntax is bad but I can't figure out where.
You can't put control structures into arrays.
$people = array();
while ($res = mysql_fetch_array($usersphone)) {
$people[$res["UserMobile"]] = $res["UserFirstName"];
};
Also, there's a ton of posts here on SO that will tell you all about not using the mysql_* functions anymore, since they're deprecated.
You have logic in your array definition. You should define the array, and then populate it with the while.
// define the array
$people = array();
while($res = mysql_fetch_array($usersphone)) {
// populate key with mobile and value with name
$people[$res['UserMobile']] = $res['UserFirstName'];
}
How would i go about the following problem which involves running an update query for every row in the array defined below? I hope it becomes clear...
<?php
//Some code
user = array();
while ($row = mysql_fetch_array ($auctioncheck)){
$fdt[] = $row['finish_time'];
if ($date_time >= $fdt) {
$user[] = $row['current_bidder'];
{
{
I then want to update a table which sets the value as "user[]". What is the neatest way of doing this? I presume it requires a while loop only i don't know how to do it in this context. So it would be like...
<?php
//Above while loop and then...
$update = mysql_query("UPDATE homepage SET username = '$user[]'...so on");
Basically i want the update to be performed for every user[] in the above array. I might be able to figure it out if i knew how to determine the number of rows in the the user array. Any help would be much appreciated. Cheers.
It looks like the for each will come into play. Only i am now concerned with the elements of multiple arrays being used in the update.
$user = array();
$seller = array();
while ($row = mysql_fetch_array ($auctioncheck)){
$fdt[] = $row['finish_time'];
if ($date_time >= $fdt) {
$user[] = $row['current_bidder'];
$seller[] = $row['seller'];
}
}
$update = mysql_query("UPDATE homepage SET username = '$user[]'...WHERE username = '$seller'");
Any ides anyone for the multiple elements and arrays.
It should be $user = array();
Is this what you're looking for?
<?php
//Above while loop and then...
foreach($user as $value){
$update = mysql_query("UPDATE homepage SET username = '$value'");
}
?>
This is not PHP
user = array();
You are missing the $
Besides
where is
{
{
come into play?