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
Return to the default flat view Create a new topic Submit Reply
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 previous message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma:
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?-
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
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 28 10:12:10 GMT 2024

Total time taken to generate the page: 0.04555 seconds