SQL SELECT not working within Array MYSQL_ASSOC
Question
I can't seems to find the error in below script. i have been checking for while now. sorry i'm really new to SQL and PHP.
$tCompany_SQLselect = "SELECT ";
$tCompany_SQLselect .= "ID, preNAME, Name, RegType ";
$tCompany_SQLselect .= "FROM ";
$tCompany_SQLselect .= "tCompany ";
$tCompany_SQLselect_Query = mysql_query($tCompany_SQLselect);
$index = 1;
while ($row = mysql_fetch_array($tCompany_SQLselect_Query, MYSQL_ASSOC)) {
$preNAME = $row['preNAME'];
$Name = $row['Name'];
$RegType = $row['RegType'];
echo $index.".".$preNAME."".$Name."".$RegType" <br />;
$index++;
}
Solution
The problem is in the way you concatenated the variables.
You forgot a dot and a quote. Change .$RegType" <br />;
to . $RegType . "<br />";
echo $index . "." . $preNAME . " " . $Name . " " . $RegType "<br /> ;
^ dot ^ quote
Change it to:
echo $index . "." . $preNAME . " " . $Name . " " . $RegType . "<br />";
You can remove the space in the " "
if you want.
OTHER TIPS
first: you can use:
$tCompany_SQLselect_Query = mysql_query($tCompany_SQLselect) or die(mysql_error());
this will allow you to debug the error.
second: you can use:
$row = mysql_fetch_assoc($tCompany_SQLselect_Query)
to shorten the syntax.
The echo
line should be:
echo $index.".".$preNAME." ".$Name." ".$RegType." . "<br />";
You were missing quotes around <br />
, and the .
for concatenation before it.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow