I have a simple mysql query which is as following:
SELECT DISTINCT(node.nid) AS nid,
node_counter.totalcount AS node_counter_totalcount,
node.title AS node_title,
node_data_field_dep.field_dep_value AS node_data_field_dep_field_dep_value,
node.type AS node_type,
node.vid AS node_vid,
node_data_field_type.field_type_value AS node_data_field_type_field_type_value
FROM node node
LEFT JOIN node_counter node_counter ON node.nid = node_counter.nid
LEFT JOIN content_field_dep node_data_field_dep ON node.vid = node_data_field_dep.vid
LEFT JOIN content_type_project node_data_field_type ON node.vid = node_data_field_type.vid
WHERE (node.type in ('project')) AND (node.status <> 0)
GROUP BY nid
ORDER BY node_counter_totalcount DESC
so i execute the following statements:
$result = mysql_query($query);
where $query is the above SQL query.
Usually, I use mysql_fetch_array($result) or mysql_fetch_row($result) to access the data, but these are not working now and instead giving some error. Is there any other way to use them. I want to print the data selected in a formatted manner.(Say, a table)
It means your query is not executed, there is some error in it or maybe in the connection.
You can find out what the error is using mysql_error() like this:
$result = mysql_query($myquery) or die(mysql_error());
or you can use
$row = mysql_num_rows($result);
echo($row);
If it returns 1 or higher it means your query is returning results.
Related
I am pretty new to PHP and don't know how to get results from the mysqli_fetch_assoc() function if my select statement has table joins and those joins are handled with table aliases.
The code looks like following:
$sql = 'SELECT T1.update_text, T1.created_at, T2.username, T3.group_name FROM updates AS T1
INNER JOIN users AS T2 ON T2.user_id = T1.user_id_fk
INNER JOIN groups AS T3 ON T3.group_id = T1.group_id_fk
WHERE T1.user_id_fk = "1"';
And the PHP Code afterwards:
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
printf ("%s (%s)\n",$row["username"],$row["T1.created_at"]);
}
For the "username" row I am not getting an error, but nothing is displayed. For the second one I am getting an Index undefined error. (This is only a snippet of the code)
Change the SQL Query as following,
$sql = 'SELECT T1.update_text as update_text, T1.created_at as created_at, T2.username as username, T3.group_name as group_name FROM updates AS T1
INNER JOIN users AS T2 ON T2.user_id = T1.user_id_fk
INNER JOIN groups AS T3 ON T3.group_id = T1.group_id_fk
WHERE T1.user_id_fk = "1"';
then change the PHP code as following,
$result = $mysqli->query($sql);
if (mysqli_num_rows($result) > 0) {
while($row = $results->fetch_object())
{
echo $row->username.$row->created_at."<br/>";
}
}
I am using the following query:
$query = "SELECT (SELECT count(*) FROM survey_ptw WHERE erfh= '0') AS F01,
(SELECT count(*) FROM survey_ptw WHERE erfh='10') AS F02,
(SELECT count(*) FROM survey_ptw WHERE erfh='50') AS F03";
$result = mysql_query($query);
print sprintf('Erf: <table><tr><td>none</td><td>%s</td></tr><tr><td>more</td><td>%s</td></tr></table>', $F01,$F02);
When I'm doing the above query in phpMyAdmin, it displays the variables nicely with the correct result values. Doing the same in PHP however, no results are displayed (empty strings)!
mysql_errno() and mysql_error() don't return errors. The DB has been open and selected further up in the code.
Any idea what's going on?
Because you aren't doing anything with the results.
From the documentation for mysql_query():
The returned result resource should be passed to mysql_fetch_array(), and other functions for dealing with result tables, to access the returned data.
So, to retrieve the results, you can use a while loop as below:
while ($row = mysql_fetch_assoc($result)) {
# code...
}
check $res=mysql_fetch_assoc($result); then print_r(); to check the result. it will work.
$query = "SELECT (SELECT count(*) FROM survey_ptw WHERE erfh= '0') AS F01,
(SELECT count(*) FROM survey_ptw WHERE erfh='10') AS F02,
(SELECT count(*) FROM survey_ptw WHERE erfh='50') AS F03";
$result = mysql_query($query);
$res=mysql_fetch_assoc($result);
$res=$res[0];
sprintf('Erf: <table><tr><td>none</td><td>%s</td></tr><tr><td>more</td><td>%s</td></tr></table>', $res['F01'],$res['F02']);
I want to generate xml file from several tables in a MySQL database using PHP.
$query = "
SELECT
t.value,
d.value,
ty.value,
p.id,
p.ref,
p.is_renting,
p.price,
p.adress,
p.living_space,
p.rooms
FROM
jos_jea_properties p
INNER JOIN
jos_jea_towns t
ON
t.id = d.town_id
INNER JOIN
jos_jea_departments d
ON
d.id = p.department_id
INNER JOIN
jos_jea_types ty
ON
ty.id = p.type_id";
$result = mysql_query ($query, $link) or die("Could not complete database query");
But, it show me Could not complete database query, have you any idea?
There's something wrong in your query. Use mysql_error() to get the message returned by MySQL.
$result = mysql_query ($query, $link) or die("Could not complete database query: ".mysql_error()));
However, your query gets a lot of fields named value. Use aliases instead, otherwise they're getting replaced
$query = "SELECT t.value t_value, d.value d_value, ty.value ty_value,
p.id, p.ref, p.is_renting, p.price, p.adress,
p.living_space, p.rooms FROM jos_jea_properties
p INNER JOIN jos_jea_towns t ON t.id = d.town_id
INNER JOIN jos_jea_departments d ON d.id = p.department_id
INNER JOIN jos_jea_types ty ON ty.id = p.type_id";
Have you an idea about a code php How to generate a file xml for image since mysql database using php?
Is it possible to strip away columns from the response I get in a query where I join 3 tables and need more or less all columns for the query itself so that some columns aren't visible in the response?
This is the query I have:
$sth = mysql_query("
SELECT
tbl_subApp2Tag.*,
tbl_subApp.*,
tbl_tag.*
ISNULL(tbl_userDeviceNOTTag.userDevice_id) AS selected
FROM tbl_subApp2Tag
LEFT JOIN tbl_subApp
ON tbl_subApp.id = tbl_subApp2Tag.subApp_id
AND tbl_subApp.subApp_id = '".$sub."'
LEFT JOIN tbl_tag
ON tbl_tag.id = tbl_subApp2Tag.tag_id
LEFT JOIN tbl_userDeviceNOTTag
ON tbl_userDeviceNOTTag.tag_id = tbl_tag.id
AND tbl_userDeviceNOTTag.userDevice_id = '".$user."'
WHERE tbl_subApp2Tag.subApp_id = tbl_subApp.id
ORDER BY tbl_tag.name ASC ");
if(!$sth) echo "Error in query: ".mysql_error();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
You do not need to include columns in the result table, just because they are referenced elsewhere in the query. Just select the columns that you need.
The following MySQL query runs in PHP without errors, but the resultset is empty. Directly outputting the query string to a file and running the query in the MySQL client using 'source [filename]' returns several rows of results, as expected.
Is there something that would cause this query not to work with PHP? categorylinks.cl_to and smw_spec2.value_string are both varbinary(255). Show create table indicates engine=InnoDB and default charset=binary.
Things I have tried without success:
$sql = preg_replace("/[\n\t]+/", " ", $sql);
Changing '_wpg' and 'Derp' to CAST('_wpg' AS BINARY(255))
Changing '_wpg' and 'Derp' to BINARY '_wpg'
I am using the MediaWiki DatabaseMysql class to execute the query and fetch rows, but it's a very thin abstraction, and I'm certain it's not the problem (see below).
SELECT
prop.name AS prop_name, prop.count AS prop_count, prop.type AS prop_type,
val.value AS val_value, val.unit AS val_unit, val.count AS val_count
FROM
(
SELECT
s_id, name, type, COUNT(foo.name) AS count
FROM (
(
SELECT
cl.cl_to AS cat_name, s.smw_id AS s_id, s.smw_sortkey AS name, spec.value_string AS type
FROM `smw_ids` s
INNER JOIN (`categorylinks` cl, `page` p, `smw_ids` s2, `smw_atts2` a)
ON (cl.cl_from = p.page_id AND
p.page_title = s2.smw_title AND
s2.smw_id = a.s_id AND
a.p_id = s.smw_id)
LEFT JOIN `smw_spec2` spec ON s.smw_id = spec.s_id
)
UNION ALL
(
SELECT
cl.cl_to AS cat_name, s.smw_id AS s_id, s.smw_sortkey AS name, '_wpg' AS type
FROM `smw_ids` s
INNER JOIN (`categorylinks` cl, `page` p, `smw_ids` s2, `smw_rels2` a)
ON (cl.cl_from = p.page_id AND
p.page_title = s2.smw_title AND
s2.smw_id = a.s_id AND
a.p_id = s.smw_id)
)
) AS foo
WHERE foo.cat_name = 'Derp'
GROUP BY name
ORDER BY count DESC
LIMIT 10
) AS prop
INNER JOIN
(
SELECT
bar.p_id AS p_id, bar.value AS value, bar.unit AS unit, COUNT(bar.value) AS count,
IF( #prev != p_id, #rownum := 1, #rownum := #rownum+1 ) AS rank,
#prev := p_id
FROM (
(SELECT a.p_id AS p_id, a.value_xsd AS value, a.value_unit AS unit FROM `smw_atts2` a)
UNION ALL
(SELECT r.p_id AS p_id, s.smw_sortkey AS value, NULL AS unit
FROM `smw_rels2` r INNER JOIN `smw_ids` s ON r.o_id = s.smw_id)
) AS bar
GROUP BY value, unit
ORDER BY count DESC
) AS val
ON prop.s_id = val.p_id
WHERE val.rank <= 50
ORDER BY prop_count DESC, prop_name, val_count DESC, val_value
Edit: The following test script outputs nothing. query.sql contains exactly the query above, written to file immediately preceding the mysql_query() call in MediaWiki's database class.
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('mediawiki', $db);
$res = mysql_query(file_get_contents("query.sql"), $db);
while ($row = mysql_fetch_assoc($res)) {
var_dump($row);
}
echo mysql_error($db);
Edit: I imported a huge database dump and afterwards, when I loaded the PHP page, there was a noticeable wait that seemed to indicate that the query was running, but still no results showed. I ended up reworking the query, and I no longer have this problem.
Try this to detect and report errors better:
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('mediawiki', $db);
$res = mysql_query(file_get_contents("query.sql"), $db);
if (!$res) {
print "SQL Error ".mysql_errno().":".mysql_error().", from query: '".file_get_contents("query.sql")."'";
} else {
while ($row = mysql_fetch_assoc($res)) {
var_dump($row);
}
}
Try this after mysql_connect:
mysql_query('SET NAMES utf8;');