?
Solved

Listing MySql Database Entries

Posted on 2003-04-01
4
Medium Priority
?
198 Views
Last Modified: 2006-11-17
I have a PHP script that contains the following code. It's purpose is to select and show records from a database table named 'streets_articles' all those records whose 'article_id' is zero. Running this code gives the output:

0011

This is because there are two articles with the id 0 and two with the id 1. What I want to be produced is:

0011
00

Because the 'if' statement says if the id is 0, print the id again. What seems to happen is the first print statement works fine, but for some reason when the 'if' clause is reached, the article_id field doesn't have a zero in it.

Any ideas on how to make this work?

The second thing I want to do is only to display the first four records in the table that have an index of 0. Is this difficult?

Thanks

HERE IS THE CODE:

$str_requete = "SELECT page_id,article_id,article_type,page_type,page_text,page_img,article_title,article_date,articleimage FROM streets_articles ORDER BY article_date DESC";
 
$result_articles = mysql_query ($str_requete,$ezine_db) or ezine_mysql_die();

while ($articleDb =mysql_fetch_object($result_articles))
{

   print($articleDb->article_id);

   if($articleDb->article_id = 0) {

   print($articleDb->article_id);

   }
}
0
Comment
Question by:mi5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
VGR earned 280 total points
ID: 8244832
of course. Classical Pascal/BASIC guys error :D

you wrote an assignation, not a test

test operator for equality is == not =

so :

$str_requete = "SELECT page_id,article_id,article_type,page_type,page_text,page_img,article_title,article_date,articleimage FROM streets_articles ORDER BY article_date DESC";

$result_articles = mysql_query ($str_requete,$ezine_db) or ezine_mysql_die();

while ($articleDb =mysql_fetch_object($result_articles))
{

  print($articleDb->article_id);

  if($articleDb->article_id == 0) { // please note that this block {} is useless for one line only in the "then"

  print($articleDb->article_id);

  }
}
0
 

Expert Comment

by:cybertech_sk
ID: 8245222
i would like suggest you to use, this form of writing in php and c++ too:

if(0 == $articleDb->article_id)
{
   print($articleDb->article_id);
}

it's better to find this mistakes and it's safer too
0
 
LVL 15

Expert Comment

by:VGR
ID: 8245262
yes but no, this a "pis-aller".
if the language was using "natural" syntax and operators, it would be better...

like

if $articleDb->article_id=0 then {
  $articleDb->article_id := 1; // change to 1
} else {
}

silly parentheses (LISP influence ? :D ), == and = just make things more complexe than they should be. Moreover, ||, &&, & are totally confusing. Thanks God, the PHP people have left "normal" operators AND, OR and NOT ;-)

0
 

Author Comment

by:mi5
ID: 8245889
Thanks a lot - my stupidity earns you some easy points!! I should have remembered from my dabbles with Java. You're right about the Basic bit - I mainly use VB, so I was reading through the code again and again and just not seeing the mistake.

Anyway, the program works fine. Cheers.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question