Jump to content

How do I extract the data from my database query?


Canadian

Recommended Posts

I have used the following select statement to get data from my database.

 


$npn_count_query = "SELECT * FROM npn ORDER BY count DESC LIMIT 1";

$npn_count = $db->query($npn_count_query);

 

I'm getting this when I echo $npn_count.  Which is the pointer not the data.

 


Object id #2

 

How do I extract the DATA from the pointer?  Do I use something like...

 


$data = $npn_count->fetch();

 

Any help would be greatly appreciated.

 

Thanks

Link to comment
Share on other sites

How do I extract the DATA from the pointer?  Do I use something like...

 


$data = $npn_count->fetch();

I guess so.

 

I cant tell you the exact method you need to use in order to extract your results from your query. You seem be using a database class object. What is the database class you're using? Is it a custom class or the built in mysqli class?

Link to comment
Share on other sites

Oh trust me, at my level it's nothing custom.  I'm 99% sure it's the built in mysqli class.  I got it from the book "PHP and MySQL Web Development".

 

I don't necessarily have to use OOP PHP.  Is there an easier/better way to do this?  Should I try using procedural PHP?  If so any suggestions?

 

Thanks again.

Link to comment
Share on other sites

If you're reading the book it should be explaining to you how to use the mysqli class properly, maybe you skipped too far in the book or missed a chapter. If you're stuck at any time on how to use a PHP function then have read of the PHP manual on that particular function. So for all documention on mysqli is here.

 

You can use the built in mysqli class or use mysqli procedurally.

Link to comment
Share on other sites

Try using fetch_object:

 

$npn_count_query = "SELECT * FROM npn ORDER BY count DESC LIMIT 1";
$npn_count_result = $db->query($npn_count_query);
$npn_count_dataObj = $npn_count_result->fetch_object();

echo $npn_count_dataObj->field1;
echo $npn_count_dataObj->field2;
echo $npn_count_dataObj->field3;

 

Replace field1, field2, field3 with the field names from your database that you need to access.

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.