Printing a MySQL query in PHP - php

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());

Related

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.

Query Result: What is $row[0]

I know this may sound like a stupid question from a programming-newbie, but I just want to make sure I understand correctly.
After a query, what does $row[0] stand for/ result in?
Is my understanding correct that $row[0] shows ALL results?
HERE ARE EXAMPLES:
$query = "SELECT count(commentid) from comments where jokeid = $jokeid";
$result = mysql_query($query);
$row=mysql_fetch_array($result);
if ($row[0] == 0)
{
echo "No comments posted yet. \n";
} else
{
echo $row[0] . "\n";
echo " comments posted. \n";
AND THIS ONE
$query = "Select count(prodid) from products where catid = $catid";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if ($row[0] == 0)
{
echo "<h2><br>Sorry, there are no products in this category</h2>\n";
}
else
{
$totrecords = $row[0];
Thanks in advance.
$row[0] will simply echo the first column in your database.
0 is the first because all arrays in PHP (and in most programming languages) are zero-based - they simply start with zero.
$row[0] will be the value of the first column in your results. If you use mysql_fetch_assoc($result) you will have an array in the form:
array(column_name => column_value);
e.g.
$row = mysql_fetch_asssoc($result);
$value_column_1 = $row['column_1'];
You can also use mysql_fetch_object($result) to get an object with column names as the parameters.
$row = mysql_fetch_object($result);
$value = $row->column_name
mysql_fetch_array() takes the next (in your examples first) row out of the resultset and stores the data in an array $row.
$row[0] now represents the first value of that row.
So in total in your examples the variable holds the first value of the first row of your resultset.

Figuring out why I am getting a Resource ID #5 error

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);

Displaying one result on a query with LIMIT 10

Today i'm makeing this new question because this is not posible to display one result on a query with limit 10.
Here is the query:
$query = "SELECT * FROM articol WHERE status = 1 ORDER BY data DESC LIMIT 10";
$result = mysql_query($query) or die ("Could not execute query");
while($row = mysql_fetch_array($result))
{
$id = $row["id"];
$titlu = $row["titlu"];
$data = $row["data"];
$desc = $row["continut"];
$part = strip_tags($desc);
}
And this is the echo to display
<link>http://dirlink.ro/articol.php?art_id=<?php echo $id; ?></link>
<title><?php echo $titlu; ?></title>
<description><?php echo substr($part,0,180); ?> ...{Citeste tot} </description>
<pubDate><?php echo $data; ?></pubDate>
The same code is putted on other page, for other category of my website and it's work just fine. I don't understand why at this section is echoing only one result.
All you are doing is reassigning the variables, you are not outputting them, so you will only ever end up outputting the last result.
What you need to do is call them inside the while loop:
$query = "SELECT * FROM articol WHERE status = 1 ORDER BY data DESC LIMIT 10";
$result = mysql_query($query) or die ("Could not execute query");
while($row = mysql_fetch_assoc($result)) {
$id = $row["id"];
$titlu = $row["titlu"];
$data = $row["data"];
$desc = $row["continut"];
$part = strip_tags($desc);
print "<link>http://dirlink.ro/articol.php?art_id=$id</link>\n"
."<title>$titlu</title>\n"
."<description>".substr($part,0,180)." ...{Citeste tot} </description>\n"
."<pubDate>$data</pubDate>\n";
}
Edited a little after I read the question properly... [blush]
I think you want to use mysql_fetch_assoc($result) for named elements, but I could be mistaken.
your echos need to be in the loop if you are trying to list them all; otherwise your values are going to be overwritten and only the last row will be displayed.
I have fixed it: I checked the RSS field with rssvalidator.org and there it showed me the errors and how to fix it.
So I fixed the error, which seem to originate from some &circi;, which is a character of a Romanian language.

SQL query is only retrieving first record

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 );
}

Categories