PHP MYSQL array issue

Hello,
I am facing a problem with mysqli_fetch_array(); in the below code
I have stored in

$row['items1'] is 1,2,3,4

[php]<?php

$host = ‘localhost’;
$pass = ‘’;
$user = ‘root’;
$db = ‘test’;

$con=mysqli_connect($host,$user,$pass,$db);

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,“SELECT * FROM tstraffle”);

while($row = mysqli_fetch_array($result))
{
$items = mysqli_fetch_array($row[‘item1’],MYSQLI_NUM);
}
echo $items[1];

?>[/php]

i want to get all of my items1 in an array.
Thanks

$items[] = mysqli_fetch_array($row[‘item1’],MYSQLI_NUM);

Thanks for the quick responce
But in $items[0] its storing all of the values
i want like this

$items[0] = 1;
$items[1] = 2;
$items[2] = 3;
$items[3] = 4;

So, you have multiple numbers stored in one column in the DB? If so, you likely have a bad database design. Post the SQL schema of your tables.

If they are all in one column you will need to use explode

$exploded_items = explode(’,’, $items);

echo $exploded_items[0];
echo $exploded_items[1];

http://share.cx.com/SMSWdz

See my updated post above…

You have a bad database design. You need another table for items with a foreign key that connects the data to the tstraffle table. If you had a hundred items, you would have two hundred columns just for items instead of just the only TWO columns that are actually needed.

How can i do that i can make another table and make it as foreign key but how to insert 5 items and echo those 5 items
Thanks

Post an actual sql dump of your table and I will do it for you. Its very simple. (Not a pdf, a file I can import into mysql)

[code]-- phpMyAdmin SQL Dump
– version 4.1.6
http://www.phpmyadmin.net

– Host: localhost
– Generation Time: Mar 11, 2014 at 03:15 PM
– Server version: 5.6.16
– PHP Version: 5.5.9

SET SQL_MODE = “NO_AUTO_VALUE_ON_ZERO”;
SET time_zone = “+00:00”;

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT /;
/
!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS /;
/
!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION /;
/
!40101 SET NAMES utf8 */;


– Database: test



– Table structure for table tstraffle1

CREATE TABLE IF NOT EXISTS tstraffle1 (
RID int(11) NOT NULL AUTO_INCREMENT,
item1 varchar(16) DEFAULT NULL,
info1 varchar(32) DEFAULT NULL,
nameofraffle varchar(30) NOT NULL,
slots varchar(16) DEFAULT NULL,
noofpeople varchar(32) DEFAULT NULL,
duration varchar(32) DEFAULT NULL,
otherinfo varchar(32) DEFAULT NULL,
entfees varchar(32) DEFAULT NULL,
entinfo varchar(255) DEFAULT NULL,
PRIMARY KEY (RID)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;


– Dumping data for table tstraffle1

INSERT INTO tstraffle1 (RID, item1, info1, nameofraffle, slots, noofpeople, duration, otherinfo, entfees, entinfo) VALUES
(1, ‘172,173,174’, ‘Unique1,Unique2,Unique3’, ‘My First raffle’, ‘12’, ‘12’, ‘36’, ‘Please Do it fast’, ‘222’, ‘Please do it fast’);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT /;
/
!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS /;
/
!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
[/code]

[php]SELECT * from tstraffle r, items i WHERE r.RID =1 AND i.raffle_id=1[/php]

[php]/*
Navicat MySQL Data Transfer

Source Server : Localhost
Source Server Version : 50523
Source Host : localhost:3306
Source Database : raffle

Target Server Type : MYSQL
Target Server Version : 50523
File Encoding : 65001

Date: 2014-03-11 10:42:18
*/

SET FOREIGN_KEY_CHECKS=0;


– Table structure for items


DROP TABLE IF EXISTS items;
CREATE TABLE items (
item_id int(11) NOT NULL AUTO_INCREMENT,
raffle_id int(11) DEFAULT NULL,
item varchar(255) DEFAULT NULL,
item_info varchar(255) DEFAULT NULL,
PRIMARY KEY (item_id),
KEY raffle_id (raffle_id),
CONSTRAINT items_ibfk_1 FOREIGN KEY (raffle_id) REFERENCES tstraffle (RID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;


– Records of items


INSERT INTO items VALUES (‘1’, ‘1’, ‘172’, ‘Unique1’);
INSERT INTO items VALUES (‘2’, ‘1’, ‘173’, ‘Unique2’);
INSERT INTO items VALUES (‘3’, ‘1’, ‘174’, ‘Unique3’);


– Table structure for tstraffle


DROP TABLE IF EXISTS tstraffle;
CREATE TABLE tstraffle (
RID int(11) NOT NULL AUTO_INCREMENT,
nameofraffle varchar(30) NOT NULL,
slots varchar(16) DEFAULT NULL,
noofpeople varchar(32) DEFAULT NULL,
duration varchar(32) DEFAULT NULL,
otherinfo varchar(32) DEFAULT NULL,
entfees varchar(32) DEFAULT NULL,
entinfo varchar(255) DEFAULT NULL,
PRIMARY KEY (RID)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;


– Records of tstraffle


INSERT INTO tstraffle VALUES (‘1’, ‘My First raffle’, ‘12’, ‘12’, ‘36’, ‘Please Do it fast’, ‘222’, ‘Please do it fast’);
[/php]

Thanks +Karma

Sponsor our Newsletter | Privacy Policy | Terms of Service