Jump to content

How to ready all Db table in to array


jasonc

Recommended Posts

CREATE TABLE `info` ( `id` int(11) NOT NULL, `section` text NOT NULL, `content` text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

The table below is what I have setup but having issues accessing the content based on the section needed.

I have tried...
$result['sectionname']['content']
but nothing shows.
What is the correct way to access each content using the section. without doing it one query for each one.

Link to comment
Share on other sites

You title makes no sense and your code is nothing at all.  What are you trying to learn???

Are you simply asking how to read an entire table?  Very simple to do.  But do you really need all of the columns in all of the rows?  As for getting the data "in to an array", well, that is basically how a query result is returned.   One can simply do a fetchall to grab all the results in one swoop but most of the time one processes the query results by using a fetch and a loop to process each row.

As for your create - you might want to use the varchar type instead of the text type.  And do you really want to use the 'latin1' charset over the more usual 'utf8' one?

As for your last question - you haven't shown us your query so kinda hard to help you with that.  Although it sounds like you just need to use the order by clause.

I have given you a lot to answer so I patiently await your thoughtful response.

Edited by ginerjm
Link to comment
Share on other sites

	$fetch = db_query($mysqli, "SELECT * FROM `info` WHERE `section` = 'openingtimes'");
	$result = $fetch->fetch_assoc();
	$openingtimes = $result['content'];
	$openingtimes = str_replace("\r\n", "
	<br>
	", $openingtimes);
	
	$fetch = db_query($mysqli, "SELECT * FROM `info` WHERE `section` = 'foodtimes'");
	$result = $fetch->fetch_assoc();
	$foodtimes = $result['content'];
	$foodtimes = str_replace("\r\n", "
	<br>
	", $foodtimes);
	?>
	<?php
	echo($openingtimes); ?><br><br><?php
	echo($foodtimes);?><br><br>

Instead of accessing the DB twice only access once.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.