Jump to content

Help with SQL / PHP


zer0uk

Recommended Posts

Hi please can someone set me right, the below code isn't working I get an error "Undefined offset: 0"

example below is the problem code with error (2nd example works fine)

 // check how many rides user has done
       $resultset = $conn->query("SELECT COUNT(*)FROM tbl_rides WHERE username_id = 12");
       while($row = $resultset->fetch_assoc())
       $var_l_rides=$row[0];
  $resultset = $conn->query("SELECT `gold`FROM tbl_accounts WHERE id = 12");
       while($row = $resultset->fetch_assoc())
       $var_prem=$row['gold'];

 

Link to comment
Share on other sites

21 minutes ago, zer0uk said:

fetch_assoc

the associative index for the COUNT(*) expression is actually something like $row['COUNT(*)']. to simplify this, add an alias name for the COUNT(*) expression in the query and use that alias name for the associative index.

next, both of the query examples you have shown will/probably match at most one row. why you are using a loop to fetch the data. just fetch the single row without the loop.

  • Like 1
Link to comment
Share on other sites

26 minutes ago, gw1500se said:

Since you do no error checking after the query, that is what happens if no rows are returned due to either and error or nothing in the database meets the WHERE clause.

I ran the SQL in the console and it returns 14 

Link to comment
Share on other sites

15 minutes ago, mac_gyver said:

the associative index for the COUNT(*) expression is actually something like $row['COUNT(*)']. to simplify this, add an alias name for the COUNT(*) expression in the query and use that alias name for the associative index.

next, both of the query examples you have shown will/probably match at most one row. why you are using a loop to fetch the data. just fetch the single row without the loop.

Thanks for your reply  makes sense but how to I create an alias then?

Could only get this working using the code I've used do realise that their is no need for a loop, feel free to show me what I should be doing .. only been doing this for a month so learning ...

 

Thanks for your help.. appreciated

Link to comment
Share on other sites

I'm confused by what you are trying to do. COUNT() returns the number of rows that match the query criteria. Is that what you really want? Why put it in a loop? The reason the 2nd one works is because it actually returns a row of data.

Edited by gw1500se
Link to comment
Share on other sites

Basically its part of my membership system so the system is checking how many record the user has if its more than so many they will be denied access to add another record.

using ['COUNT(*)'] fixed the issue but if anyone wants to show me the correct way of doing this and without the loop id appreciate the learning.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.