jjmusicpro Posted October 10, 2007 Share Posted October 10, 2007 i am trying to loop through results in my db, however, i cant get it to work. i need the echo to ouput something with " in it... <?php //Begin of Checking Loop $URLarray = array( parse_str("$QUERY_STRING"); $db = mysql_connect("localhost", "url_tester_user","xxxxx") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("url_tester_db",$db)) die("No database selected."); $acc1="SELECT add_url from urls"; $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_array($acc2)) { echo " "\$acc3[add_url]\";"; } // need to be formatted likt this -- >"http://testplace.com/", "http://myspace.com/" ); echo "<table border=1>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/ Share on other sites More sharing options...
trq Posted October 10, 2007 Share Posted October 10, 2007 Replace.... $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_array($acc2)) { echo " "$acc3[add_url]";"; } with.... if ($acc2 = mysql_query($acc1)) { if (mysql_num_rows($acc2)) { while ($acc3 = mysql_fetch_array($acc2)) { echo " " . $acc3['add_url'] . ";"; } } } else { die("Could not select accounts."); } Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366234 Share on other sites More sharing options...
freakstyle Posted October 10, 2007 Share Posted October 10, 2007 echo " "$acc3[add_url]";"; // should be: echo $acc3['add_url'] ; // if you need a link w/some formatting : echo '<a href="' . $acc3['add_url'] . '" onclick="window.open(this.href); return false">' . $acc3['add_url'] . '</a><br/>' . "\r\n"; if that doesn't help, you'll need to provide more details into what is not working for you. is the array empty after you're result? what is an example of the value in the 'add_url' column? good luck Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366236 Share on other sites More sharing options...
jjmusicpro Posted October 10, 2007 Author Share Posted October 10, 2007 Im trying to get the url's from the db, and put them into the array. I ned ot make sure the last value pulled does not have the ; at the end. I have it working when its hard coded, it looks like this: <?php //Begin of Checking Loop $URLarray = array( "http://ebay.com", "http://myspace.com/" ); echo "<table border=1>\n"; echo " <tr>\n"; I just want to get the values of "url" from the db, and put them in for the values of the array, instead of me hard coding them in. FYI notice the last value of the arry does not have the , at the end. i tried the code you both gave and its a no go. Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366250 Share on other sites More sharing options...
kenrbnsn Posted October 10, 2007 Share Posted October 10, 2007 Why didn't you say that in the first place. Use this: <?php $URLarray = array(); $acc1="SELECT add_url from urls"; $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_assoc($acc2)) $URLarray[] = $acc3[add_url]; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366280 Share on other sites More sharing options...
jjmusicpro Posted October 10, 2007 Author Share Posted October 10, 2007 where do i put that? this is my code i have now... <?php //Begin of Checking Loop $URLarray = array( "http://ebay.com", "http://myspace.com/" ); echo "<table border=1>\n"; echo " <tr>\n"; echo " <td>\n"; echo "<b>URL</b>"; echo " </td>\n"; echo " <td>\n"; echo "<b>Time</b>"; echo " </td>\n"; echo " </tr>\n"; // Display the URLS being processed. foreach($URLarray as $K => $URL) { echo "<tr>\n"; echo "<td>\n"; echo "<a href=$URL>$URL</a>"; echo "</td>\n"; echo "<td>\n"; echo "<div id='$K'>please Wait</div>"; echo "</td>\n"; echo "</tr>\n"; ob_flush(); flush(); } echo "</table>\n"; //Times foreach($URLarray as $K => $URL) { $mtime = microtime(); $mtime = explode(' ', $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; if( url_exists($URL) ) { $data = file_get_contents($URL); unset($data);//dump the data $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); $UrlResults[$URL] = array("Access" => true, "Time"=>$totaltime); echo " <script language='javascript'>display('$K', '$totaltime seconds');</script>"; }else{ $UrlResults[$URL] = array("Access" => false); echo " <script language='javascript'>display('$K', 'Failed');</script>"; } ob_flush(); flush(); } function url_exists($strURL) { $resURL = curl_init(); curl_setopt($resURL, CURLOPT_URL, $strURL); curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1); curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'readHeader'); curl_setopt($resURL, CURLOPT_FAILONERROR, 1); curl_setopt($resURL, CURLOPT_TIMEOUT, 1); $x = curl_exec($resURL); $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE); curl_close ($resURL); if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) { return false; }else{ return true ; } } function readHeader($data) { return $data; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366284 Share on other sites More sharing options...
trq Posted October 10, 2007 Share Posted October 10, 2007 One would assume that you would replace.... $URLarray = array( "http://ebay.com", "http://myspace.com/" ); with what Barand provided. But thats all we can do, assume, because your not exactly being helpfull with your descriptions of the problem at hand. Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366286 Share on other sites More sharing options...
jjmusicpro Posted October 10, 2007 Author Share Posted October 10, 2007 Sorry... i tried to put it there.... but there is no where in there for me to connect to my db... when i run the script it says "could not connect".. <?php //Begin of Checking Loop $URLarray = array(); $acc1="SELECT add_url from urls"; $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_assoc($acc2)) $URLarray[] = $acc3[add_url]; echo "<table border=1>\n"; echo " <tr>\n"; echo " <td>\n"; echo "<b>URL</b>"; echo " </td>\n"; echo " <td>\n"; echo "<b>Time</b>"; echo " </td>\n"; echo " </tr>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366293 Share on other sites More sharing options...
trq Posted October 10, 2007 Share Posted October 10, 2007 Well obviosuly you need to connect prior to calling mysql_query(). Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366297 Share on other sites More sharing options...
jjmusicpro Posted October 10, 2007 Author Share Posted October 10, 2007 still cant get it to work Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366382 Share on other sites More sharing options...
kenrbnsn Posted October 10, 2007 Share Posted October 10, 2007 Please post the non-working code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366420 Share on other sites More sharing options...
darkfreaks Posted October 10, 2007 Share Posted October 10, 2007 i agree with thorpe you need to connect to the database prior to the query Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366422 Share on other sites More sharing options...
jjmusicpro Posted October 10, 2007 Author Share Posted October 10, 2007 Ok, i got it working. However, i added another row in my database called add_company. How do i display that before the URL? <?php //Begin of Checking Loop $URLarray = array(); $db = mysql_connect("localhost", "url_tester_user","xxxxxxx") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("url_tester_db",$db)) die("No database selected."); $acc1="SELECT * from urls"; $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_assoc($acc2)) $URLarray[] = $acc3[add_url]; echo "<table border=1>\n"; echo " <tr>\n"; echo " <td>\n"; echo "<b>URL</b>"; echo " </td>\n"; echo " <td>\n"; echo "<b>Time</b>"; echo " </td>\n"; echo " </tr>\n"; // Display the URLS being processed. foreach($URLarray as $K => $URL) { echo "<tr>\n"; echo "<td>\n"; echo "<a href=$URL>$URL</a>"; echo "</td>\n"; echo "<td>\n"; echo "<div id='$K'>please Wait</div>"; echo "</td>\n"; echo "</tr>\n"; ob_flush(); flush(); } echo "</table>\n"; //Times foreach($URLarray as $K => $URL) { $mtime = microtime(); $mtime = explode(' ', $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; if( url_exists($URL) ) { $data = file_get_contents($URL); unset($data);//dump the data $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); $UrlResults[$URL] = array("Access" => true, "Time"=>$totaltime); echo " <script language='javascript'>display('$K', '$totaltime seconds');</script>"; }else{ $UrlResults[$URL] = array("Access" => false); echo " <script language='javascript'>display('$K', 'Failed');</script>"; } ob_flush(); flush(); } function url_exists($strURL) { $resURL = curl_init(); curl_setopt($resURL, CURLOPT_URL, $strURL); curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1); curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'readHeader'); curl_setopt($resURL, CURLOPT_FAILONERROR, 1); curl_setopt($resURL, CURLOPT_TIMEOUT, 1); $x = curl_exec($resURL); $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE); curl_close ($resURL); if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) { return false; }else{ return true ; } } function readHeader($data) { return $data; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-366461 Share on other sites More sharing options...
jjmusicpro Posted October 11, 2007 Author Share Posted October 11, 2007 i added a new column in db called add_client, and i want to put that in another row in front of the hyperlink when kicking out. However the script runs fine, it just dosent put the add company in the beginning. Any ideas? <head><title>Checker</title></head> <script language="javascript"> function display(id ,str) { document.getElementById(id).innerHTML = str; } </script> <body> <b>Check Links</b><br> <br> ***Notes***<br> If site takes more then 8 seconds to load, it will show "failed".<br> URL's are being pulled from DB - THIS MAY TAKE SOME TIME. <br> ***Notes***<p> <?php //Begin of Checking Loop $URLarray = array(); $db = mysql_connect("localhost", "url_tester_user","xxxxxx") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("url_tester_db",$db)) die("No database selected."); $acc1="SELECT * from urls"; $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_assoc($acc2)) $URLarray[] = $acc3[add_url]; $add_company = $acc3[add_company]; echo "<table border=1>\n"; echo " <tr>\n"; echo " <td>\n"; echo "<b>URL</b>"; echo " </td>\n"; echo " <td>\n"; echo "<b>Time</b>"; echo " </td>\n"; echo " </tr>\n"; // Display the URLS being processed. foreach($URLarray as $K => $URL) { echo "<tr>\n"; echo "<td>\n"; echo "$add_company <a href=$URL>$URL</a>"; echo "</td>\n"; echo "<td>\n"; echo "<div id='$K'>please Wait</div>"; echo "</td>\n"; echo "</tr>\n"; ob_flush(); flush(); } echo "</table>\n"; //Times foreach($URLarray as $K => $URL) { $mtime = microtime(); $mtime = explode(' ', $mtime); $mtime = $mtime[1] + $mtime[0]; $starttime = $mtime; if( url_exists($URL) ) { $data = file_get_contents($URL); unset($data);//dump the data $mtime = microtime(); $mtime = explode(" ", $mtime); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; $totaltime = ($endtime - $starttime); $UrlResults[$URL] = array("Access" => true, "Time"=>$totaltime); echo " <script language='javascript'>display('$K', '$totaltime seconds');</script>"; }else{ $UrlResults[$URL] = array("Access" => false); echo " <script language='javascript'>display('$K', 'Failed');</script>"; } ob_flush(); flush(); } function url_exists($strURL) { $resURL = curl_init(); curl_setopt($resURL, CURLOPT_URL, $strURL); curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1); curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'readHeader'); curl_setopt($resURL, CURLOPT_FAILONERROR, 1); curl_setopt($resURL, CURLOPT_TIMEOUT, 1); $x = curl_exec($resURL); $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE); curl_close ($resURL); if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) { return false; }else{ return true ; } } function readHeader($data) { return $data; } ?> </body> Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-367190 Share on other sites More sharing options...
jjmusicpro Posted October 11, 2007 Author Share Posted October 11, 2007 anyone ??? :'( Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-367229 Share on other sites More sharing options...
jjmusicpro Posted October 11, 2007 Author Share Posted October 11, 2007 sorry the bump guys... Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-367312 Share on other sites More sharing options...
jjmusicpro Posted October 11, 2007 Author Share Posted October 11, 2007 I added a new column in the Db called add_company, and i wanted to kick that out in front of hyperlinke, but when i run my script it wont work $URLarray = array(); $db = mysql_connect("localhost", "url_tester_user","xxxxxx") or die("Could not connect."); if(!$db) die("no db"); if(!mysql_select_db("url_tester_db",$db)) die("No database selected."); $acc1="SELECT * from urls"; $acc2=mysql_query($acc1) or die("Could not select accounts."); while($acc3=mysql_fetch_assoc($acc2)) $URLarray[] = $acc3[add_url]; $add_company = $acc3[add_company]; echo "<table border=1>\n"; echo " <tr>\n"; echo " <td>\n"; echo "<b>URL</b>"; echo " </td>\n"; echo " <td>\n"; echo "<b>Time</b>"; echo " </td>\n"; echo " </tr>\n"; // Display the URLS being processed. foreach($URLarray as $K => $URL) { echo "<tr>\n"; echo "<td>\n"; echo "$add_company <a href=$URL>$URL</a>"; echo "</td>\n"; echo "<td>\n"; echo "<div id='$K'>please Wait</div>"; echo "</td>\n"; echo "</tr>\n"; ob_flush(); flush(); } echo "</table>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/#findComment-367371 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.