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?
5 replies to this topic
#1
Posted 07 November 2011 - 06:57 AM
|
|
|
#2
Posted 07 November 2011 - 06:59 AM
Problem is with this line:
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.
$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
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.
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
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.
EDIT: Didn't see till after I posted that you got it with string concatenation. That'll work too.
$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
Posted 08 November 2011 - 03:22 AM
I have another concatenation issue.
I'm trying to add :50 to an array of variables. Is something wrong with "$SD[] = $value. ':50';"?
$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
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.
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


Sign In
Create Account

Back to top









