1

Hi, I have been trying to solve this for ages and just can’t seem to get it right.

I want to do 2 things here, i am getting an array out of a mysql database (i am getting the user’s id) and then converting that array into a string and using that string to get information out of another database.

i am trying to use the implode function $string=implode("OR id =" , $userid)
here is what i am trying to achieve: mysql_query(mysql stuf... WHERE id = $string) this should now equal: mysql_query(blah blah blah WHERE id = 1 OR id = 5 OR id = 19) because that is what is inside that vaiable now.

The problem i am having is i can't seem to get multiple rows out of my database, it only loads one.
when i echo out my array it said this Array ( [0] => 9 [ID] => 9 [1] => 19 [userid] => 19 [2] => 0 [approved] => 0 )

ALso i have tried a few differnt things with the imploe function at the moment i think it is coming up with an error.
anyway here is my code. i hope someone can help :)

<?php

$username = $_COOKIE['user'];

@mysql_connect ("localhost","down2par_down2pa","4m329aMh") or die ("could not connect to MySQL");
@mysql_select_db ("down2par_d2pdb") or die ("no database");

$query1 =mysql_query("SELECT * FROM login WHERE username = '$username'");
$user = mysql_fetch_array($query1);
$admin = $user['admin'];

if ($admin==1){

//get user id from "approval" database 
$query2 =mysql_query("SELECT * FROM approval WHERE approved = '0'");
$result =mysql_fetch_array($query2);


//turn array of ids into a string
$string = implode(" OR id =", $result['userid']);

//get total number of users
$numrow = mysql_num_rows($query2);

//use string to get users details from "login" database
$query =mysql_query("SELECT * FROM login WHERE id ='$string'");







echo ('
<html>
<head>
<style type="text/css">

td {
//padding-left: 10px;
//padding-right: 10px;
}

table {
border: solid grey 2px;
margin: 5px;
}

</style>
</head>
<h1> ads for approval: ' . $numrow . ' </h1> 
('.$string.') ');
print_r($result); 
echo('
<br><br>
');


while ($result=mysql_fetch_array($query)) {

echo ('
<table width="35%">
        <tr>
                <td width="20%"><p>user: </p>' . $result['username'] . ' </td> 
                <td width="20%"><p>club: </p>' . $result['club'] . ' </td> 
                <td width="20%"><p>city: </p>' . $result['city'] . ' </td> 
                <td width="20%"><p>night: </p>'. $result['night'] . ' </td> 
                <td width="20%"> <a href="home.php"> Approve</a> </td> 
        </tr>
</table>
<hr>
');
}
echo ('

<a href="http://www.down2party.com/Nathan/home_page/loginpage.php">Return to home</a>





</html>');
} else {echo ('you are not authorised');}
?>
3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by |-|x
2

I have changed line 16, 20 and 26 yof your code above.
1) In 16 you are fething your row only once, you need loop through query2 result.
2) In 20 now I am using new array I set in loop, to implode.
3) In 26 I have removed single quotes around $string variable.

<?php
$username = $_COOKIE['user'];
@mysql_connect ("localhost","down2par_down2pa","4m329aMh") or die ("could not connect to MySQL");
@mysql_select_db ("down2par_d2pdb") or die ("no database");
$query1 =mysql_query("SELECT * FROM login WHERE username = '$username'");
$user = mysql_fetch_array($query1);
$admin = $user['admin'];
if ($admin==1){
//get user id from "approval" database 

$query2 =mysql_query("SELECT * FROM approval WHERE approved = '0'");
unset($arruserid);

while($result =mysql_fetch_array($query2))
{
  $arruserid[]=$result['userid'];   

}
//turn array of ids into a string
$string = implode(" OR id =", $arruserid);


get total number of users
$numrow = mysql_num_rows($query2);
//use string to get users details from "login" database
$query =mysql_query("SELECT * FROM login WHERE id =$string");






echo ('
<html>
<head>
<style type="text/css">
td {
//padding-left: 10px;
//padding-right: 10px;
}
table {
border: solid grey 2px;
margin: 5px;
}
</style>
</head>
<h1> ads for approval: ' . $numrow . ' </h1> 
('.$string.') ');
print_r($result); 
echo('
<br><br>
');
while ($result=mysql_fetch_array($query)) {
echo ('
<table width="35%">
        <tr>
                <td width="20%"><p>user: </p>' . $result['username'] . ' </td> 
                <td width="20%"><p>club: </p>' . $result['club'] . ' </td> 
                <td width="20%"><p>city: </p>' . $result['city'] . ' </td> 
                <td width="20%"><p>night: </p>'. $result['night'] . ' </td> 
                <td width="20%"> <a href="home.php"> Approve</a> </td> 
        </tr>
</table>
<hr>
');
}
echo ('
<a href="http://www.down2party.com/Nathan/home_page/loginpage.php">Return to home</a>
</html>');
} else {echo ('you are not authorised');}
?>

Edited by urtrivedi: forgot ) in while loop,

0

implode needs to be passed an array in order to work properly, what you are doing is passing a single element of an array
$result['userid'] will return the value of the userid for the first record. $result is in fact only the first record of the result set presented as an array.

Unfortunately I don't believe implode can be used to achieve what you need.
What you will need to do is loop through each record, and manually append the result value to your new query string.

for example

while ($row = mysql_fetch_array($query2)) {
  if (!empty($string))
    $string .= " OR "
  $string .= "id =".$row['userid'];
}
0

perfect! thanks urtrivedi, just to clarify so i understand in the future.
you made an empty array with $arruserid[] and added into it with $result['userid'] and because you had the $results in a while it gave all values for that array one at a time, not just the first.

0

$arruserid[] is used to automatically call append new useid value to array , it will give index itself from 0,1,2....n

it is similar to following code

$i=0;
 while($result =mysql_fetch_array($query2))
 {
   $arruserid[$i]=$result['userid'];   
   $i++;
 }
0

ok understand. thanks. Just wondering if you could help me with one more thing. i am not sure if i can do this, each time the new data is printed out in the while it is printed into a form in the form there is a submit button. i need to connect the submit button with the data in that particular form. how would i go about that?
At the moment i have a hidden field saying <input type="hidden" value="'. $result['id'] . '"> but that does not work. when i try to use that variable it is empty.

0

I think that should work, but you will need to give the hidden field a name in order to identify and grab it from the POST array.

<input type="hidden" name="id" value="'. $result['id'] . '">

then you will be able to access the data like so: $_POST['id']

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.