the following sql query works perfectly in phpAdmin.
SELECT response_text FROM statement_responses WHERE response_code = "s1_r1"
it doesn't work without the speech marks around response_code value.
I'm attempting to use the value of response_text in php.
for ($x = 1; $x <= 9; $x++) {
$user_response = ${"statement_" . $x . "_response"};
$sql = "SELECT response_text FROM statement_responses WHERE response_code = \"$user_response\"";
$result = mysqli_query($sql);
$value = mysqli_fetch_object($result);
echo $user_response . "<br>";
echo $sql . "<br>";
echo $result . "<br>";
echo $value . "<br>";
}
The echoes allow me to see what each of the variables contains.
I get the following:
s1_r3 (the value of $user_response)
SELECT response_text FROM statement_responses WHERE response_code = "s1_r3" (the value of $sql - which is identical to the phpAdmin query that works.)
There are no values echoed for $result or $value.
What am I doing wrong, please? Why am I not getting the values from the database into my php code?
The first parameter for the mysqli_query should be the database connection created with mysqli_connect. Similarily, the first parameter for the mysqli_fetch_object, should be the result set identifier returned by mysqli_query.
It is a good practice to check the return values from the functions you call.
firstly, thank you for all of the responses. The following code works - i.e. it returns the value contained in 'response_text' column with the selected row identified by 'response_code' for use as the value of $response_text.
for ($x = 1; $x <= 9; $x++) {
$user_response_pre = ${"statement_" . $x . "_response"};
$user_response = "\"'" . $user_response_pre . "'\"";
$sql = "SELECT response_text FROM statement_responses WHERE response_code = $user_response";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_array($result);
$response_text = $row[0];
${"statement_" . $x . "_complete"} .= $response_text;
}
you can write like this
for ($x = 1; $x <= 9; $x++) {
$user_response = ${"statement_" . $x . "_response"};
$sql = 'SELECT response_text FROM statement_responses WHERE response_code = "'.$user_response.'"';
$result = mysqli_query($sql);
$value = mysqli_fetch_object($result);
echo $user_response . "<br>";
echo $sql . "<br>";
echo $result . "<br>";
echo $value . "<br>";
}
Related
I am currently trying to output the value of an array using a for loop.
I have tried outputting the query that is running in the loop (Resulting in an echo of 24 queries)
1
$number_of_beams = 24;
for ($i = 0; $i < $number_of_beams; $i++)
{
$query = "SELECT fitacf_data.time,
SUM(fitacf_data.num_pts) AS point_total
FROM
fitacf_data
WHERE abbrev = '" . $radar_array[0]['radar_abbrev'] ."'
AND fitacf_data.beam = '" . $i . "'
GROUP BY fitacf_data.time";
$result = pg_query($query) or die('Error: ' . pg_last_error());
while ($row = pg_fetch_array($result)) {
$beam_total_array[] = $row;
}
echo $i
echo $beam_total_array[0]['point_total'] . "<br><br>";
}
If I hard code $i to any value from 0-23 echo $beam_total_array[0]['point_total']; outputs the correct value 24 times.
ie:
2
for ($i = 0; $i < $number_of_beams; $i++)
{
$query = "SELECT fitacf_data.time,
SUM(fitacf_data.num_pts) AS point_total
FROM
fitacf_data
WHERE abbrev = '" . $radar_array[0]['radar_abbrev'] ."'
AND fitacf_data.beam = '5'
GROUP BY fitacf_data.time";
$result = pg_query($query) or die('Error: ' . pg_last_error());
while ($row = pg_fetch_array($result)) {
$beam_total_array[] = $row;
}
echo $i
echo $beam_total_array[0]['point_total'] . "<br><br>";
}
$i is returning 0-23 as expected.
If I run the code as shown in #1 using the $i variable the output is 0 for the 24 times.
What am I missing here?
The problem is that you're declaring $i as a string, which has a value of 0. Remove the double quotes around $i: AND fitacf_data.beam = ' . $i . '
I have created a query which I am using to display reports. The query gets data from a few different tables which is fine, although I cannot figure out how to echo more than one matching value within a table. For example the table vtiger_addisa has more than one newcurrentamount which I need displayed.
$sql = $adb->query("SELECT *
FROM vtiger_isa, vtiger_addisa, vtiger_contactdetails
WHERE vtiger_isa.relatedclient = vtiger_addisa.addrelatedclient
AND vtiger_addisa.addrelatedclient = vtiger_contactdetails.contactid
AND vtiger_isa.relatedclient = $relatedclient
AND vtiger_isa.policynumber = $policynumber");
//Uncomment lines below for testing sql
//echo $sql;
//exit;
while ($sql->fetchInto($row)) {
// Assuming DB's default fetchmode is DB_FETCHMODE_ORDERED
echo $row['firstname'] . "\n";
echo $row['lastname'] . "\n";
echo $row['policynumber'] . "\n";
echo $row['newcurrentamount'] . "\n";
echo $row['newcurrentamount'] . "\n";
echo $row['currentamount'] . "\n";
exit;
}
You can get Multiple Row result as below.
global $adb;
$sql = $adb->query("SELECT * FROM vtiger_isa, vtiger_addisa, vtiger_contactdetails
WHERE vtiger_isa.relatedclient = vtiger_addisa.addrelatedclient
AND vtiger_addisa.addrelatedclient = vtiger_contactdetails.contactid
AND vtiger_isa.relatedclient = $relatedclient
AND vtiger_isa.policynumber = ?");
$params = array($policynumber);
$result = $adb->pquery($query, $params);
$noOfRows = $db->num_rows($result);
for($i=0; $i<$noOfRows; ++$i) {
$firstname = $db->query_result($result, $i, "firstname");
$lastname = $db->query_result($result, $i, "lastname");
$policynumber = $db->query_result($result, $i, "policynumber");
$newcurrentamount = $db->query_result($result, $i, "newcurrentamount");
$currentamount = $db->query_result($result, $i, "currentamount");
}
Use 'as' operator
select newcurrentamount as a, newcurrentamount as b from vtiger_addisa
P.S- Try avoiding ' * ' and use only the necessary columns.
I have different paramter of particular mysql query I am running so I need to run them in bunch.. so say
$server = 'server_';
for ($a=0 ; $a<5 ; ++$a) {
$criteria = $server . $a;
$query1 = 'select blabh blah not important ' . $criteria . 'order by date desc limit 10';
$query_result = mysql_query($query1);
}
Above only comes back results for server_4
I naively thought I could just do
$query_result .= mysql_query($query1);
But clearly that doesn't work. I hope no one says why don't you run mysql as
like '$server%'
I am looking for to see if what I am trying to do is possible. appending.. I guess string append is possible but I perahps simply don't understand what's coming back from mysql?
fetch code sample
select * from tableName where server like $criteria order by date desc limit 10
=========================================================
code sample
$data = array();
$dataMaster = array();
for ( $x = 0; $x <= 8; $x++ ) {
$server = ‘server_’ . $x;
$myquery = 'select * from serverTable where servername like ' . '"' . $server . '"' . ' order by date1 desc limit 100';
$query = mysql_query($myquery);
if ( ! $query ) {
echo mysql_error();
die;
}
for ( $x = 0; $x < mysql_num_rows($query); $x++ ) {
$data[] = mysql_fetch_assoc($query);
}
$data = array_reverse($data);
array_push($dataMaster, $data);
}
echo json_encode($dataMaster)
Try to avoid SQL inside PHP loop, you can do the following instead:
SELECT * FROM serverTable WHERE servername IN
('server_0', 'server_1', 'server_2', 'server_3', 'server_4')
This is cancel_order function,that also in it will call the increase_gameamount() function, i am trying to call increament_gameamount() function it works but when I try to call it from while loop nothing changes in database.
//cancel function
function cancel_order($ord) {
global $conn;
$bqty = 0;
$gqty = 0;
$res = array();
echo "entered cancel function " . $ord . "<br>";
$st = "select (B_qty+G_qty) newqt, B_GM_ID from tb_basket b, tb_game g
where b.B_GM_ID = g.G_ID
and B_O_ID='$ord' ";
$sql = $conn->prepare($st);
$sql->execute();
$sql->bind_result($newqt, $gid);
$i = 0;
while($row = $sql->fetch()) {
$res[$i][0] = $newqt;
$res[$i][1] = $gid;
$i++;
}
$j = 0;
$sql->free_result();
$sql->close();
while($j < sizeof($res)) {
echo $gd = $res[$j][0] . "<br>";
echo $qty = $res[$j][1] . "<br>";
increament_gameamount($gd, $qty);
$j++;
}
}
//increament function
function increament_gameamount($gameid, $new_qty) {
global $conn;
echo "entered increament_gameamount function";
echo $gameid;
echo $new_qty;
$varupdateqty = $conn->prepare("update tb_game set G_qty=? where G_ID=?");
$varupdateqty->bind_param("ss", $new_qty, $gameid);
$varupdateqty->execute();
echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
}
As I stated in the comments I think you are failing on the WHERE of your query because
echo $gd=$res[$j][0]."<br>";
is making $gd a string like 125<br> and the DB cannot find that.
Also, this would cause an error but if the type of your column is int and you pass:
echo $qty=$res[$j][1]."<br>";
again you make $qty something like 1000<br> and that would fail again this would be an error the above for ID check would not.
UPDATE
Just realized I did not specifially state the resolution. Set the variables then echo them and you should be all good.
$gd=$res[$j][0];
$qty=$res[$j][1];
echo $gd . "<br>" . $qty . "<br>";
I'm not sure why this SQL query is not working.
I'm new to SQL/PHP so please forgive.
mysql_query("
SELECT * FROM table WHERE name = " . "'Bob'" .
while($i < $size)
{
$i++;
echo "OR name = '";
echo $array[$i] . "'";
} .
" ORDER BY id DESC "
);
Dreamweaver gives me an error saying it is not correct but does not tell me what is wrong.
Is it possible to put a while loop into an sql command?
you can not use a while in a string
$where = "";
if ($size > 0)
{
$where .= " WHERE ";
}
while($i < $size)
{
$i++;
$where .= "OR name = '".$array[$i]."' ";
}
$query = "SELECT * FROM table WHERE name = '".Bob."'".$where." ORDER BY id DESC";
mysql_query($query);
(this code is not tested)
Woot !
You just can't write this :D
Build your OR condition before writing the query and it will be just fine:
$myCondition = " ";
while($i < $size) {
$i++;
$myCondition .= "OR name = '" . $array[$i] . "'";
}
mysql_query(
"SELECT * FROM table WHERE name = " . "'Bob'" . $myCondition . " ORDER BY id DESC ");
echo is to output the string, and it won't return the string.
Something like $str = "aaa" . echo "bbb"; won't work.
For you case, use IN will be better.
foreach ($array as &$name) {
$name = "'".mysql_real_escape_string($name)."'";
}
mysql_query("SELECT * FROM table WHERE name IN (".implode(',', $array).")");
Or use
"SELECT * FROM table WHERE name IN(".implode( ',', $array).")";