I want to get all emp_ids of employees which is manager_id equal to login user_id. But why this query show only first result of the column. I could not able to find the problem.
$userID=$_SESSION['userID'];
var_dump($userID);
$result1 = mysql_query("SELECT emp_id FROM employee where manager_id='$userID' ORDER BY emp_id");
$array = mysql_fetch_array($result1);
$id1=$array['emp_id'];
Please help !
You should loop over the result
and eg: you can store all the empID in an array
$cnt = 0;
while ($row = mysql_fetch_array($result1)) {
echo "ID: " . $row[0] ;
$myArrayOfEmpID[$cnt] = $row[0];
$cnt++;
}
instead you only set
$array = mysql_fetch_array($result1);
$id1=$array['emp_id'];
Related
This query returns 13 individual arrays:
$array = array($pgff_id, $pgfm_id, $pgmf_id, $pgmm_id, $mgff_id, $mgfm_id, $mgmf_id, $mgmm_id, $pgf_id, $pgm_id, $mgf_id, $mgm_id, $fid, $mid);
foreach($array as $id) {
$stmt = $db->prepare("SELECT birth_year, death_year FROM index WHERE id = ?");
$stmt->execute([$id]);
$data = $stmt->fetch(PDO::FETCH_ASSOC);
print_r shows that they look like this:
Array ([birth_year] => 1750 [death_year] => 1824)
Array ([birth_year] => 1770 [death_year] => 1836)
... etc
Is it possible to assign a number or name to these individual arrays? The results are not useful without a way to identify them.
I tried doing it like shown below. This way does number the arrays but orders the results as they are found in the table. I really need the results ordered as they are in $array (which the first method does manage).
$in = str_repeat('?,', count($array) - 1) . '?';
$sql = "SELECT birth_year, death_year FROM index WHERE id IN ($in)";
$stmt = $db->prepare($sql);
$stmt->execute($array);
$data = $stmt->fetchAll();
Taking your code and adding in id as an expression in the query would result in this:
$in = str_repeat('?,', count($array));
$sql = "SELECT id, birth_year, death_year FROM index WHERE id IN ($in)";
$stmt = $db->prepare($sql);
$stmt->execute($array);
$rows = $stmt->fetchAll();
foreach ($rows as $row) {
echo "here starts another row:<br>";
echo "id = ".$row["id"]."<br>";
echo "birth_year = ".$row["birth_year"]."<br>";
echo "death_year = ".$row["death_year"]."<br>";
}
So, that's how you can access it.
You can rearrange the data in the rows after you've received them from the database, again by using a foreach loop:
$birth = [];
$death = [];
foreach ($rows as $row) {
$id = $row["id"];
$birth[$id] = $row["birth_year"];
$death[$id] = $row["death_year"];
}
Now you can access both arrays to get the birth or death year based on the id like this:
echo $birth[4]. 'and '. $death[4];
where id is 4.
I'm looking for a way to search a MySQL database for specific values and put these in an array.
The contacts table has name,email,group,Phone...
I would like to search the database by group and return the email adresses in an array, separated by , (comma) to use further in my code.
What is the best way to do this?
$result = mysqli_query($link,"SELECT * FROM Contacts WHERE Group='Group 1'")
or die(mysqli_error());
...
while($row = mysqli_fetch_array( $result ))
{
array ( row->email,...)
}
Here you have different options beginning with:
Update your select query to only display the email, if you don't require the other fields this is the way to go
In your loop just add only the email field to the array
$dataArray[] = $row->email;
$result = mysqli_query($link,"SELECT email FROM Contacts WHERE Group='Group 1'")
or die(mysqli_error());
$emailArray = [];
while($row = mysqli_fetch_array( $result ))
{
array_push($emailArray,$row->email);
}
$responseEmail = implode(",", $emailArray);
Hope this will works!
I found the way of what I was trying to do. I needed a selectbox with values from the database.
....
$query = mysqli_query($link,"SELECT * FROM XXX WHERE category='$category' AND stelplaats='$stelplaats'");
echo '<td><select name ="collega" required>';
echo '<option value"">---</option>';
while ($row2 = mysqli_fetch_array( $query ))
{
echo '<option value =" '.$row2['id'].'"';
echo '>'.$row2['name'].'</option>';
}
echo '</select></td></tr>';
....
I'm trying to get an array of id's from my database and then be able to echo out each id.
Something like this:
$query = mysql_query("SELECT id FROM TableName WHERE field = 'test' ORDER BY id DESC") or die(mysql_error());
$row = mysql_fetch_array($query);
echo "array: ".$row[1]." <br>";
echo "array: ".$row[2]." <br>";
echo "array: ".$row[3]." <br>";
This doesn't seem to be working though?
The problem is that mysql_fetch_array fetches an ARRAY, which is 0-based. You're fetching a single field from the database, which will be stored at $row[0] in your result array. Since you're echoing out only row[1] through row[3], you'll never see the result:
$row = mysql_fetch_array($query);
print_r($row);
should give you:
Array (
0 => 'id_field_value_here'
)
and
echo $row[0]
would also output
id_field_value_here
mysql_fetch_array fetches 1 row. You need to do something like
...
$res = array();
while ($row = mysql_fetch_array($query))
{
$res[] = $row;
}
//now $res[0] - 1st row, $res[1] - 2nd, etc
here is the code that i am using, the code is simply getting the same value by two different methods, but the mysql_insert_id() echo is not giving the output.
The value given by -> echo mysql_insert_id(); is 0
Can anyone tell me the possible reason and how to resolve this problem?
include'config.php';
$query = "SELECT id
FROM users
ORDER BY id DESC
LIMIT 1";
if($query_run = mysql_query($query))
{
$query_result = mysql_fetch_row($query_run);
$id = $query_result[0];
echo $id;
echo $lastId = mysql_insert_id().'<br />';
//'Query Successful!';
} else {
echo mysql_error();
}
If you are looking for the next auto increment id that will be inserted. I guess you should try this.
$query_autoinc="SHOW TABLE STATUS LIKE 'mytable'";
$exec_autoinc=mysql_query($query_autoinc);
$row_autoinc=mysql_fetch_assoc($exec_autoinc);
$inserted_id = $row_autoinc['Auto_increment'];
Or if you want the last inserted id, you might try this.
$query= "SELECT max(id)
FROM users
ORDER BY id DESC
LIMIT 1";
What's the best way to check whether the value is in the database?
Am I doing it correct?
$result = mysql_query("SELECT COUNT(*) FROM table WHERE name = 'John'");
$count = count($result);
you could use straight forward ,
mysql_num_rows() ;
eg :
$con = mysql_connect($host,$uname,$passwd)
mysql_select_db($dbase,$con);
$result = mysql_query($query,$con);// query : SELECT * FROM table WHERE name='jhon';
if( ! mysql_num_rows($result)) {
echo " Sorry no such value ";
}
Yes you are doing it right, if you are only concerned with checking if there are any records where name='john'
SELECT COUNT(*) FROM table WHERE name = 'John'
will return the no. of records where name field is 'John'. if there are no records then it will return 0, and if there are any records it will return the number of records.
But the above query will miss the entries where name is 'John Abraham' or 'V john', to include even these
you can modify your query like this.
SELECT COUNT(*) FROM table WHERE name like '%John%'
I'd say yes.
$result = mysql_query("SELECT COUNT(*) AS 'nb' FROM table WHERE name = 'John'");
$line = mysql_fetch_array($result, MYSQL_ASSOC);
$count = $line['nb'];
Will give you the number of matching rows.
$result = mysql_query("SELECT COUNT(*) as user FROM table WHERE name = 'John'");
$line = mysql_fetch_array($result, MYSQL_ASSOC);
$count = $line['user'];
if($count!=0)
{
echo "user exists";
}
else
{
echo "There is no such user";
}