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


Groups > comp.lang.php > #7660 > unrolled thread

Manually create array that matches mysql_fetch_array

Started byJason C <jwcarlton@gmail.com>
First post2012-11-29 00:51 -0800
Last post2012-11-30 00:23 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.php


Contents

  Manually create array that matches mysql_fetch_array Jason C <jwcarlton@gmail.com> - 2012-11-29 00:51 -0800
    Re: Manually create array that matches mysql_fetch_array Captain Paralytic <paul_lautman@yahoo.com> - 2012-11-29 02:07 -0800
    Re: Manually create array that matches mysql_fetch_array Norman Peelman <npeelmandog@cfl.rr.com> - 2012-11-29 08:35 -0500
    Re: Manually create array that matches mysql_fetch_array "Peter H. Coffin" <hellsop@ninehells.com> - 2012-11-29 08:24 -0600
    Re: Manually create array that matches mysql_fetch_array Denis McMahon <denismfmcmahon@gmail.com> - 2012-11-29 20:26 +0000
      Re: Manually create array that matches mysql_fetch_array Denis McMahon <denismfmcmahon@gmail.com> - 2012-11-30 00:23 +0000

#7660 — Manually create array that matches mysql_fetch_array

FromJason C <jwcarlton@gmail.com>
Date2012-11-29 00:51 -0800
SubjectManually create array that matches mysql_fetch_array
Message-ID<0fc98fc4-0d67-4e62-a598-200eefcec965@googlegroups.com>
In my script, I'm potentially creating information to insert in to MySQL (based on a few variables), then later in the script I'm SELECTing the same data back out of MySQL. Like this:

if ($_GET['whatever']) {
  $query = "REPLACE INTO...";
  mysql_query($query);
}

$s_query = "SELECT ...";
$sql = mysql_query($s_query);

while (list(...) = mysql_fetch_array($sql)) {
  // show data
}

This is obviously redundant, though, so I'm trying to find a way to create $sql in the first if() statement, so that the SELECT statement isn't necessary if it already exists.

Like:

if ($_GET['whatever']) {
  $query = "REPLACE INTO...";
  mysql_query($query);

  $sql = ???;
}

if (!$sql) {
  $s_query = "SELECT ...";
  $sql = mysql_query($s_query);
}

while (list(...) = mysql_fetch_array($sql)) {
  // show data
}


I know that I could create a regular array, then after the conditional SELECT statement use a loop to push the data in to the array. But what I'm curious about is if it's possible to just manually create the same type of array as mysql_query()?

[toc] | [next] | [standalone]


#7661

FromCaptain Paralytic <paul_lautman@yahoo.com>
Date2012-11-29 02:07 -0800
Message-ID<076b5c59-a870-41b5-951b-a5012939384c@n5g2000vbk.googlegroups.com>
In reply to#7660
On Nov 29, 8:51 am, Jason C <jwcarl...@gmail.com> wrote:
> In my script, I'm potentially creating information to insert in to MySQL (based on a few variables), then later in the script I'm SELECTing the same data back out of MySQL. Like this:
>
> if ($_GET['whatever']) {
>   $query = "REPLACE INTO...";
>   mysql_query($query);
>
> }
>
> $s_query = "SELECT ...";
> $sql = mysql_query($s_query);
>
> while (list(...) = mysql_fetch_array($sql)) {
>   // show data
>
> }
>
> This is obviously redundant, though, so I'm trying to find a way to create $sql in the first if() statement, so that the SELECT statement isn't necessary if it already exists.
>
> Like:
>
> if ($_GET['whatever']) {
>   $query = "REPLACE INTO...";
>   mysql_query($query);
>
>   $sql = ???;
>
> }
>
> if (!$sql) {
>   $s_query = "SELECT ...";
>   $sql = mysql_query($s_query);
>
> }
>
> while (list(...) = mysql_fetch_array($sql)) {
>   // show data
>
> }
>
> I know that I could create a regular array, then after the conditional SELECT statement use a loop to push the data in to the array. But what I'm curious about is if it's possible to just manually create the same type of array as mysql_query()?

Have you read the manual?
mysql_query() doesn't return an array.

[toc] | [prev] | [next] | [standalone]


#7662

FromNorman Peelman <npeelmandog@cfl.rr.com>
Date2012-11-29 08:35 -0500
Message-ID<k97ob4$6j3$1@dont-email.me>
In reply to#7660
On 11/29/2012 03:51 AM, Jason C wrote:
> In my script, I'm potentially creating information to insert in to MySQL (based on a few variables), then later in the script I'm SELECTing the same data back out of MySQL. Like this:
>
> if ($_GET['whatever']) {
>    $query = "REPLACE INTO...";
>    mysql_query($query);
> }
>
> $s_query = "SELECT ...";
> $sql = mysql_query($s_query);
>
> while (list(...) = mysql_fetch_array($sql)) {
>    // show data
> }
>
> This is obviously redundant, though, so I'm trying to find a way to create $sql in the first if() statement, so that the SELECT statement isn't necessary if it already exists.
>
> Like:
>
> if ($_GET['whatever']) {
>    $query = "REPLACE INTO...";
>    mysql_query($query);
>
>    $sql = ???;
> }
>
> if (!$sql) {
>    $s_query = "SELECT ...";
>    $sql = mysql_query($s_query);
> }
>
> while (list(...) = mysql_fetch_array($sql)) {
>    // show data
> }
>
>
> I know that I could create a regular array, then after the conditional SELECT statement use a loop to push the data in to the array. But what I'm curious about is if it's possible to just manually create the same type of array as mysql_query()?
>

   MySQL doesn't return an array, it returns a Resource Identifier... so 
that won't work. In the first IF you need to create $sql as an array of 
the data that you have (as mysql_fetch_array() would return it), and 
change the later code to something like:

if (!$sql) {
     // $sql NOT set or is empty "", let's read from the db
     $s_query = "SELECT ...";
     $sql = mysql_query($s_query);
     while (list(...) = mysql_fetch_array($sql)) {
     // show data
     }
} else {
    // $sql is populated with something, let's not read from the db
    while (list(...) = $sql)
    // show data
}

...of course you should be doing some error checking to make sure that:

#1 - $sql starts out empty, in this case $sql = array()
#2 - make sure $sql is what you expect it to be - an array
#3 - error checking to make sure your queries are completing

   All that being said, what's wrong with just reading info back from 
the db?

-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#7663

From"Peter H. Coffin" <hellsop@ninehells.com>
Date2012-11-29 08:24 -0600
Message-ID<slrnkbes0a.4ja.hellsop@nibelheim.ninehells.com>
In reply to#7660
On Thu, 29 Nov 2012 00:51:04 -0800 (PST), Jason C wrote:

> In my script, I'm potentially creating information to insert in
> to MySQL (based on a few variables), then later in the script I'm
> SELECTing the same data back out of MySQL. Like this:

Stop a moment, back up, and start by describing what you want to
ACCOMPLISH, not what you want to do. Because it sounds like you have
Some Data (A + B), some of which (may) exist in a database (B + U), and
you want to have access to the most current of all of it (A + B + U)
efficiently. If that's the case, then it's not really redundant at all;
REPLACE INTO... (or, depending on your design, INSERT ... ON DUPLICATE
... ) is the proper path, because something else could change part of
the data you thought "correct" between the time that you do the insert
and the time you get to pulling your result set (which should be right
before you want to use it).

-- 
23. I will keep a special cache of low-tech weapons and train my troops
   in their use. That way--even if the heroes manage to neutralize my 
   standard-issue energy weapons useless--my troops will not be overrun
   by a handful of savages armed with spears.

[toc] | [prev] | [next] | [standalone]


#7664

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-11-29 20:26 +0000
Message-ID<k98gdk$tuf$1@dont-email.me>
In reply to#7660
On Thu, 29 Nov 2012 00:51:04 -0800, Jason C wrote:

> In my script, I'm potentially creating information to insert in to MySQL
> (based on a few variables), then later in the script I'm SELECTing the
> same data back out of MySQL.

If all of the data you select when you read from the database is in the 
the $_GET superglobal array, then it is still there after the query has 
put copies of it into the array.

However, if the subsequent select might pick mother data that you want 
which was not in the initial input and hence wasn't part of the "replace 
into" sql, then stick with the resource from the select query.

Also, as someone else has pointed out, your query result isn't presented 
as an array and can not be handled as such, although the data it contains 
can be thought of as a 2d array.

Rgds

Denis McMahon

[toc] | [prev] | [next] | [standalone]


#7665

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2012-11-30 00:23 +0000
Message-ID<k98u9h$7t3$1@dont-email.me>
In reply to#7664
On Thu, 29 Nov 2012 20:26:28 +0000, Denis McMahon wrote:

> put copies of it into the array.

put copies of it into the *database*.

Rgds

Denis McMahon

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web