How to print selected value from database - php

I am selecting a value from database and want to print it in the html page. I have tried this piece of code:
$queryreg = mysql_query("SELECT available FROM hospital WHERE h_name='$hospital' ");
$a= $queryreg;
echo $a;
But the result is showing as "Resource id #7". Please help me out

$queryreg = mysql_query("SELECT available FROM hospital WHERE h_name='$hospital' ");
$queryreg = mysql_fetch_assoc($queryreg);
$a= $queryreg;
echo $a['available'];

mysql_query() returns a resource on success. You will have to use mysql_fetch_row to retrieve values that returns a numerical array that corresponds to the fetched row and moves the internal data pointer ahead.
$queryreg = mysql_query("SELECT available FROM hospital WHERE h_name='$hospital' ");
if($queryreg)
{
if(mysql_num_rows($queryreg) > 0)
{
$a = mysql_fetch_row($queryreg);
echo $a[0];
}
else
echo "No records found.";
}
else
echo "Cannot fetch records ".mysql_error();
In cases when there are multiple rows returning from query, you can use mysql_fetch_array() that fetches a result row as an associative array, a numeric array, or both
$queryreg = mysql_query("SELECT available FROM hospital WHERE h_name='$hospital' ");
if($queryreg)
{
if(mysql_num_rows($queryreg) > 0)
{
while($result = mysql_fetch_array($queryreg))
{
echo "<br>".$result[0];
}
}
else
echo "No records found.";
}
else
echo "Cannot fetch records ".mysql_error();

Related

in_array Finding first Value Only

What I'm trying to do is to assign variables based upon a sql query.
The field "County" is a varchar containing the names of Counties.
$sql="SELECT County from GAINLP WHERE SpeciesName LIKE '%Actias_luna%'";
($result=mysqli_query($connect,$sql));
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if (in_array("Porter", $row)) {
$Porter='Present';
}
else {$Porter='Not Present';
};
echo $Porter;
What happens is that in_array is only detecting the very first county in the array. If I echo $row, I get this:
OwenPikeParkeOwenOwenScottMorganGreeneHendricksLawrenceSt.JosephWashingtonVigoMorganOwenDuboisJeffersonSwitzerlandMadisonGreeneFayetteMarionOrangeParkeClarkJeffersonFayetteFountainMontgomeryHendricksHowardOwenElkhartMarionHendricksWashingtonTippecanoePutnamWashingtonBrownHendricksJenningsOwenWhitleyKosciuskoPorterVermillionHendricks
If I assign the needle as "Owen," echo $Porter returns "Present". All other values echo "Not Present." If I loop it, even "Owen" returns "Not Present" to $Porter. What am I doing wrong here?
This also does not work:
$sql="SELECT County from GAINLP WHERE SpeciesName LIKE '%Actias_luna%'";
($result=mysqli_query($connect,$sql));
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
if (in_array("Porter", $row)) {
$Porter='Found it';
}
else {$Porter='Didn\'t Find it';
}};
echo $Porter;
the line
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
will only retrieve the first row of the database.
If you want to check every row to see if ANY of them are Porter, then you will need to set the flag to a default of "no", loop your results, and set the flag if you find it:
$Porter="Not Present";
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
if($row['County'] == "Porter"){
$Porter = "Present";
}
}
echo $Porter;
(this is just a basic example; you can optimize it farther by breaking out of the loop when you first find the result for example, but this will do the job)
mysqli_fetch_array only fetches one row. Use while loop to go over all rows:
while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
If you don't want to check per row but want to check if the value exists in any of the rows use mysqli_fetch_all:
$sql="SELECT County from GAINLP WHERE SpeciesName LIKE '%Actias_luna%'";
$result=mysqli_query($connect,$sql);
$rows=mysqli_fetch_all($result,MYSQLI_ASSOC);
if (in_array("Porter", $rows)) {
$Porter='Present';
} else {
$Porter='Not Present';
};
echo $Porter;

While loop not working for every index

I am new to php, but I had used while loop and it worked. But in this case i don't know why it is not working.
I am using following query to fetch data from mysql data base.
$query="SELECT * FROM `groupMembers` WHERE group_id = '$group_id'";
if ($result = mysql_query($query)) {
# code...
echo mysql_num_rows($result);
}
It prints 2 as number of rows. But the problem is in following while loop:-
while ($data = mysql_fetch_array($result)) {
# code...
echo $data['member_id'];
}
I prints only one member's id. (the second one == member_2)
The above query returns 2 rows when run in mysql :-
member_id | group_id
----------|----------
member_1 | group_1
member_2 | group_1
i had same problem,i was calling following code two times
mysql_fetch_array($result)
Show more code - what happens before while
first try mysql_num_rows in how many rows is returns.
if "0" then can't fetch any record from your mysql and check your sql query. if return no of row then try below code:
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. "<br>";
}
} else {
echo "0 results";
}
Enjoy Coding.
That is because when it create array for your MySQL query it will change me with to same value "group_1"
Try to convert mysqli_fetch_array to mysqli_fetch_assoc instead
$query="SELECT * FROM `groupMembers` WHERE group_id = '$group_id'";
Here,$group_id can't be place between '';
use $results->fetch_assoc(); which get your data
<?php
// your database connection
if ($results->num_rows > 0) {
while ($row = $results->fetch_assoc()) {
echo 'Member Id:' . $row["member_id"] . '<br>';
}
} else {
echo " No Result";
}
?>

Select query returning only 1 of 2 requested columns

I'm 90% sure the problem is in the sql syntax, but can't seem to spot where exactly. The sql is returning the correct email, however the password returns empty. I have tried using a different column in the database but I get the same problem.
Note: The password is hashed in db.
Code:
$sql = $link->prepare('SELECT email, password FROM users WHERE email=:email');
$sql->bindParam(':email', $email);
$sql->execute();
$resultemail = $sql->fetchColumn();
$resultpassword = $sql->fetchColumn(1);
if (!$resultemail){
echo "empty";
} else {
echo $resultemail;
}
if (!$resultpassword){
echo "empty";
} else {
echo "$resultpassword";
}
Each time you call fetchColumn, it moves to the next row of the result set, and fetches the specified column from that row (the default is column 0). So you're setting $resultemail to the first column of the first row of the results, and $resultpassword to the first column of the second row of the results. But since the query only returns one row, you're setting $resultpassword to false.
You should fetch the whole row, and set the variables to the corresponding columns of that row.
$row = $sql->fetch(PDO::FETCH_ASSOC);
if ($row) {
$resultemail = $row['email'];
$resultpassword = $row['password'];
if (!$resultemail){
echo "empty";
} else {
echo $resultemail;
}
if (!$resultpassword){
echo "empty";
} else {
echo "$resultpassword";
}
} else {
echo "empty";
}

Not enter while loop after mysql_fetch_assoc

please take a look at this code :
$sql = "SELECT * FROM shop";
$result = mysql_query($sql);
echo $result;
echo "before lop";
while ($xxx = mysql_fetch_assoc($result)) {
echo "inside lop";
echo $xxx['column_name'];
}
echo "after lop";
When I run such code i receive :
Resource id #244
before lop
after lop
It did not enter while lop, and I really don't know why :(
I used before such code and there were no problems.
Can someone help me?
$sql = "SELECT * FROM shop";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);
Check how many records are present in your shop table. I think shop table is empty.That is why not entering in the while loop.
You can do like this
$count = mysql_num_rows($result);
if($count > 0) {
while ($xxx = mysql_fetch_assoc($result)) {
echo $xxx['column_name'];
}
}
I would guess that the call to mysql_fetch_assoc() has returned false, possibly due to no results being returned from the database, this would cause the while loop to not execute even once. I would check the output of var_dump(mysql_fetch_assoc($result)) to ensure that data has been returned.

Display mysql data in php function

Ok i got a problem now i want to display a data from the database and display it through a function now how do i do that??
like i have fetched a row from the database and its name is $row_field['data']; and it is correct now i have assigned a variable to it like this $data = $row_field['data']; now if i call it in a function it shows undefined variable even after i assigned it global in the function like this
function fun(){
global $data;
echo $data;
}
but if i assign it a value like 1 or 2 or anything it gets displayed without any error why is that so??
If it displays if you assign it a value like 1 or 2 while still in the global scope, then I can only assume that your database did not return the result you thought it did. Does the database value display if you echo it out outside of the function?
Global is evil. I dont know what you are trying to do, but why dont you just do the query in the function itself?
If you have a column named data and your php call was something like
$result = mysql_query("SELECT data FROM mytable");
while ($row_field = mysql_fetch_assoc($result, MYSQL_NUM)) {
...
}
Then you could replace ... with print $row_field['data'].
Else please provide a snippet of your code where you query the db and retrieve the result.
When learning php try to start with simple things. For example in order to get some data from a database follow the examples from php website.
<?php
$conn = mysql_connect("localhost", "mysql_user", "mysql_password");
if (!$conn) {
echo "Unable to connect to DB: " . mysql_error();
exit;
}
if (!mysql_select_db("mydbname")) {
echo "Unable to select mydbname: " . mysql_error();
exit;
}
$sql = "SELECT id as userid, fullname, userstatus
FROM sometable
WHERE userstatus = 1";
$result = mysql_query($sql);
if (!$result) {
echo "Could not successfully run query ($sql) from DB: " . mysql_error();
exit;
}
if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
mysql_free_result($result);
If all this goes well go a little further change a little the while loop.
$myArray = array();
while ($row = mysql_fetch_assoc($result)) {
$myArray[] = $row;
}
mysql_free_result($result);
// now you can start playing with your data
echo $myArray[0];
Small steps...

Categories