Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Mysql WHERE statement syntax

mysql syntax

  • Please log in to reply
5 replies to this topic

#1 Fearless

Fearless

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 07 November 2011 - 06:57 AM

I'm a newbie PHP dabbler. I've hit a road block with this code:

echo $domainname; //Test to see if variable is correct.
$x= 'SELECT * FROM `domains`.`stats` WHERE domain = "$domainname"';
$stats = mysql_query($x);
$num_results = mysql_num_rows($stats);
echo "<td>$num_results</td>";


$num_results returns 0. If I take out the WHERE statement I get the total lines in the DB. If I take out the variable in the WHERE statement and put in a domain name, I get the correct number of rows in the DB for that domain name.
I've tried changing up the syntax for the query all sorts of ways and nothing works.

What am I doing wrong?
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 07 November 2011 - 06:59 AM

Problem is with this line:
$x= 'SELECT * FROM `domains`.`stats` WHERE domain = "$domainname"';

Because you've enclosed the string in single quotes, PHP does not parse it for PHP variables (i.e., $domainname). You will have to use double quotes in order to have this functionality, or, use string concatenation.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 Fearless

Fearless

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 07 November 2011 - 07:17 AM

If I change to $x= "SELECT * FROM `domains`.`stats` WHERE domain = $domainname";
I get a blank for the results.

---------- Post added at 09:17 AM ---------- Previous post was at 09:03 AM ----------

I got it with this:

$stats = mysql_query("SELECT * FROM `domains`.`stats` WHERE domain = '".$domainname."'");
$num_results = mysql_num_rows($stats);
echo "<td>$num_results</td>";


Thanks for your help.
  • 0

#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 07 November 2011 - 07:36 AM

The $domainname gets replaced with the value of that variable, but you still need quotes around it for the MySQL syntax.

$x= "SELECT * FROM `domains`.`stats` WHERE domain = '$domainname'";
Try that.

EDIT: Didn't see till after I posted that you got it with string concatenation. That'll work too.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5 Fearless

Fearless

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 08 November 2011 - 03:22 AM

I have another concatenation issue.


$SD = array();
foreach($SD as $value )
{
$SD[] = $value. ':50';
echo $SD; //:50 is not added to $SD
}


I'm trying to add :50 to an array of variables. Is something wrong with "$SD[] = $value. ':50';"?
  • 0

#6 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 08 November 2011 - 06:24 AM

Your loop is malformed. foreach doesn't run here because you have only declared $SD on the previous line, so $SD is empty. Not only that, but you're adding elements to the array with $SD[] inside the body of the loop, which isn't good practice.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Also tagged with one or more of these keywords: mysql, syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download