PHP while loop Error - php

My query is right but its fetch zero result so why this while loop is print ones this statement No error please tell thanks in advance
while(mysql_fetch_array($query))
{ echo "<br>"."No Error"."<br>"; }

Please do a small debugging and put "echo mysql_num_rows($query);" just before that. It should tell you the exact number of records - and thus, the number of loops in while. mysql_fetch_array returns FALSE when no more records (or no records from the beginning).

mysql_fetch_array() method takes result of running a query on database as parameter rather than query itself and returns a row as an array.
Correct code would be:
$result = mysql_query($query);
while($row = mysql_fetch_array($result) )
{
echo "<br>"."No Error"."<br>";
}
For more details on mysql_fetch_array(), have a look at:
http://php.net/manual/en/function.mysql-fetch-array.php

Related

Query more slow

I do a query but does not show anything on the screen and when I opened the page where I do the query is slow and does not show anything
$query="SELECT * FROM Dettagli_macchina WHERE macchine_id='$macchine' and Email='$_SESSION[login_user]'";
$result = mysqli_query($conne,$query);
while($row=mysqli_num_rows($result)){
echo $row['COMPONENTE'];
}
mysqli_num_rows() will only use for getting no of rows not for row data.
You need yo use mysqli_fetch_*()
while($row=mysqli_fetch_array($result)){
echo $row['COMPONENTE'];
}
Why this query slow? Because you are using infinite loop here, always TRUE.
while($row=mysqli_num_rows($result))
One more story, I hope you are using session_start() in your file, but suppose that if $_SESSION not found or not start than your query will failed.
In last, this is just a suggestion regarding Naming Convention, you are using column name in small letter, capital small, full capital, this is not related to answer but you must need to learn about this art.
this will help you to understand Naming Convention: Database, Table and Column Naming Conventions?
This reference will help you to understand how mysqli_fetch_array() works: http://php.net/manual/en/mysqli-result.fetch-array.php
Please try with this one for return value and if you need number of raw then the statements will be different.
$query="SELECT * FROM Dettagli_macchina WHERE macchine_id='$macchine' and Email='$_SESSION[login_user]'";
$result = mysqli_query($conne,$query);
while($row=mysqli_fetch_array($result)){
echo $row['COMPONENTE'];
}
As others have pointed out, you have mixed up the mysqli_num_rows function, here I am using it to print the number of results found, then loop through the results after converting the mysqli result object to an array named $row
echo 'Found '. mysqli_num_rows($result) .' results';
while ($row = mysqli_fetch_array($result)) {
echo $row['COMPONENTE'];
}
You are fetching record but using mysqli_num_rows() which return total number of rows/records replace it with mysqli_fetch_array() here is the working example.
$query="SELECT * FROM Dettagli_macchina WHERE macchine_id='$macchine' and Email='$_SESSION[login_user]'";
$result = mysqli_query($conne,$query);
while($row = mysqli_fetch_array($result)) { //replace mysqli_num_rows with mysqli_fetch_array
echo $row['COMPONENTE'];
}

fetch on PDO returns only one row and first character. why?

i'm new on PDO with PHP and i want your help. i made a function with a select statement and if the query has value greater than 0 it returns the fetch.
$this->result->setFetchMode(PDO::FETCH_OBJ);
if ($this->result->rowCount() > 0) {
return $this->result->fetch();
}
and on my main program i have this
foreach ($category_results as $row){
echo .$row['id_category'].' '. $row['name'];
}
this command echo only 1 and F twice but if i put only $row it echo all the results correct e.g 1 Food, 2 Drinks. how i can echo the results separately like the example i have above? i want to put them in a list box
thank you
You are using:
$this->result->setFetchMode(PDO::FETCH_OBJ);
So you will have your rows stored in objects, also see the manual.
To get them, you can use:
echo .$row->id_category.' '. $row->name;
Also note that fetch only gets one row, to get all rows, use fetchAll. However, without seeing the complete code, I'm not sure whether you would need that.

Second while loop not running. Why?

I have two while loops running one after the other (not inside of each other) - I've simplified the code a bit so that only the important parts of it are listed below. The problem arises when I compare the two echoed queries because the 2nd while loop apparently isn't running at all.
I read somewhere that someone got around the problem by using a for loop for the second one but I want to get down to why exactly the second while loop is not running in my code.
$query_work_title = "SELECT title FROM works WHERE ";
while ($row = mysql_fetch_assoc($result_work_id)) {
$query_work_title .= "OR '$work_id' ";
}
echo $query_work_title;
echo '<br />';
$result_work_title = mysql_query($query_work_title) or
die(mysql_error($cn));
// retrieve the authors for each work in the following query
$query_author_id = "SELECT author_id FROM works_and_authors WHERE ";
while ($row = mysql_fetch_assoc($result_work_id)) {
$query_author_id .= "work_id = 'hello' ";
}
echo $query_author_id;
The MySQL extension keeps track of an internal row pointer for each result. It increments this pointer after each call to mysql_fetch_assoc(), and is what allows you to use a while loop without specifying when to stop. If you intend on looping through a result set more than once, you need to reset this internal row pointer back to 0.
To do this, you would mysql_data_seek() after the first loop:
while ($row = mysql_fetch_assoc($result_work_id)) {
$query_work_title .= "OR '$work_id' ";
}
mysql_data_seek($result_work_id, 0);
You've already looped through the result rows, so it's at the end and returns FALSE. (That's why it exited the loop the first time.)
To reset the internal pointer to the beginning of the result set, use mysql_data_seek().
mysql_data_seek($result_work_id, 0);
After the first while() loop completes, the internal pointer in the MySQL result is at the end of itself. You need to tell it to go back to the beginning using mysql_data_seek() between the first and second loops:
mysql_data_seek($result_work_id, 0);
You have already reached the end of your result set, but you can use mysql_data_seek to reset it.
// query your database
$result = mysql_query(...);
// loop through results
while(($row = mysql_fetch_assoc($result))) {
}
// reset result set
mysql_data_seek($result,0);
// loop again
while(($row = mysql_fetch_assoc($result))) {
}
According to your posted code, your SQL will look something like:
SELECT title FROM works WHERE OR '1'
That query will result in an error so your script shouldn't be getting past that point.
Even if it does, your second loop:
while ($row = mysql_fetch_assoc($result_work_id))
is using a result handle that has already been completely iterated by the first loop. By the the time the second loop tries to use it, mysql_fetch_assoc will return FALSE because there are no more rows to fetch. This will cause the second loop to exit immediately.
If both while loops need to access the same rows, combine their logic so the rows only need to be iterated over one time.
mysql_fetch_assoc steps through the results, right? It's already at the end on the second while loop, so it does nothing.

Using mysql_fetch_assoc and mysql_result together?

I am having great trouble trying to use mysql_fetch_assoc and mysql_result together in the same PHP script.
Originally (when not utilizing the mysql_result function) I was getting the required database values using a combination of mysql_query and mysql_fetch_assoc and everything was fine. Then i added 2 lines into my code to obtain certain ‘title’ field values using mysql_result.
Now if i run my script as it is below i will only receive 1 result even though there are 2 result. Then if i move my do/while loop up so that it is between the other 2 blocks of code (mysql_fetch_assoc and mysql_result lines) i will receive the desired 2 results.
I need my loop to come after the mysql_result section so putting the loop before it is not an option.
// connect to DB and get values
mysql_select_db($database, $mywebsite);
$query_not_related_before = "SELECT * FROM table limit 2";
$not_related_before = mysql_query($query_not_related_before, $ mywebsite);
$row_not_related_before = mysql_fetch_assoc($not_related_before);
// Extract just the results from the title field (the problem area!)
$before_essayid4 = mysql_result($not_related_before,0, 'title');
$before_essayid5 = mysql_result($not_related_before,1, 'title');
// Display results etc
do {
echo "<br />".$row_not_related_before['title']."<br />";}
while ($row_not_related_before = mysql_fetch_assoc($not_related_before));
Plase help,
Many thanks,
David
I am unsure if this will solve your problem but I think you should "seek" the result back.
mysql_data_seek ($not_related_before, 0)
Also, check out the warning on the mysql_result page:
Calls to mysql_result() should not be
mixed with calls to other functions
that deal with the result set.
Hope this helps ;)
You get one row because the first already requested here:
$row_not_related_before = mysql_fetch_assoc($not_related_before);
So I think you have to move result pointer to beginning:
mysql_data_seek($not_related_before, 0);
// Display results etc
do {
echo "<br />".$row_not_related_before['title']."<br />";}
while ($row_not_related_before = mysql_fetch_assoc($not_related_before));
A simpler solution would be to use 2D array's, i have always found the mysql functions to be a little cumbersome.
<?php
$result = mysqli_query(db_connect(),$query);
$result_out = array();
if(#mysqli_num_rows($result))
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC))$result_out[]=$row;
foreach($result_out as $row)
{
echo "<br />".$row['title']."<br />";
}
?>
hth

mysql single returned value

I have a mysql query:
$a=mysql_query("SELECT id FROM table1 WHERE p2='fruit' LIMIT 1");
This will only ever return one result or none.
I'm trying to first count the results, then if it 1, to pass the returned id to a variable.
Question: If I do
$results=count(mysql_fetch_assoc($a));
to count the number of rows returned, can I still do later
while($row = mysql_fetch_array($a)){
$id=$row['id'];
}
or will the first delete the array somehow???
Is their a better way to do all this?
You really not need to do anything if there is one row or null
Consider below code it will set id value if there is 1 row fetched otherwise it will be null
$id=''
while($row = mysql_fetch_array($a)){
$id=$row['id'];
}
No count needed.
$results=count(mysql_fetch_assoc($a));
does not count the number of rows as mysql_fetch_assoc returns one row. I believe you're looking for mysql_num_rows:
$results = mysql_num_rows($a);
$num_rows = mysql_num_rows($a);
You can then do an IF statement on this and later do a while loop on the fetched array
Have you tried mysql_num_rows ??
http://php.net/manual/en/function.mysql-num-rows.php
What happens when using:
while($row = mysql_fetch_array($a)){
$id=$row['id'];
}
it is just like reading a file,every time mysql_fetch_array($a) is called the next line is parsed untill it reaches the end of the file. A cursor of some sort is known in the background. Meaning that if you would enter by hand
//backgroundCursor = 0
$row = mysql_fetch_array($a)
//backgroundCursor = 1
$row = mysql_fetch_array($a)
//backgroundCursor =2
$row = mysql_fetch_array($a)
//backgroundCursor = 3
The while instruction just automates the stuff.
mysql_fetch_array increases the background cursor at every call so if you call it twice it will not give you the same line.
To get a clear view on this read something about reading from a file line by line.
No you cannot as each call to mysql_fetch_assoc() loads a new row, if there is any. You could indeed assign both variables at once:
$results=count($row = mysql_fetch_assoc($a));
but
But mysql_fetch_assoc($a)) returns false and as the result of count(false) is 1 this will not tell you anything. Besides, if there is no row all you really need is $row = mysql_fetch_assoc($a) and test if ($row) {...}.

Categories