#42961 [Fbk->Csd]: mysqli would not return result values within "for" loop.

From: Date: Tue, 16 Oct 2007 03:49:06 +0000
Subject: #42961 [Fbk->Csd]: mysqli would not return result values within "for" loop.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-118578@lists.php.net to get a copy of this message
ID: 42961 User updated by: wilsonwg at gmail dot com Reported By: wilsonwg at gmail dot com -Status: Feedback +Status: Closed Bug Type: MySQLi related Operating System: Windows XP SP2 PHP Version: 5.2.4 New Comment: Hi: Yes, you were right: after a close look of my much longer codes, I found that the error was caused by some typos on variable names along the lines. This is a simple error but it stopped my coding for many days already. Anyway, thanks for the suggestion for solving my problem! Previous Comments: ------------------------------------------------------------------------ [2007-10-15 10:02:10] jani@php.net Try turning on error_reporting = E_ALL and/or add some checks in your code for errors. Similar stuff works just fine for me so I think you just have some problem with your query. ------------------------------------------------------------------------ [2007-10-14 11:26:29] wilsonwg at gmail dot com Oops, and the second last line in my code should be: $id = $result['id']; ------------------------------------------------------------------------ [2007-10-14 11:24:35] wilsonwg at gmail dot com I forgot to add under score in my result sections. SELECT * FROM compounds where compound_name = 'item 1' Should be: SELECT * FROM compounds where compound_name = 'item_1' ------------------------------------------------------------------------ [2007-10-14 11:20:13] wilsonwg at gmail dot com Description: ------------ I set up an array in the code and then use the for loop to query my database. However, the mysqli functions would not return the result values. This is the table used for the query(columns separated a comma): ------------- id, item_name ------------- 1, item_1 2, item_2 3, item_3 4, item_4 ------------- Reproduce code: --------------- $mysqli_connect =mysqli_connect("localhost", "root", "password", "myDB"); $array_items = "item_1,item_2,item_3,item_4"; $array_list = explode (",", $array_items); for ($i=0; $i<count($array_list); $i++){ $select_item = "SELECT * FROM compounds where item_name = '".$array_list[$i]."'"; echo "$select_item <p>"; $search_item_query = mysqli_query($mysqli_connect, $select_item); $result = mysqli_fetch_array($search_item_query); $id = $result['item_id']; echo "$id <p>"; } Expected result: ---------------- SELECT * FROM compounds where compound_name = 'item 1' 1 SELECT * FROM compounds where compound_name = 'item 2' 2 SELECT * FROM compounds where compound_name = 'item 3' 3 SELECT * FROM compounds where compound_name = 'item 4' 4 Actual result: -------------- SELECT * FROM compounds where compound_name = 'item 1' SELECT * FROM compounds where compound_name = 'item 2' SELECT * FROM compounds where compound_name = 'item 3' SELECT * FROM compounds where compound_name = 'item 4' ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=42961&edit=1

« previous php.bugs (#118578) next »