The $pKeyArray only prints first row from the database which meets the WHERE clause, why isn't showing all rows which meets the WHERE clause, I don't want to put it in any loop, i just need is an array of P_Key of all rows.
$getPKey = "SELECT P_Key FROM likersTable WHERE executed=0";
$PKeyDetails = mysqli_query($dbConnect,$getPKey)
or die('Some error in post id');
$pKeyArray = mysqli_fetch_array($PKeyDetails);
print_r($pKeyArray);
You need to call mysqli_fetch_array() for each row.
while ($pKeyArray = mysqli_fetch_array($PKeyDetails)) {
print_r($pKeyArray);
}
Use mysqli_fetch_all:
$array = mysqli_fetch_all( $PKeyDetails);
You have to use a loop because the mysqli_fetch_*() functions only returns one row per call.
Use this code:
$getPKey = "SELECT P_Key FROM likersTable WHERE executed=0";
$PKeyDetails = mysqli_query($dbConnect,$getPKey)
or die('Some error in post id');
while ($row=mysqli_fetch_array($PKeyDetails))
{
// Do something with $row
}
Or use mysqli_fetch_all():
$result = mysqli_fetch_all($PKeyDetails, MYSQLI_ASSOC); // or use MYSQLI_NUM
while($pKeyArray = mysqli_fetch_array($PKeyDetails)) {
print_r($pKeyArray);
}
Related
I am trying to get the values using the below code
$query="select document_id from certificate_documents where certificate_id=$certificate_id";
$res = db_query($query);
$row_count = db_num_rows($res);
$doc_id=array();
for($j=1;j<=$row_count;$j++){
$document_copy = db_fetch_object($res);
$doc_id[$j]=$document_copy->document_id;
print "$doc_id[$j]";
}
But the above code print nothing.
I have to use this value into another query . How can i get this? please help.
You can try this:
$query="select document_id from certificate_documents where certificate_id=$certificate_id";
$res = db_query($query);
$doc_id=array();
while($document_copy = db_fetch_object($res)) {
$doc_id[]=$document_copy->document_id;
}
print_r($doc_id);
I am trying to query a db for an entire column of data, but can't seem to get back more than the first row.
What I have so far is:
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_fetch_array($medicationItemObj, MYSQLI_NUM)){
echo count($row);
}
It's not my intention to just get the number of rows, I just have that there to see how many it was returning and it kept spitting out 1.
When I run the sql at cmd line I get back the full result. 6 items from 6 individual rows. Is mysqli_fetch_array() not designed to do this?
Well, I had a hard time understanding your question but i guess you are looking for this.
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_num_rows($medicationItemObj))
{
echo $row;
}
Or
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
$i = 0;
while ($row = mysqli_fetch_array($medicationItemObj))
{
$medicationItem[] = $row[0];
$i++;
}
echo "Number of Rows: " . $i;
If you just want the number of rows i would suggest using the first method.
http://php.net/manual/en/mysqli-result.num-rows.php
You can wrote your code like below
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
while ($row = mysqli_fetch_assoc($medicationItemObj))
{
echo $row['medication'];
}
I think this you want
You could give this a try:
$results = mysqli_fetch_all($medicationItemObj, MYSQLI_NUM);
First, I would use the object oriented version of this and always use prepared statements!
//prepare SELECT statement
$medicationItemSQL=$connection->prepare("SELECT medication FROM medication");
// execute statement
$medicationItemSQL->execute();
//bind results to a variable
$medicationItemSQL->bind_result($medication);
//fetch data
$medicationItemSQL->fetch();
//close statement
$medicationItemSQL->close();
You can use mysqli_fetch_assoc() as below.
while ($row = mysqli_fetch_assoc($medicationItemObj)) {
echo $row['medication'];
}
I'm using PHP and I do a mysql query and I save the result of this query in an array. Then, I use a loop and into the loop i do another mysql query using the values of the array in the where clause. It's right but if I try to get the result of the query outside the loop I can't.
Here an example code
$result=$mysqli->query("SELECT code FROM referee");
$i=0;
$arcode=array();
while($row=$result->fetch_array()){
$arcode[$i]=$row["code"];
$i++;
}
for($j=0;$j<sizeof($arcode);$j++){
$result2=$mysqli->query("SELECT code, time FROM match where referee_code IN ($arcode[$j])");
}
/*Here I can't get the result values*/
$matcode=array();
$hour=array();
$k=0;
while($row2=$result2->fetch_array()){
$matcode[$k]=$row2["code"];
$hour[$k]=$row2["time"];
}
If I put all in the same loop I get the result repeated (This code is one example of all my code but the idea is the same in the rest).
You are overwriting the result set values into $result2.
The correct method would be something like:
$matcode = array();
$hour = array();
$result2 = $mysqli->query("SELECT code,
time
FROM `match`
WHERE referee_code IN (SELECT code
FROM referee)");
while ($row2 = $result2->fetch_array())
{
$matcode[] = $row2["code"];
$hour[] = $row2["time"];
}
You may change the index values according to the way you want the array to look like.
Also match is a reserved word. So you would have to enclose it in backticks.
If you want to print complete data then try this :
$matcode = array();
$hour = array();
$result2 = $mysqli->query("SELECT code,
time
FROM `match`
WHERE referee_code IN (SELECT code
FROM referee)");
$completeData=array();
while ($row2 = $result2->fetch_array())
{
$completeData[] = $row2;
}
print_r($completeData);
Don forget to accept answer if it helps :)
I need to print one row from a table so a while loop isn't necessary, is there any other method?
You need not while.
Just do your while condition outside while 1 time.
i.e
$a=mysql_fetch_row($sql);
//use $a
instead of
while($a=mysql_fetch_row($sql)){
//use $a
}
if (($dbResult = mysql_query("SELECT ... FROM ... LIMIT 1")) !== false)
{
$row = mysql_fetch_array($dbResult);
echo $row['Column_Name'];
}
Just fetch one row, no need to always loop a retrieval.
$results = mysql_query("SELECT * FROM my_table WHERE user_id = 1234");
$row = mysql_fetch_assoc($results);
echo ($row['user_id']);
Do what you would have done inside the condition of your loop, and you'll be fine.
i have a query that should return 40 results:
$test = mysql_query(" SELECT fname FROM online_all WHERE verify_status = 'v' LIMIT 40 ");
if (!$test ) {print " - Mysql Error - ";echo fns_et_mysql_error(mysql_error());}
if i echo mysql_num_rows($test); i get 40. which means that i get the results
but when i print_r $roww = mysql_fetch_array($test); i get only one result.
there are no php errors.
You have to loop through the results with:
while($row = mysql_fetch_array($test)) {
// awesomeness with $row
}
Try using a loop.
while($row = mysql_fetch_array($test)){
}
mysql_fetch_array returns one row at a time. If you want to loop through all of them you do the following:
while ($row = mysql_fetch_array($test))
//Do something
http://php.net/manual/en/function.mysql-fetch-array.php
Check the documentation. mysql_fetch_array only fetches one row of data at a time. To get the data for all 40 rows you need a loop (like a for loop conditioned by mysql_num_rows).
You need to iterate the results from the query.
while($row = mysql_fetch_array($test))
{
// This is how you print fname.
echo $row[0];
}
Please refer to the documentation where there's also many useful tips of how you can use this function.
http://php.net/manual/en/function.mysql-fetch-array.php
You should also know that there exist another useful function for fetching results from mysql queries that fetches results as an associative array.
http://php.net/manual/en/function.mysql-fetch-assoc.php
You have to loop thru the rows:
while($row = mysql_fetch_array($test)){
var_dump($row);
//etectera
}