views:

69

answers:

2

I have db with column named "link" Links have redirect urls, which I want to change. I have php script, which would take the link and gives me out put as follows

$redo = get_redirect('htp://www.mydomain.com/long/url/here&ID=123');

print_r($rez);

this is the output:

Array
(
    [0] => htp://www.otherdomain.com
)

how do I loop all items in my database, and run this script for each item. my db is called "apps" and table is called "items" and column containing urls is called "link" above php script is called getredirect.php

Also when I run script I need to run without the "&ID=123&otherstuff=whatever"

(htp supposed to be http... I can't add links per board rules) Thanks

A: 

Looks like your get_redirect is just a function that connects to DB and gets the redirect URL for a given URL.

You can write a SQL query to get redirects for all the items:

Select urls,link from apps.items;

You could also modify the function defenition so it expects multiple items and returns approporate redirects:

function get_multiple_redirect($aItems){

$SQL = "Select urls,link from apps.items WHERE url IN ('" . implode("','",$aItems) ."')";
#... add mysql calls here


print_r( get_multiple_redirect(array("http://abc","http://def")) );
}
Mohammad
get redirect is actual script that goes to actual url and gives me final destination ;)
Ossi
A: 

how do I loop all items in my database, and run this script for each item

Can you just do a simple select and put it in an array, and loop through it?

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT link FROM apps");

while($row = mysql_fetch_array($result))
  {
      $url=implode('&', $row[0]);
      include 'getredirect.php?url='.$url;

  }

mysql_close($con);

Not sure whether it helps, your question is kinda unclear to me.

If you aren't sure of the output, you can put a print_r statement, i.e.,

while($row = mysql_fetch_array($result))
  {
      print_r($row);
      include 'getredirect.php?url='.$url;

  }

This would help you understand the PHP code clearer

Ngu Soon Hui
Ossi
I think your solution is really close. When I run the script, it looks like its doing something, but dunno what, and get this 20 times Warning: implode() [function.implode]: Invalid arguments passed in ....line 10 ???
Ossi
"it looks like its doing something, but dunno what", maybe you should read about database access in php then?
Nicky De Maeyer
$url=implode('Above line is returning string not array, so I changed it to$url=($row[0]);But now how do I instert this to database?Do I need to use insert or update etc, or is their easier way of doing this?
Ossi
Getting closer when I add this: $newurl= include 'http://localhost/getredirect.php?url='.$url; $sql_update = "UPDATE items SET newlink = '$newurl'"; mysql_query($sql_update) or die(mysql_error()); var_dump($newurl);I get "1" all over my newlink column and vardump shows this Array ( [0] => http://www.newlink.com ) int(1)
Ossi