Following is my code,
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
When I'am trying to use $myArrayOfemp_id variable in $sql query, It shows that error:
Array to string conversion in..
How can I fix it?
You are trying to convert an array into a string in the following line:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id='$myArrayOfemp_id' ORDER BY apply_date DESC";
$myArrayOfemp_id is an array. That previous line of code should be changed to:
$sql = "SELECT emp_id FROM emp_leaves
WHERE emp_id={$myArrayOfemp_id[0]} ORDER BY apply_date DESC";
I placed 0 inside {$myArrayOfemp_id[0]} because I'm not sure what value want to use that is inside the array.
Edited:
After discussing what the user wanted in the question, it seems the user wanted to use all the values inside the array in the sql statement, so here is a solution for that specific case:
$sql = "SELECT emp_id FROM emp_leaves
WHERE ";
foreach ($myArrayOfemp_id as $value)
{
$sql .= " emp_id={$value) || ";
}
$sql .= "1=2";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
$sql = "SELECT emp_id FROM emp_leaves WHERE emp_id in
(SELECT GROUP_CONCAT(emp_id) FROM employee where manager_id=".$userID.")
ORDER BY apply_date DESC";
$result = mysql_query($sql);
$total_results = mysql_num_rows($result);
just change your query like above might solve your problem.
you can remove following code now. :)
$result1 = "SELECT emp_id FROM employee where manager_id=".$userID;
$array = mysql_query($result1);
$cnt = 0;
while ($row = mysql_fetch_array($array)) {
"emp_id: " . $row[0];
$myArrayOfemp_id[$cnt] = $row[0];
$cnt++;
}
var_dump($myArrayOfemp_id);
I have the following code
$sql = "SET #uid := (SELECT ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);";
$sql = "UPDATE channels SET Used = 1 WHERE ID = #uid;";
$sql = "SELECT * FROM channels WHERE ID IN = #uid;";
$result = mysqli_multi_query($conn, $sql)
or die( mysqli_error($sql) );
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
Now the issue i'm having is the php isnt displaying the result returned by the MYSQL query which is stored in a session later on in the code to be displayed on a dummy page it comes up with the following error
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result
The my SQL query does exactly what I need it to I just need to, so I don't really want to change it. I just need some advice on how i'd get the PHP to echo the #uid
is there anyone willing to help me solve the issue? if so thankyou.
You have 3 queries in your $sql so you should use multi_query function
http://php.net/manual/en/mysqli.multi-query.php
And you can change your first query to:
SET #uid = 0;
SELECT #uid := ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);
Update You can try this fragment of your code modified with all commented improvements.
$sql = 'SET #uid = 0;';
$sql .= 'SELECT #uid:= ID FROM channels WHERE Used = 0 ORDER BY RAND() LIMIT 1);';
$sql .= 'UPDATE channels SET Used = 1 WHERE ID = #uid;';
$sql .= 'SELECT * FROM channels WHERE ID IN = #uid;';
if (mysqli_multi_query($conn, $sql)) {
do {
$result = mysqli_store_result($conn);
} while(mysqli_next_result($conn));
if (mysqli_num_rows($result) > 0) {
$text = '';
while($row = mysqli_fetch_assoc($result)) {
$Channel_Location = $row['Channel_Location'];
$text = $text . $Channel_Location;
}
}
} else {
die( mysqli_error($conn) );
}
How to loop php using ref data from mysql ?
work flow :
Loop If column 'xxx' == ''
{
$sql = "SELECT * FROM table WHERE xxx == '' order by id desc";
$result = mysql_query($sql);
$datas=mysql_fetch_array($result);{
$id = stripslashes(str_replace('\r\n', '<br>',($datas['id'])));
}
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}
ypur query is confusing. why would you store ids with html in them? regardless, this should work
$sql = "SELECT * FROM table WHERE xxx != '' "; //ORDER BY will not work since ids are not int
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result){
$id = stripslashes(str_replace('\r\n', '<br>',($row['id'])));
$sql = "UPDATE table SET xxx = 'test' WHERE id = '$id' ";
$dbQuery = mysql_query($sql);'
}
I have a table with a column called 'status'. The defaut value of 'status' is 0. I want to update the value to '1' after using it.
I basically want to check if the status is 0, if it is, do an operation and then change the value to 1.
Here is the code. All works perfectly except that the value of 0 is not changed to 1.
I am a novice so maybe is a very basic mistake :(
<?php
$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$SummonerID = $row["number"];
$status = $row["status"];
if($status=='0'){
$recentgames=$lol->getRecentGames($SummonerID);
$MatchID1=$recentgames->games[0]->gameId;
$sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";
$sql = "UPDATE summonerid SET status='1' WHERE status='0';"; // THIS IS THE PART THAT DOES NOT WORK WELL
}
}
}
?>
Any help would be highly appreciated
Try this.. you are not executing the sql statements
$sql = "SELECT number, status FROM summonerid";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$SummonerID = $row["number"];
$status = $row["status"];
if($status=='0'){
$recentgames=$lol->getRecentGames($SummonerID);
$MatchID1=$recentgames->games[0]->gameId;
$sql = "INSERT INTO matchid (number) SELECT * FROM (SELECT '$MatchID1') AS tmp WHERE NOT EXISTS (SELECT number FROM matchid WHERE number = '$MatchID1') LIMIT 1;";
$result1 = $conn->query($sql);
$sql = "UPDATE summonerid SET status='1' WHERE status='0';";
$result1 = $conn->query($sql);
}
}
}
?>
I have a query that gets 5 lines of data like this example below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
}
I want to run a query inside each results like this below
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
but for some reason it's only display the first line when I add the query inside it. I can seem to make it run all 5 queries.
SELECT
t1.ref,
t1.user,
t1.id,
t2.domain,
t2.title
FROM
table AS t1
LEFT JOIN anothertable AS t2 ON
t2.domain = t1.ref
LIMIT
0, 5
The problem is that inside the while-cycle you use the same variable $result, which then gets overridden. Use another variable name for the $result in the while cycle.
You change the value of your $query in your while loop.
Change the variable name to something different.
Ex:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$qry = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$rslt = mysql_query($qry) or die(mysql_error());
if (mysql_num_rows($rslt) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}
Use the following :
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query_domain = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result_domain = mysql_query($query_domain) or die(mysql_error());
if (mysql_num_rows($result_domain) )
{
$row_domain = mysql_fetch_row($result_domain);
$title = $row_domain['title'];
} else {
$title = "No Title";
}
echo "$ref - $title";
}
This is a logical problem. It happens that way, because you are same variable names outside and inside the loop.
Explanation:
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
// Now $results hold the result of the first query
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
//Using same $query does not affect that much
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
//But here you are overriding the previous result set of first query with a new result set
$result = mysql_query($query) or die(mysql_error());
//^ Due to this, next time the loop continues, the $result on whose basis it would loop will already be modified
//..............
Solution 1:
Avoid using same variable names for inner result set
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$sub_result = mysql_query($query) or die(mysql_error());
// ^ Change this variable so that it does not overrides previous result set
Solution 2:
Avoid the double query situation. Use joins to get the data in one query call. (Note: You should always try to optimize your query so that you will minimize the number of your queries on the server.)
SELECT
ref,user,id
FROM
table t
INNER JOIN
anothertable t2 on t.ref t2.domain
LIMIT 0, 5
Learn about SQL joins:
SELECT table.ref, table.user, table.id, anothertable.title
FROM table LEFT JOIN anothertable ON anothertable.domain = table.ref
LIMIT 5
You're changing the value of $result in your loop. Change your second query to use a different variable.
it is not give proper result because you have used same name twice, use different name like this edit.
$query = "SELECT ref,user,id FROM table LIMIT 0, 5";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$ref = $row['ref'];
$query1 = "SELECT domain,title FROM anothertable WHERE domain = '$ref'";
$result1 = mysql_query($query1) or die(mysql_error());
if (mysql_num_rows($result1) )
{
$title = $row['title'];
} else {
$title = "No Title";
}
echo "$ref - $tile";
}