php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42961 mysqli would not return result values within "for" loop.
Submitted: 2007-10-14 11:20 UTC Modified: 2007-10-16 03:49 UTC
From: wilsonwg at gmail dot com Assigned:
Status: Closed Package: MySQLi related
PHP Version: 5.2.4 OS: Windows XP SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: wilsonwg at gmail dot com
New email:
PHP Version: OS:

 

 [2007-10-14 11:20 UTC] 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'


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-14 11:24 UTC] 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:26 UTC] wilsonwg at gmail dot com
Oops, and the second last line in my code should be:

  $id = $result['id'];
 [2007-10-15 10:02 UTC] 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-16 03:49 UTC] wilsonwg at gmail dot com
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!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC