Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #14495
| From | Matthew Carter <m@ahungry.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Pulling ad-hoc data from a database into a PHP web page |
| Date | 2014-11-06 10:44 -0500 |
| Organization | Ahungry (http://ahungry.com) |
| Message-ID | <87oaskxriy.fsf@ahungry.com> (permalink) |
| References | <m3ef2b$u9h$1@dont-email.me> <m3erom$ri2$1@dont-email.me> <m3g44n$ve0$1@dont-email.me> |
"James Harris" <james.harris.1@gmail.com> writes:
> "Jerry Stuckle" <jstucklex@attglobal.net> wrote in message
> news:m3erom$ri2$1@dont-email.me...
>> On 11/5/2014 7:23 PM, 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. Some of the ad-hoc info will be
>>> scalars. Some will be lists and, possibly, some will be tabular. Is there
>>> already a standard way to do things like this?
>>>
>>> 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. A call to
>>> the scalar function would be something like
>>>
>>> scalar_get("key")
>>>
>>> where the key uniquely identifies a cell in a table. The subroutine would
>>> just echo the result into the output html that gets sent to the web
>>> browser.
>>> Is that reasonable 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?
>>>
>>> James
>>>
>>>
>>
>> First of all, there is not "cell" in a SQL database table. You get one
>> or more rows, and can select columns from that row. If you have a
>> primary key, you will get a single row back by selecting on that key.
>
> Would "field" be better? Whatever the case I mean the intersection of a row
> and a column of a table, i.e. a specific field in a specific record where
> the record is idenfitied by a unique key.
>
> To illustrate what I was asking in my original post I have come up with a
> function to retrieve scalars. It works but I am concerned that it may be
> inefficient due to reissuing the database-connect call each time. Is there a
> better way to write this so that the connection is opened once and just
> reused as many times as the function is called? Would making the connection
> a gobal be the way to do it?
>
> function scalar_get($key) {
> global $sv, $db, $un, $pw;
> echo "In function scalar_get($key)<br/>";
> echo "Make db connection to $db<br/>";
>
> $conn = mysqli_connect($sv, $un, $pw);
> if (!$conn) {
> die("Database connection failed: " . mysqli_connect_error());
> }
> echo "Database connected successfully<br/>";
>
> $sql = "select value from $db.scalars where id = \"$key\";";
> $result = mysqli_query($conn, $sql);
> if ($result === FALSE) {
> echo "Query ($sql) failed: " . mysqli_error($conn) . "<br/>";
> } elseif (mysqli_num_rows($result) !== 1) {
> echo "Expected one row, got " . mysqli_num_rows($result) . "<br/>";
> } else {
> echo "Got one row<br/>";
> $row = mysqli_fetch_row($result);
> echo "Result is ($row[0])<br/>";
> }
> }
>
> The idea is that a function like the above (but less verbose!) would be
> called an arbitrary number of times in the main body of a page by means of
> calls such as the following. At least for scalars I may get it to return the
> string instead of printing it.
>
> scalar_get("forename");
> echo "<br/>";
> scalar_get("surname");
> echo "<br/>";
>
>> As for getting multiple rows - I normally select all rows of interest,
>> and display them in a loop. That is much more efficient than retrieving
>> multiple rows based on the selection criteria used to retrieve the
>> primary keys.
>
> Understood.
>
> James
>
>
I would either:
- Wrap the function in a class and store the DB connection as a class
variable
- Pass the connection as an additional parameter to the function
- If you insist on including a 'global' line in there, use the
connection as the global (instead of the name/pass etc.)
Opening a database each time you want to do a single query is more
inefficient than opening and keeping open.
Is your database going to be two column like this?
--------------
| id | value |
--------------
If so, you could probably make 'id' your unique key and omit your elseif
clause in the row checking snippet (it seems like having duplicate ids
would break the manner in which you want to query this information).
--
Matthew Carter (m@ahungry.com)
http://ahungry.com
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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