MySQLi Array in PHP sometimes coming up empty

Okay, so I'm pretty much at the end of my rope trying to discover the issue here, because sometimes this code works perfectly, and then sometimes not, so here it is:

$allItems = array(); $allItemsQuery = mysqli_query($conn,"SELECT * FROM cart_items WHERE crt_id='".$row['crt_id']."' AND quantity>0"); while($itemsArray = mysqli_fetch_assoc($allItemsQuery)){ array_push($allItems,$itemsArray['item_id']); } $allItemsNames = array(); foreach($allItems as $key => $value){ $itemNames = mysqli_fetch_assoc(mysqli_query($conn,"SELECT * FROM items WHERE id='$value'")); array_push($allItemsNames, $itemNames['item_name']); } $allItemsFinal = array_combine($allItems,$allItemsNames); $itemsList = 'Items:<br>'; foreach($allItemsFinal as $key => $value){ $itemsList .='<a href="http://www.exampleurl.org/product.php?ID='.$key.'">'.$value.'</a><br><br>'; }

The idea is it's supposed to get all of the item ID's the user has in their cart from the DB and put it into an array, which I've established exists, grab all of the item names based on their ID, and put that into an array, and then combine the two arrays as a key and value. Then I output the data from the arrays into a string.

This code works perfectly sometimes, and then suddenly not. Can somebody give me any idea why? I'd be indebted forever.

-------------Problems Reply------------

I think you should throw this code away and use something like this:

$items = array();
$itemsQuery = "SELECT i.item_name, i.item_id
FROM items i WHERE i.item_id
IN (SELECT ci.item_id
FROM cart_items
WHERE crt_id='".$row['crt_id']."'
AND quantity>0
)");

$allItemsQuery = mysqli_query($conn, $itemsQuery);
while ($row = mysqli_fetch_assoc($allItemsQuery))
$items[$row['item_id']] = $items[$row['item_name']];

Category:php Views:69 Time:2019-03-21
Tags: php arrays mysqli

Related post

  • process mysql array with php 2012-02-02

    I have an array coming from my database I want to format and display with php Array ( [customer] => Array ( [customer_id] => 1 [firstname] => Mitchell [lastname] => Bray [order_id] => 3 [date_added] => 2012-01-17 11:22:30 [name] =

  • Wordpress php, get_the_title() coming out empty when run through substr & strrchr 2012-03-16

    I'm currently working on a Wordpress site. And what the client wants is to be able to create a schedule for a convention. What he is asking for is the option of clicking a button in each post which would add a 15 minute Q&A slot. The way I went a

  • How can I convert this M-N mysql array in this other array in PHP? (examples inside) 2012-01-02

    Well, is pretty common to get an array like this in relations M-N in mysql: [0] 'book_id' => 1 'title' => 'title' 'author_id' => 1 'author_name' => 'name1' [1] 'book_id' => 1 'title' => 'title' 'author_id' => 2 'author_name' =

  • What is the easiest way to turn a mysql result variable into a multidimensional array in php? 2009-07-01

    What is the easiest way to turn a mysql result variable into a multidimensional array in php? Thanks --------------Solutions------------- if you happen to be using the mysqli extension and php 5.3, you could use http://us2.php.net/manual/en/mysqli-re

  • Organize array in PHP from mysql 2010-06-17

    Hi i have a social networking website. what i want it to do is pull out my friends status updates. basically what it does is i have a mysql query that pulls out all of my friends and in that while loop there is another mysql query that pulls out the

  • Creating an empty 2D array in PHP? 2011-07-07

    I know that arrays are created dynamically, and creating them ahead of time isn't really nessicary, but how would one do that with a 2D array? The same way? (for$j) { for($i) { $array[j][i] = "data"; } } Something like that? Obviously real for loops,

  • Intersection of two arrays coming up empty 2011-08-30

    I have two arrays and I'm trying to intersect them so I can run a scope, my rails code looks like: def index @lists = current_user.lists @tasks = @lists.collect{|list| list.tasks} @search_tasks = Task.search(params[:search]) @user_tasks = (@tasks

  • Function to Make Empty String NULL for MySQL Insertion in PHP 2011-10-13

    This is similar question to MySQL and PHP - insert NULL rather than empty string but I'm still encountering the problem. I'm trying to create a function so that an empty string is inserted as a NULL into MySQL. I create the function IsEmptyString: fu

  • deserializing json nested object when some times few elements coming as empty array, and some times an object 2011-12-16

    I am new to windows phone7. In my app we are using webervices. For one request if there is data, it appears as {"login_status":"1","req_start_time":"2011-12-15 19:19:01","req_end_time":"2011-12-15 19:19:01","exec_tine":"Page Execution Takes : 0.08275

  • What is the best way to pass an array from php to MySQL stored procedure? 2012-01-12

    I am looking to pass a list of id's from PHP to a MySQL stored procedure so that I can then join to this list. I know that I don't want to pass the array as a csv list to the SP, but I'm not sure what the best practice is to do. With SQL Server I've

  • What do you recommend to do MySQL queries on PHP array without MySQL connection (interpreter)? 2012-02-09

    The idea is this: <?php // table definition as a php array $array = array( array( "code" => 0, "value" => 1 ), // => row1 array( "code" => 1, "value" => 2 ),// => row2 array( "code" => 2, "value" => 345 ),// => row3 arra

  • Mysql extracting array in Php 2012-02-29

    What is the problem with my mysql array extraction below: $mysql_array = mysql_query("SELECT * FROM table1 WHERE uid1='$uid1'"); $array = array(); while($row = mysql_fetch_assoc($mysql_array)){ $array[] = $row; } $array = array_unique($array); $array

  • How to add elements to an empty array in PHP? 2009-03-24

    If I define an array in PHP such as (I don't define its size): $cart = array(); Do I simply add elements to it using the following? $cart[] = 13; $cart[] = "foo"; $cart[] = obj; Don't arrays in PHP have an add method, for example, cart.add(13)? -----

  • How to store MySql values as Multidimensional Array using PHP 2010-08-30

    I have a database table as follows. <table border='1'><th>Id</th><th>FirstName</th><th>last Name</th><tr><td>1</td><td>Tom</td><td>T</td></tr><tr><td

  • How to create an empty array in PHP with predefined size? 2011-03-22

    I am creating a new array in a for loop. for $i < $number_of_items $data[$i] = $some_data; PHP keeps complaining about the offset since for each iteration I add a new index for the array, which is kind of stupid. Notice: Undefined offset: 1 in inc

  • Is there a function in PHP that will remove empty cells from an array? 2011-08-02

    After merging several arrays I run into the issue of having undeclared cells in an array. For example: Array ( [0] => 713 [1] => 714 [4] => 712 [6] => 428 [7] => 711 [8] => 515 [9] => 645 ) I have written a function to rewrite th

  • How to remove duplicate values from an array in PHP 2008-11-21

    How can I remove duplicate values from an array in PHP? --------------Solutions------------- Use array_unique(). Example: $array = array(1, 2, 2, 3); $array = array_unique($array); // Array is now (1, 2, 3) //Find duplicates $arr = array( 'unique', '

  • How do you reindex an array in PHP? 2009-02-26

    I have the following array, which I would like to reindex so the keys are reversed (ideally starting at 1): Current array (edit: the array actually looks like this): Array ( [2] => Object ( [title] => Section [linked] => 1 ) [1] => Object

  • Best practice: Import mySQL file in PHP; split queries 2009-12-10

    I have a situation where I have to update a web site on a shared hosting provider. The site has a CMS. Uploading the CMS's files is pretty straightforward using FTP. I also have to import a big (relative to the confines of a PHP script) database file

  • Creating a mySQL query using PHP form dropdowns - If user ignores dropdown, do not filter by that parameter 2010-03-27

    I am creating a simple MySQL query that will be built from the user selecting options from 2 dropdowns. The issue I am having is that I would like each drop down to default that if they do not actually choose an option, do not filter by that dropdown

  • Filling an evenly distributed matrix of arrays with PHP 2010-10-18

    I have an array of objects, containing lists of times. I have to stick this in a Matrix-alike array, in order to put it in a table. This is a part of my earlier SO question on how to build calendar tables $node->sessions = array( 1286452800, // '2

  • Extracting an ID number from a MySQL Database using PHP 2011-05-17

    I have a PHP script in the works which is going to end up, checking a database twice, once to see if the user has registered, and once to get a user ID (if someone has registered) I have that bit working (you can find the code below) But the value th

  • How to parse csv file into mysql DB with PHP with conditions 2011-06-27

    how do you parse a csv file into a mysql database with php, From bits I have read from php documents I am not sure how or if its possible to add conditions to the lines that you are reading into the database so that only values that meet the conditio

  • Arranging data in arrays using PHP 2012-02-06

    For some reason I having a hard time trying to arrange my data in arrays. Now I have a database that holds the title of a page, the content, id and date modified. I have a function that calls onto another function that does a query and gets all the t

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.191 (s). 12 q(s)