Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Fetch data from db with var and then echo?

Featured Replies

Arg, I feel like such a pathetic, helpless nerdfolk asking all these questions. :mellow:

 

Anyhow, onto the quandary. Here's my code. It gives me a blank page.

I want to get the information from my query and be able to echo it: it's a number, not a bunch of complex stuff. I think I need to pull it out of the once-cell table it's trapped in before I can echo it or use it as a number in a variable. How can I do that?

 


<?php

include("secretpwordsclickmeooohoh.php");

$cxn = mysqli_connect($host,$user,$passwd,$dbname) or die ("Couldn't connect to server.");

# This will get the number of albums.
$query = "SELECT max(albumID) FROM album";
$num_albums = mysqli_query($cxn,$query) or die ("Get number of albums query failed.");

echo $num_albums;

?>

i dont think you could just echo a Db result.

 

you need to first fetch the row in an array format.

 

---

Example

---

 

<?php// Make a MySQL Connection$query = "SELECT * FROM example";

$result = mysql_query($query) or die(mysql_error());

$row = mysql_fetch_array($result) or die(mysql_error());

echo $row['name']. " - ". $row['age'];?>

Sorry if i misunderstood you or this is a stupid answer to ur query in any way..lol

hope this helps

You would do it something like this:

 

<?php
include("secretpwordsclickmeooohoh.php"); 

# This will get the number of albums. 
$query = "SELECT max(albumID) as max_id FROM album"; 
$rsAlbums = mysqli_query($cxn,$query) or die ("Get number of albums query failed."); 

while ($arrRow = mysqli_fetch_array($rsAlbums)) 
{
 echo $arrRow['max_id'];
}
?>

 

mysqli_query will execute a query and return a result set.

 

You then need to execute mysqli_fetch_array (or some other mysqli_fetch) to return one row from the result set.

 

In the example I have looped through the result set in a while loop. Each iteration of the loop fetches a the next row. In your example there will only be one row returned - so technically you could remove the while loop and do this:

 

<?php
include("secretpwordsclickmeooohoh.php"); 

# This will get the number of albums. 
$query = "SELECT max(albumID) as max_id FROM album"; 
$rsAlbums = mysqli_query($cxn,$query) or die ("Get number of albums query failed."); 

$arrRow = mysqli_fetch_array($rsAlbums);

echo $arrRow['max_id'];

?>

 

 

Hope this helps.

  • Author

That works, Zoonder, thanks.

 

I don't completely understand it though.

 

When you said "SELECT..as max_id" did that give a name (key) to the value you're select? And that's how you were able to refer to it later?

In a nutshell, yes.

 

Apologies for any confusion - I changed the "SELECT" statement to make the example more clear.

 

I could have used your original "SELECT max(albumID)..." and done this:

echo $arrRow['max(albumID)'];

 

Instead I modified the SQL so that max(albumID) was assigned to a column alias "max_id" .

 

The "mysqli_fetch_array" function returns two arrays by default one is an indexed array and the other is an associative array. You can specify what is returned by using the result type parameter of mysqli_fetch_array like this:

$arrRow = mysqli_fetch_array($rsAlbums,MYSQLI_ASSOC); // for an ASSOCIATIVE ARRAY ONLY
$arrRow = mysqli_fetch_array($rsAlbums,MYSQLI_NUM); // for an INDEXED ARRAY ONLY
$arrRow = mysqli_fetch_array($rsAlbums,MYSQLI_BOTH); // for both an INDEXED array and an ASSOCIATIVE ARRAY

 

This can all be found in the (PHP Manual Page for mysqli_fetch_array).

 

Try using one of the three examples above in your code - To see the arrays in a human readable format use

print_r($arrRow);

 

The associative array uses the field name(s) from the "SELECT" statement as the key - hence I was able to refer to the key "max_id".

That works, Zoonder, thanks.

 

I don't completely understand it though.

 

When you said "SELECT..as max_id" did that give a name (key) to the value you're select? And that's how you were able to refer to it later?

 

Its getting the number of albums, u can also use count

 

$query = "SELECT count(albumID) as max_id FROM album"; 
$rsAlbums = mysqli_query($cxn,$query) or die ("Get number of albums query failed."); 

while ($arrRow = mysqli_fetch_array($rsAlbums)) 
{
 echo $arrRow['max_id'];
}

Its getting the number of albums, u can also use count
.

 

Yes this is true.

 

$query = "SELECT count(albumID) as count_of_albumID FROM album";

 

The above will count the number of non-NULL values in the column "albumID".

 

Where as the SQL max function will return the highest value in the column.

$query = "SELECT max(albumID) as max_id FROM album";

MAX is used to find the maximum value (e.g find the most expensive album, the album with the most tracks, etc) COUNT is used to count things, so if you are trying to find the number of albums in the table then you'd use COUNT.

MAX is used to find the maximum value (e.g find the most expensive album, the album with the most tracks, etc) COUNT is used to count things, so if you are trying to find the number of albums in the table then you'd use COUNT.

 

Mmm yea i had an idea that u would just use count, but was not sure so i thought u could use max as well thanks for clearing it up :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.