/    Sign up×
Community /Pin to ProfileBookmark

SQL Error. I can’t see what’s wrong

I’ve been staring at this for hours, it works in mysql.
Any ideas?

Fatal error: Uncaught Exception: Database Error [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘?’ at line 1 in /home2

“`
$Db = new Dbh;
$con = $Db->connect();

$teamList = array();

$SQL = “SELECT t.teamId, t.teamName, t.knownAs, t.teamEmblem, t.teamLogo FROM `teams` t JOIN `competitionteams` c ON t.teamId = c.teamId WHERE c.competitionId = ?”;
$stmt = $con->prepare($SQL);
$stmt->bind_param(‘i’, $competitionId);
$result = $stmt->get_result();

$result = $con->query($SQL);
if (!$result) {
throw new Exception(“Database Error [{$con->errno}] {$con->error}”);
}

while($row = $result->fetch_assoc()){
$Team = new Team(intval($row[‘teamId’]), $row[‘teamName’], $row[‘knownAs’], $row[‘teamEmblem’], $row[‘teamLogo’]);
$teamList[] = $Team;
}

return $teamList;
“`

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@kiwisauthorNov 25.2020 — when i force in a competitionid say 5477 it works so something to do with the bind.

it's a function and the value is being passed into the function.
Copy linkTweet thisAlerts:
@NachfolgerNov 25.2020 — ``SQL<i>
</i>t.teamLogo FROM
teams t JOIN competitionteams<i>
</i>
``

Could be wrong, but you need to specify the join type.
Copy linkTweet thisAlerts:
@GarpyNov 25.2020 — @Nachfolger#1625384

You're right
Copy linkTweet thisAlerts:
@NogDogNov 25.2020 — > @Nachfolger#1625384 Could be wrong, but you need to specify the join type.

Not really, assuming the OP wants it to be an inner join. (It assumes inner join if you just use join.)
Copy linkTweet thisAlerts:
@NogDogNov 25.2020 — > @kiwis80#1625373 $result = $con-&gt;query($SQL);

My guess is that it's failing here, and that you probably don't want this line in there at all? It's just executing the SQL as written, with the literal "?" in it; but you should already have executed the query in the preceding command, I think? (I'm a PDO guy, so it's possible I'm confused by MySQLi ;) )
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...