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++;
}
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.
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.
Related
<?php
$id = '2422414574';
$json = file_get_contents("http://xxxxxx.com/api.php?token=xxxx&id=xxxx");
$data = json_decode($json);
echo $data->phim[0]->filmName . "<br/>";
echo $data->phim[0]->epsList[0]->name . " - ";
echo $data->phim[0]->epsList[0]->id . "<br/>";
echo $data->phim[0]->epsList[1]->name . " - ";
echo $data->phim[0]->epsList[1]->id . "<br/>";
echo $data->phim[0]->epsList[2]->name . " - ";
echo $data->phim[0]->epsList[2]->id . "<br/>";
echo $data->phim[0]->epsList[3]->name . " - ";
echo $data->phim[0]->epsList[3]->id . "<br/>";
echo $data->phim[0]->epsList[4]->name . " - ";
echo $data->phim[0]->epsList[4]->id . "<br/>";
echo $data->phim[0]->epsList[5]->name . " - ";
echo $data->phim[0]->epsList[5]->id . "<br/>";
echo $data->phim[0]->epsList[6]->name . " - ";
echo $data->phim[0]->epsList[6]->id . "<br/>";
echo $data->phim[0]->epsList[7]->name . " - ";
echo $data->phim[0]->epsList[7]->id . "<br/>";
echo $data->phim[0]->epsList[xxxx]->name . " - ";
echo $data->phim[0]->epsList[xxxx]->id . "<br/>";
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
As you can see, I have to repeat 1 code in many time. Is there any short way to do this without repeat code?
For example, I want to get all values from id and name from this URL, which have 24 items. That means I have to repeat code in 24 times :(
what about foreach()?
foreach($data->phim as $phim)
{
echo $phim->filmName . "<br/>";
foreach($phim->epsList as $epsList)
{
echo $epsList->name . " - ";
echo $epsList->id . "<br/>";
}
}
You need to iterate over it using a foreach loop. This is one of the basic functions of PHP and there are many tutorials for it.
the below code contains if statements depending on usertype and status. It won't display anything. In the code above i had usertype==1 and status==1 and it is working as it should but this below code won't work. Any suggestions? Please ignore the code i have as i am a beginner and the code is messy.
} else if ($usertype == 2) {
$server1 = "localhost";
$user1 = "r";
$pass1 = "";
$db1 = "";
$db4 = "";
$user5 = $_SESSION['username'];
$mysqli = new Mysqli($server1, $user1, $pass1, $db1) or mysqli_error($mysqli);
$mysqli6 = new Mysqli($server1, $user1, $pass1, $db4) or mysqli_error($mysqli);
$name2= $mysqli6->query("SELECT name FROM head WHERE username= '$user5'")->fetch_object()->name2;
$status2 = $mysqli->query("SELECT status FROM Overrides WHERE head = '$name2'")->fetch_object()->status2;
$headforms = $mysqli->query("SELECT * FROM Overrides WHERE head = '$name2' ");
$num_rows2 = mysqli_num_rows($headforms);
if ($status2 == 2) {
echo "Overrides today: " . $num_rows2;
while($row2 = mysqli_fetch_array($headforms)) {
echo "<br /><br />First Name: " . $row2['name'] . "<br />";
echo "<br />Middle Name: " . $row2['mname'] . "<br />";
echo "<br />Family Name: " . $row2['fname'] . "<br />";
echo "<br />Student ID: " . $row2['sid'] . "<br />";
echo "<br />Scolarship: " . $row2['sc'] . "<br />";
echo "<br />Phone No: " . $row2['phone'] . "<br />";
echo "<br />Email: " . $row2['email'] . "<br />";
echo "<br />Class: " . $row2['class'] . "<br />";
echo "<br />Section: " . $row2['section'] . "<br />";
echo "<br />Semester: " . $row2['semester'] . "<br />";
}
}
?>
Concerning the database entries and data, i have checked them and i'm 100% sure it should display correct info.
I did a test : i tried doing print_r($status2); and print_r($name2); but page is empty, could the problem be with the userype == 2 since it's displaying nothing.
errors:
Notice: Undefined property: stdClass::$name2 in /home/aukwizcq/public_html/override.php on line 310
Notice: Trying to get property of non-object in /home/aukwizcq/public_html/override.php on line 311
So, I'll post the code below. Beneath the code is where I will pose my question.
if (!empty($_SESSION['username']) && !empty($_SESSION['password']))
{
$server=mysql_real_escape_string($_POST['server']);
$teamname=mysql_real_escape_string($_POST['teamname']);
$creator=$_SESSION['username'];
$verify = mysql_real_escape_string($_POST['verify']);
$date = date("F j, Y, g:i a");
if (!empty($teamname)) {
// if ($verify == "wookie" ||
// $verify == "Wookie" ||
// $verify == "WOOKIE")
// {
$sql="INSERT INTO rated_teams (server, name, creator, created, players, win, loss)
VALUES ('$server', '$teamname', '$creator','$date', '', '', '')";
if (mysql_query($sql,$con))
{
echo "<p>Added ". $teamname . " on " . $server . " by " . $creator . " on " . $date ." <br /><a href='myprofile.php'>Return to Profile</a></p>";
}
else
{
echo $sql . "<br />";
echo "<br /><h1>Error</h1>";
echo "<p><a href='myprofile.php'>Sorry, your team registration has failed. Please go back and try again.</a></p>
<br />" . $teamname . " on " . $server . " by " . $creator . " on " . $date;
}
//} else { echo "That isn't how you spell Wookie!"; }
} else { echo "Team Name is empty, <a href='myprofile.php'>go back and give yourself a Team Name</a>"; }
} else { echo "You must be <a href='login.php'>logged in</a>!"; }
This issue is that the line "if (mysql_query($sql,$con))" goes directly to the ELSE. I'm assuming the problem lies with my $sql but I can't pinpoint where it is. Another pair of eyes would really help. Thanks a bunch!
To trace errors with mysql_query() , you should use mysql_error(). Here's an example, inspired of one of the PHP mysql_error() doc
$sql="INSERT INTO rated_teams (server, name, creator, created, players, win, loss)
VALUES ('$server', '$teamname', '$creator','$date', '', '', '')";
mysql_query($sql,$con);
if (mysql_errno()) {
$error = "MySQL error ".mysql_errno().": ".mysql_error()."\n<br>When executing:<br>\n$sql\n<br>";
// Your stuff
echo $sql . "<br />";
echo "<br /><h1>Error</h1>";
echo "<p><a href='myprofile.php'>Sorry, your team registration has failed. Please go back and try again.</a></p>
<br />" . $teamname . " on " . $server . " by " . $creator . " on " . $date;
}
else {
echo "<p>Added ". $teamname . " on " . $server . " by " . $creator . " on " . $date ." <br /><a href='myprofile.php'>Return to Profile</a></p>";
}
Also, you should use PDO or mysqli, since mysql_* are deprecated since PHP 5.x
i have the following code in PHP
$link ="http://ws.audioscrobbler.com/2.0/?method=&user=xgayax" .
"&api_key=b25b959554ed76058ac220b7b2e0a026";
$xml = #simplexml_load_file($link);
$tracks = $xml->recenttracks->track;
for ($i = 0; $i < 3; $i++) {
$playingnow = $tracks[$i]->attributes()->nowplaying;
$name = $tracks[$i]->name;
$artist = $tracks[$i]->artist;
$url = $tracks[$i]->url;
$date = $tracks[$i]->date;
$img = $tracks[$i]->children();
$img = $img->image[0];
echo "<a href='" . $url . "' target='TOP'>";
if ($nowplaying == "true") {
echo "Now playing: ";
}
echo "<img src='" . $img . "' alt='album' />
$artist . " - " . $trackname . " # " . $date . "
</a>
";
}
and i got the following error
Parse error: syntax error, unexpected '#', expecting ',' or ';' on line 31
any solution for this problem???
Your last lines have a error, the correct code is:
echo "<img src='" . $img . "' alt='album' />" .
// ^^^ missing
$artist . " - " . $trackname . " # " . $date . "
</a>
";
There has to be a easier way...
I keep getting this for the second line.
Parse error: syntax error, unexpected ';'
while($row = mysql_fetch_array($result)){
echo ("'$MAP_OBJECT->addMarkerByCoords";
"(\"";
$row['longitude'];
",";
$row['latitude'];
",\"";
$row['routername'];
"-";
$row['desc'];
"\", \"";
$row['routername'];
"-";
$row['desc'];
"<br><a href=\"./div/";
$row['routername'];
"\">Site Info</a>'");
echo "<br />";
}
You have to combine with a . and not with ;
echo ("'$MAP_OBJECT->addMarkerByCoords" .
"(\"" .
$row['longitude'] .
....
Have a look into the manual: http://php.net/manual/en/language.operators.string.php
Most of the ; should be . if you are attempting to concatenate these strings:
while($row = mysql_fetch_array($result)){
echo ("'$MAP_OBJECT->addMarkerByCoords" .
"(\"" .
$row['longitude'] .
"," .
$row['latitude'] .
",\"" .
$row['routername'] .
"-" .
$row['desc'] .
"\", \"" .
$row['routername'] .
"-" .
$row['desc'] .
"<br><a href=\"./Ldiv/" .
$row['routername'].
"\">Site Info</a>'"); // Here's the actual end of the statement
echo "<br />";
}
This woudl be a lot tidier with a HEREDOC:
echo <<<ROW
$MAP_OBJECT->addMarkerByCoords(
{$row['longitude']},
{$row['latitude']},
"{$row['routername']}-{$row['desc']}",
"$row['routername']}-{$row['desc']}"<br>
Site Info
)
ROW;
Although , it looks like something is missing before the <br> since the previous quote doesn't get closed.
Your syntax is wrong for what you are trying to execute.
The ; in PHP is an end statement, essentially. You are telling PHP to stop executing the echo on the very first line echo ("'$MAP_OBJECT->addMarkerByCoords" ; which is NOT what you want.
Instead, replace all the ; with .'s except for the last echo statement
while($row = mysql_fetch_array($result)){
echo "'$MAP_OBJECT->addMarkerByCoords" .
"(\"" .
$row['longitude'] .
"," .
$row['latitude'] .
",\"" .
$row['routername'] .
"-" .
$row['desc'] .
"\", \"" .
$row['routername'] .
"-" .
$row['desc'] .
"<br><a href=\"./Ldiv/" .
$row['routername']; .
"\">Site Info</a>'";
echo "<br />";
}