Reputation: 147
i am using curl to fetch mysql data from my one site to another. The codes are
$ch = curl_init();
$myurl = "http://domain.com/account.php?siteid=$siteid";
curl_setopt ($ch, CURLOPT_URL, $myurl);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $siteref);
$file_contents = curl_exec($ch);
curl_close($ch);
$data=unserialize($file_contents);
print_r($data);
the code on account.php
of other site is
$q=mysql_query("SELECT * FROM `".$siteid."`");
$data=mysql_fetch_array($q);
echo serialize($data);
the problem is i am getting only the first record from the table. I want the entire contents of the table sent back. how can i do that?
Upvotes: 1
Views: 2353
Reputation: 24661
From the docs:
mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
Unless you're looping over the result from mysql_query
, you're only ever going to get one row. Try something along the lines of:
$q = mysql_query("SELECT * FROM `" . $siteid . "`");
$data = array();
while($row = mysql_fetch_array($q)) {
$data[] = $row;
}
However, look at that docs page again and you'll notice a big red WARNING indicating that you should no longer use the mysql extension. Instead you should build your application using PDO or mysqli.
Upvotes: 1
Reputation: 3813
You need a loop to store each array. mysql_fetch_array returns only one row at a time.
while ($data=mysql_fetch_array($q)) {
$dataRows[] = $data;
}
echo serialize($dataRows);
Also, all mysql functions are now deprecated. Take a look at mysqli and PDO.
Upvotes: 1
Reputation: 71384
You are only fetching one row of data from the result set. If you want all rows, you will need to read out the result set in a loop construct. that typically looks something like this:
$q=mysql_query("SELECT * FROM `".$siteid."`");
$data = array();
while($row = mysql_fetch_array($q)) {
$data[] = $row;
}
echo serialize($data);
A few important notes:
mysql_*
functions as they are deprecated. Consider using mysqli
or PDO
instead.$siteid
before using it in a query.Upvotes: 0
Reputation: 354
This goes in your account.php
file:
(to pull multiple records off database you should use loops)
$q=mysql_query("SELECT * FROM `".$siteid."`");
if(mysql_num_rows($q) >= 1) {
while($data = mysql_fetch_array($q)) {
echo serialize($data);
}
} else { echo "there are no rows"; }
also using mysql_num_rows
is a good fail-safe method.
as of PHP 5.4 all mysql_
functions have been deprecated, they are not secure and no longer recommended, consider using PDO
or mysqli
instead!
Upvotes: 0