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.
Related
I try to get the value 'entryScans' from my SQL-table and send it to $output based on the IF-ELSE case. What am I doing wrong?
<?php
include ('config.php');
$TicketNo=$_POST["TicketNo"];
$hash=$_POST["hash"];
$sql = "SELECT TicketNo,hash,entryScans FROM `Tickets` WHERE `TicketNo` = '$TicketNo' AND `hash` = '$hash'" or die(mysql_error());
$result = mysql_query($sql);
$row=mysql_num_rows($result);
if($row['entryScans'] = 0){
$output="ok";
}
else if ($row['entryScans'] > 0) {
$output="maybe";
else{
$output="error";
}
print(json_encode($output));
mysql_close();
?>
You are doing ...
if($row['entryScans'] = 0){
Which has 2 problems, = is assignement, == is testing equal to. The second part is that your fetching the results in...
$result = mysql_query($sql);
$row=mysql_num_rows($result);
So $row is the number of rows, $result is the query results...
So to check the number of rows in the result set
if($row == 0){
etc.
Update:
If you want the value of the column entryScans to be used, then you need to change the call of mysql_num_rows() to mysql_fetch_assoc(), so
$row=mysql_fetch_assoc($result);
Then you can leave the rest of the code to use $row['entryScans']
You are not processing your results. You just get the total number of rows in $row variable.
You need to process your query result by looping method.
Here is the example
while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
if($row['entryScans'] = 0)
{
$output="ok";
}
else if ($row['entryScans'] > 0)
{
$output="maybe";
}
else
{
$output="error";
}
}
Hope this works for you.
function MattsScript()
{
$query = 'SELECT * FROM `ACCOUNTING` WHERE `ACCTSTATUSTYPE` = "start" AND `Process_status` IS NULL LIMIT 0,100';
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);
while ($row = mysql_fetch_assoc($result))
{
echo $row['USERNAME'] . "<br />";
echo $row['ACCTSTATUSTYPE'];
}
}
I am trying to echo the results of a query. What I think is happening here is I am saving a query to a variable, the first 100 results (LIMIT 0,100) then using a loop to echo each row to the page.
However nothing is happening, no error and nothing written to the page.
Is there something I am missing?
if you are expecting only one result remove the while loop if not leave the while loop and remove the line $row = mysql_fetch_assoc($result); before the while loop. Also make sure you are querying your database correctly.
Example: $result = mysql_query($query) or die(mysql_error());
I have one record in a table in my mysql database. I use the following PHP code to retrieve data:
$result = mysql_query($query = "SELECT realname FROM t_user");
if($result)
{
while($data=mysql_fetch_assoc($result)){
echo $data['realname'];
}
}
the results do not appear, but when I use a do-while loop like below:
if($result)
{
$data=mysql_fetch_assoc($result);
do{
echo $data['realname'];
} while($data=mysql_fetch_assoc($result));
}
the results appear, then I tried to add one more record to the table, in the while loop, only shows one data record (the first record), and the do-while loop displays all the data. Why is that? Is it because there is my code wrong?
The code you have shown can't possibly exhibit this problem, so the logical explanation is this:
$result = mysql_query($query = "SELECT realname FROM t_user");
if($result)
{
// Something fetched a row from $result before this statement is run
while($data=mysql_fetch_assoc($result)){
echo $data['realname'];
}
}
Seeing how you're comparing two similar codes, you may have accidentally written something in between the two:
if($result)
{
mysql_fetch_assoc($result);
while($data=mysql_fetch_assoc($result)){
echo $data['realname'];
}
}
$result = mysql_query($query = "SELECT realname FROM t_user");
if($result) { while($data=mysql_fetch_assoc($result)){
echo $data["realname"]; } }
$result = mysql_query($query = "SELECT realname FROM t_user");
var_dump($data);die;
see the result array in your browser. the above while loop should work. check your result.
This is a part of my code, and the echo is to test the value and it gives me Resource ID #5
$id = mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail'") or die(mysql_error());
$counter = mysql_num_rows($id);
echo $id;
I am just getting into programming, and lately seeing lot of Resource ID outputs/errors while working with Databases.
Can someone correct the error in my code? And explain me why it isnt giving me the required output?
This is not an error. This is similar to when you try to print an array without specifying an index, and only the string "Array" is printed. You can access the actual data contained within that resources (which you can think of as a collection of data) using functions like mysql_fetch_array().
In fact, if there were an error here, the value of $id would not be a resource. I usually use the is_resource() function to verify that everything is alright before using variables which are supposed to contain a resource.
I guess what you intend to do is this:
$result = mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail'") or die(mysql_error());
if(is_resource($result) and mysql_num_rows($result)>0){
$row = mysql_fetch_array($result);
echo $row["id"];
}
Did you mean to echo $counter? $id is a resource because mysql_query() returns a resource.
If you are trying to get the value of the id column from the query, you want to use e.g., mysql_fetch_array().
Here is an excerpt from http://php.net/mysql.examples-basic:
$query = 'SELECT * FROM my_table';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// Printing results in HTML
echo "<table>\n";
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
Adapted to the code you provided, it might look something like this:
$result =
mysql_query("SELECT id FROM users WHERE firstname='$submittedfirstname' AND lastname='$submittedlastname' AND email='$submittedemail' LIMIT 1")
or die(mysql_error());
if( $row = mysql_fetch_array($result, MYSQL_ASSOC) )
{
$id = $row['id'];
}
else
{
// No records matched query.
}
Note in my code that I also added LIMIT 1 to the query, as it seems like you are only interested in fetching a single row.
are you looking for
while ($row = mysql_fetch_array($id)) {
echo $row['id'];
}
?
$kode_gel = substr($_GET['gel'],0,3);
$no_gel = substr($_GET['gel'],3,5);
$cek = mysql_query("SELECT id_arisan
FROM arisan WHERE kode_gel = '".$kode_gel."'
AND no_gel = '".$no_gel."'");
$result = mysql_fetch_array($cek);
$id = $result['id_arisan'];
header("location: ../angsuran1_admin.php?id=".$id);
I have a query which is designed to retireve the "name" field for all records in my "tiles" table but when I use print_r on the result all I get is the first record in the database. Below is the code that I have used.
$query = mysql_query("SELECT name FROM tiles");
$tiles = mysql_fetch_array($query);
I really cant see what I have done wrong, I have also tried multiple searches within google but I cant find anything useful on the matter at hand.
<?php
// Make a MySQL Connection
$query = "SELECT * FROM example";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
echo $row['name']. " - ". $row['age'];
echo "<br />";
}
?>
'mysql_fetch_array'
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
This means that it returns array (contains values of each field) of A ROW (a record).
If you want other row, you call it again.
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
// Do something with $row
}
Hope this helps. :D
Use "mysql_fetch_assoc" instead of "mysql_fetch_array".
$query = mysql_query('SELECT * FROM example');
while($row = mysql_fetch_assoc($query)) :
echo $row['whatever'] . "<br />";
endwhile;
I believe you need to do a loop to invoke fetch array until it has retrieved all the rows.
while ($row = mysql_fetch_array($query) ) {
print_r( $row );
}