FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Manually create array that matches mysql_fetch_array
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Manually create array that matches mysql_fetch_array [message #179729] Thu, 29 November 2012 08:51 Go to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
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()?
Re: Manually create array that matches mysql_fetch_array [message #179730 is a reply to message #179729] Thu, 29 November 2012 10:07 Go to previous messageGo to next message
Captain Paralytic is currently offline  Captain Paralytic
Messages: 204
Registered: September 2010
Karma: 0
Senior Member
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.
Re: Manually create array that matches mysql_fetch_array [message #179731 is a reply to message #179729] Thu, 29 November 2012 13:35 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
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?-
Re: Manually create array that matches mysql_fetch_array [message #179732 is a reply to message #179729] Thu, 29 November 2012 14:24 Go to previous messageGo to next message
Peter H. Coffin is currently offline  Peter H. Coffin
Messages: 245
Registered: September 2010
Karma: 0
Senior Member
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.
Re: Manually create array that matches mysql_fetch_array [message #179733 is a reply to message #179729] Thu, 29 November 2012 20:26 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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
Re: Manually create array that matches mysql_fetch_array [message #179734 is a reply to message #179733] Fri, 30 November 2012 00:23 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Query a Array Field in MySQL
Next Topic: Experience Web and Smartphone designer required
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 21 21:25:28 GMT 2024

Total time taken to generate the page: 0.02742 seconds