Jump to content

Mysql WHERE statement syntax

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Fearless

Fearless

    Newbie

  • Members
  • Pip
  • 3 posts
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?

#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
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.
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

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
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.
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

    Newbie

  • Members
  • Pip
  • 3 posts
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';"?

#6
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
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.
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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users