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!
How to get data from mySQL and insert it in Array?
Moderator: General Moderators
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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:
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.
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;
}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
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
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";
?>http://php.telepac.pt/manual/en/functio ... -array.php
regards
duk
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!
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!
- launchcode
- Forum Contributor
- Posts: 401
- Joined: Tue May 11, 2004 7:32 pm
- Location: UK
- Contact:
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:
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.
Much easier:
Code: Select all
for ($i = 0; $i < mysql_num_rows($result); $i++)
{
$line[] = mysql_result($result, $i, 'url');
}