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


Groups > comp.lang.php > #7662

Re: Manually create array that matches mysql_fetch_array

From Norman Peelman <npeelmandog@cfl.rr.com>
Newsgroups comp.lang.php
Subject Re: Manually create array that matches mysql_fetch_array
Date 2012-11-29 08:35 -0500
Organization A noiseless patient Spider
Message-ID <k97ob4$6j3$1@dont-email.me> (permalink)
References <0fc98fc4-0d67-4e62-a598-200eefcec965@googlegroups.com>

Show all headers | View raw


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?-

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


Thread

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

csiph-web