Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14508

Re: Pulling ad-hoc data from a database into a PHP web page

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: Pulling ad-hoc data from a database into a PHP web page
Date 2014-11-06 21:34 +0100
Organization PointedEars Software (PES)
Message-ID <4030537.OAdHOdONia@PointedEars.de> (permalink)
References <m3ef2b$u9h$1@dont-email.me> <9211084.ztrPHf13qF@PointedEars.de> <m3g5qi$6td$1@dont-email.me>

Show all headers | View raw


James Harris wrote:

> "Thomas 'PointedEars' Lahn" […] wrote:
>> James Harris wrote:
>>> I am looking for a good way to pull ad-hoc info from a database into a
>>> web page. By "good" I mean a way that is efficient for the server and
>>> convenient for the person writing the web page.
>> You can't have your cake and eat it.
> 
> That's too vague to reply to. Maybe I want icing and a cherry on it
> too....

(You still could not do it.  Once eaten it would be gone.)

OK, in non-idiomatic English: The goals you have set are mutually exclusive.  
The most efficient way for the server is if you issue a pure database query.  
The most convenient for the person writing the “web page” (there is no such 
thing) is if you do not do that, because you would have to hide from them 
the complexity that you have to implement elsewhere so that it looks simple 
*to them*.
 
>>> Some of the ad-hoc info will be scalars. Some will be lists and,
>>> possibly, some will be tabular.
>> With regard to data, a list is a table with one column.
> 
> With regard to data, yes.

So as this is about “pulling *data*” (from a relational database), the 
distinction you have made is purely artificial.  Data and presentation of 
data (which raises questions like “Do I use an HTML ‘ul’ element or a 
‘table‘ element?”) are separate things.
 
>>> Is there already a standard way to do things like this?
>> Yes.
> 
> Which is...?

There *are* “standard” ways to make database queries.  You are not currently 
employing any.  As a training course about how to properly write a database-
aware application in PHP is far beyond the scope of this newsgroup, I 
suggest you RTFM and STFW first, and come back with more specific questions 
later.  (And double-check against that all replies you receive in the 
meantime.)

But it turns out that “things like this” is too vague to reply to in the 
first place.  And too contradicting.
 
>>> It's been a whiile since I used PHP but from a quick look it seems it
>>> may be best to define a number of functions to retrieve and echo data.
>>
>> Three at most.
> 
> I probably agree that three would be a good target but that will depend on
> how I retrieve the data.

Yes, I was talking only about the entry points, given your very short 
specifications.  The other "functions" called by those methods would 
actually be methods, because of OOP – if you used any non-methods in the 
first place.

> I would like to be able to pull back a whole table (or a subset of rows
> thereof) from the database

There is a corresponding function and method with mysqli, and a 
corresponding method with PDO.  You will find it easily in the PHP manual if 
you replace “pull back” with the more appropriate term “fetch”.  And keep in 
mind that you can filter arrays.

> and include it in the web page with a single call so as to keep the page
> source simple and easy for people to use. If I do that then the function
> which pulls the data has to know how to format it

No, it does not.  Not separating data and presentation of data is your third 
mistake.

> - and formats may vary.

So do not do that.  Either include the formatting in the template or have 
*another* function/method called in the template that uses the retrieved 
data and does the formatting for you/the template author.  Preferably by 
include, unless the data to format is atomic.

> There may be something I can do with templates and default parameters to
> simplify this.

There is (there are many, actually).  Why do you not do *some* research and 
*try* *before* you ask?
 
> As a good compromise there may be a way to specify the layout of a table
> as three functions: header, each row, and footer. Again, am not sure yet.

You are right not to be sure; this is not a sound (or efficient) approach.  
Read on includes instead (that is not a new thing at all; one must wonder at 
this point if you have done *any* PHP development before).
 
>>> A call to the scalar function would be something like
>>>
>>>   scalar_get("key")
>>>
>>> where the key uniquely identifies a cell in a table.
>> You mean a _column_.
> 
> No, I mean a cell (or a field of a record, if you prefer).

I do.  A database table is _not_ like a spreadsheet.

> The key would-be a unique identifier for the scalar that I want to embed 
> in the page, not a key in a database sense.

That *is* the definition of a database table key.  You should also 
(re-)learn basic database theory first.
 
>>> Is that reasonable
>> No.
> 
> IYO.

What else could it be?
 
>>> and is there a way to make the subroutine efficient given that there may
>>> be many scalar_get() calls in the body of the page?
>>
>> Mu. [1]
> 
> ...
> 
>> [1] You can cache query results so that you do not have to issue a new
>> query
>>    when you need data from the same data set.  But the notion of "ad-hoc
>>    data" contradicts with that approach; "ad-hoc data" changes.
> 
> I was more concerned with reusing the connection to the database.

The proper way, the “standard way” if you will, is to use a class whose 
instance encapsulates all database-relevant data, including the connection 
resource, and actions.

> If you could take a look at the reply I made to Jerry Stuckle

In the first reply you made to Jerry Stuckle you demonstrated behavior 
indicating that it is not worthwhile investing (my) free time into your 
problems as you only laughed at the good advice that I gave you.  I do not 
see that this has changed much.  Neither do my replies to you.

However, from your other reply ISTM you are unaware of class-based object-
oriented programming in PHP or in general, which is basic knowledge by now.  
So if you want a robust solution, you should learn that first (after which 
you will discover that this wheel has already been invented by Zend, the PHP 
company).  After that, you want to look into PDO and Prepared Statement to 
avoid the SQL injection that your code currently is susceptible to.

> I included in it a working function that illustrates the problem. In terms
> of scalars - which the posted code illustrates - I guess I would want to
> connect to the database and prepare any queries once and then use the
> queries an arbitrary number of times in the page.

Reasonable.  See the paragraph above for that.

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Pulling ad-hoc data from a database into a PHP web page "James Harris" <james.harris.1@gmail.com> - 2014-11-06 00:23 +0000
  Re: Pulling ad-hoc data from a database into a PHP web page Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-06 04:54 +0100
    Re: Pulling ad-hoc data from a database into a PHP web page Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-06 09:19 -0500
      Re: Pulling ad-hoc data from a database into a PHP web page "James Harris" <james.harris.1@gmail.com> - 2014-11-06 15:39 +0000
        Re: Pulling ad-hoc data from a database into a PHP web page Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-06 11:32 -0500
          Re: Pulling ad-hoc data from a database into a PHP web page "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2014-11-26 10:26 +0100
            Re: Pulling ad-hoc data from a database into a PHP web page Tim Streater <timstreater@greenbee.net> - 2014-11-26 09:38 +0000
              Re: Pulling ad-hoc data from a database into a PHP web page "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2014-11-26 11:56 +0100
            Re: Pulling ad-hoc data from a database into a PHP web page Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-26 07:51 -0500
              Re: Pulling ad-hoc data from a database into a PHP web page "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2014-11-26 14:24 +0100
                Re: Pulling ad-hoc data from a database into a PHP web page Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-26 08:36 -0500
                Re: Pulling ad-hoc data from a database into a PHP web page Matthew Carter <m@ahungry.com> - 2014-11-26 10:21 -0500
                Re: Pulling ad-hoc data from a database into a PHP web page "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2014-11-26 20:57 +0100
                Re: Pulling ad-hoc data from a database into a PHP web page Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-26 16:21 -0500
        Re: Pulling ad-hoc data from a database into a PHP web page "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-06 18:27 +0100
    Re: Pulling ad-hoc data from a database into a PHP web page "James Harris" <james.harris.1@gmail.com> - 2014-11-06 15:58 +0000
      Re: Pulling ad-hoc data from a database into a PHP web page Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-06 21:34 +0100
  Re: Pulling ad-hoc data from a database into a PHP web page Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-05 23:00 -0500
    Re: Pulling ad-hoc data from a database into a PHP web page "James Harris" <james.harris.1@gmail.com> - 2014-11-06 15:29 +0000
      Re: Pulling ad-hoc data from a database into a PHP web page Matthew Carter <m@ahungry.com> - 2014-11-06 10:44 -0500
        Re: Pulling ad-hoc data from a database into a PHP web page "James Harris" <james.harris.1@gmail.com> - 2014-11-06 16:15 +0000
          Re: Pulling ad-hoc data from a database into a PHP web page Matthew Carter <m@ahungry.com> - 2014-11-06 11:25 -0500
            Re: Pulling ad-hoc data from a database into a PHP web page Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-06 12:42 -0500
            Re: Pulling ad-hoc data from a database into a PHP web page Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-08 13:50 +0100
              Re: Pulling ad-hoc data from a database into a PHP web page Matthew Carter <m@ahungry.com> - 2014-11-08 21:19 -0500
                Re: Pulling ad-hoc data from a database into a PHP web page Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-08 22:08 -0500
                Re: Pulling ad-hoc data from a database into a PHP web page Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-09 08:09 +0100
          Re: Pulling ad-hoc data from a database into a PHP web page "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-06 18:24 +0100
            Re: Pulling ad-hoc data from a database into a PHP web page Matthew Carter <m@ahungry.com> - 2014-11-06 12:37 -0500
            Re: Pulling ad-hoc data from a database into a PHP web page "James Harris" <james.harris.1@gmail.com> - 2014-11-06 17:51 +0000
              Re: Pulling ad-hoc data from a database into a PHP web page "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-06 20:56 +0100
      Re: Pulling ad-hoc data from a database into a PHP web page Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-06 12:40 -0500
      Re: Pulling ad-hoc data from a database into a PHP web page richard <noreply@example.com> - 2014-11-10 11:17 -0500
        Re: Pulling ad-hoc data from a database into a PHP web page Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-10 21:27 +0000

csiph-web