/    Sign up×
Community /Pin to ProfileBookmark

Trying to select from double select. doesnt work?

trying to select from a double select but it doesnt work. I get Warning: mysqli_query(): Couldn’t fetch mysqli. this is the code:

[code=php]
<?php
$result = mysqli_query($con, “SELECT u.* FROM users AS u JOIN (SELECT visitorid FROM visitors AS v WHERE v.userid = $userid ORDER BY last_visit DESC LIMIT 6) AS v1 ON u.user_id = v1.visitorid”);
?>
<table style=”width:100%”>
<?php

while($row = mysqli_fetch_array($result))
{
$cuser = $_SESSION[‘user’];
$favorite = $row[‘username’];
echo “username: $favorite”; //works

$res2 = mysqli_query($con, “SELECT * FROM favorites WHERE favorite=’$favorite’ AND cuser=’$cuser'”); //Warning: mysqli_query(): Couldn’t fetch mysqli
$row2 = mysqli_fetch_array($res2);

$favorite = $row2[‘favorite’];
echo “Favorite= $favorite”;

?>
…..
…..
…..

</table>[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmDec 13.2015 — Kind of a strange query. Why are joining in the visitors table in your first query when all you do is select the visitorid which is by your definition the same as the userid you already have from your first select? Makes no sense to include a table if all you are including is the matching key.

What you s/b doing is avoiding that query in your while loop (very bad form!) by joining it in the first query. You have everything you need, you're just not doing it.

I don't see anything wrong with your second query. How about displaying the error that s/b provided by that query if it is failing?
Copy linkTweet thisAlerts:
@jag1authorDec 13.2015 — Thanks for answer

I will use some more variables later from the first query ?

I dont either see why the second query doesnt work. It makes me crazy because it should really work. All i get is Warning: mysqli_query(): Couldn't fetch mysqli on the query line which is wierd because the WHERE variables isnt empty or anything
Copy linkTweet thisAlerts:
@ginerjmDec 13.2015 — As I said - add a check for the mysqli error code and display the message it gives you.

Never used mysqli - could it be because you have an open query result already going?

You really should fix your first query and add the troubled one to it and perhaps solve all your problems.
Copy linkTweet thisAlerts:
@NogDogDec 14.2015 — Is it safe to assume $con has been successfully defined/set before you try to use it here? (Just asking: sometimes it's the simple, silly little things.)

How I might write up the queries in order to capture some debug info:
[code=php]
<?php
ini_set('display_errors', true); // set to false in production
error_reporting(E_ALL);
$sql = <<<EOD
SELECT u.*
FROM users AS u
JOIN (
SELECT visitorid
FROM visitors AS v
WHERE v.userid = $userid
ORDER BY last_visit DESC LIMIT 6
) AS v1 ON u.user_id = v1.visitorid
EOD;
$result = mysqli_query($con, $sql);
if($result == false) {
throw new Exception(mysqli_error($con).PHP_EOL.$sql);
// at some point you want to wrap all this in a try/catch block
// so that you can more gracefully handle any DB exceptions
}
[/code]

(breaking up the SQL into multiple lines often helps you locate a problem, as the DB error message will often mention the offending line number)
×

Success!

Help @jag1 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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