Jump to content

Recommended Posts

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";

Link to comment
https://forums.phpfreaks.com/topic/72640-simple-loop-to-get-values-not-working/
Share on other sites

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.");
}

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

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.

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

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;
}
?>

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.

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";

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;
}
?>

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>

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";

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.