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);
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()?]]>jwcarlton2012-11-29T08:51:04-00:00Re: Manually create array that matches mysql_fetch_array
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=179730&th=122882#msg_179730
> 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.]]>Captain Paralytic2012-11-29T10:07:31-00:00Re: Manually create array that matches mysql_fetch_array
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=179731&th=122882#msg_179731
> 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?-]]>Norman Peelman2012-11-29T13:35:30-00:00Re: Manually create array that matches mysql_fetch_array
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=179732&th=122882#msg_179732
> 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.]]>Peter H. Coffin2012-11-29T14:24:10-00:00Re: Manually create array that matches mysql_fetch_array
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=179733&th=122882#msg_179733
> 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]]>Denis McMahon2012-11-29T20:26:28-00:00Re: Manually create array that matches mysql_fetch_array
http://fudforum.org/forum/index.phpindex.php?t=rview&goto=179734&th=122882#msg_179734
> put copies of it into the array.