Page 1 of 1

How to get data from mySQL and insert it in Array?

Posted: Thu May 20, 2004 6:42 am
by rodman
My mySQL table is on one server but PHP file which I want insert some info from this mySQL table is on another server. And also I need insert this information into Array in PHP file.

I know how to conect to mySQL server and how to get info from there and write it but I do not know how to insert it into Array in PHP file spesially when there is already some info in this Array from before.
So actually I need to update my Array with new info from mySQL table.


Here is what I got so far:

<?php
/* Connecting, selecting database */
$link = mysql_connect("www.domain.com:80/", "user", "pass")
or die("Could not connect : " . mysql_error());
echo "Connected successfully";
mysql_select_db("helpdemo_xcart") or die("Could not select database");

/* Performing SQL query */
$query = "SELECT * FROM xcart_customers";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

/* Inserting results in to PHP file */

$line = mysql_fetch_array($result, MYSQL_ASSOC)) ;

$toSave = $line;
//Open a file in write mode
$fp = fopen("test4/file.txt", "w");
if(fwrite($fp, $line)) echo "writing=Ok";
else echo "writing=Error";
fclose($fp);


/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($link);
?>

So I need to update my Array with new info from mySQL table.


any advices are welcome

Thanks a lot!

Posted: Thu May 20, 2004 6:48 am
by launchcode
Is there a specific reason why you're saving the data to a text file?

Also in the above code you will only ever save out 1 record from the table (the first row) because you do not loop through the results. Something like the following might help:

Code: Select all

$result = mysql_query($query);
foreach (mysql_fetch_assoc($result) as $data)
{
$your_array[] = $data;
}
This will loop through all returned records building them up into an array called $your_array. It'll create an assoc. array as a new element of $your_array, but there are other ways to do this - just not really sure what you want as the end result.

Posted: Thu May 20, 2004 7:31 am
by duk
you just need to read more about php and mysql

mysql_fetch_array is like mysql_fetch_row

i just use fetch_row, i like more this way...

so like this

Code: Select all

<?php

$sql_command = "select field1,field2 from table1";

$execute_command = mysql_db_query("database", $sql_command);

//to associate the values i use this

$get_values = mysql_fetch_row($execute_command);

$value1 = $get_values[0];
$value2 = $get_values[1];

echo "$value1, $value2";


?>
using mysql_fetch_array its diferent if you want to use it, try to see

http://php.telepac.pt/manual/en/functio ... -array.php

regards
duk

Posted: Fri May 21, 2004 9:26 am
by rodman
I need to fetch data from mySQL database and update existing Array in a PHP file


Here is file which should update my Array in PHP file:

<?php
/* Connecting, selecting database */
$link = mysql_connect("localhost", "user", "pass")
or die("Could not connect : " . mysql_error());
echo "Connected successfully";
mysql_select_db("helpdemo_xcart") or die("Could not select database");

/* Performing SQL query */
$query = "SELECT url FROM xcart_customers";
$result = mysql_query($query) or die("Query failed : " . mysql_error());

/* Inserting results in alowed IP file */

while ($row = mysql_fetch_array($result)) {
extract($row);
$line[] = $result; /* I think mistake is here but I do not know how to fiks it */
}


//Open a file in write mode
$fp = fopen("key.php", "w");
if(fwrite($fp, $line)) echo "writing=Ok";
else echo "writing=Error";
fclose($fp);


/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($link);
?>

And here is my PHP file(key.php):

<?php

$line = array("195.39.35.31","81.0.235.34","217.11.238.194");

foreach($line as $ip)
{
if($ip == $_SERVER[REMOTE_ADDR])
{
$allowaccess = 1;
}
}

if($allowaccess)
{
echo "Comments=2";
}

else
{
echo "Comments=3";
}

?>

What I need is to update my array $line with new values from database. The first file should do the job but it does not work.



So I need to update my Array with new info from mySQL table.


any advices are welcome

Thanks a lot!

Posted: Fri May 21, 2004 11:35 am
by launchcode
I'm not surprised! You're fetching an array (fine) and then extracting all the values from it into the local variable scope - erm.. why? Anyway I would just do it like this - there is no reason to fetch an entire array in this instance as you're only bring back 1 column from the table (url).

Much easier:

Code: Select all

for ($i = 0; $i < mysql_num_rows($result); $i++)
{
$line[] = mysql_result($result, $i, 'url');
}
Now save the $line array to a text file rather than try and re-write your PHP file and in your key.php file just load the file back up again and carry on from where you left off.