Varo Studio
Varo Studio

Reputation: 3

PHP - Not displaying anything from database -

Hello i have a problem displaying data from my database using LIMIT 2, it display nothing! no errors! What i have now:

<?php
    $con = mysql_connect("localhost","root","pass");
    if (!$con)
    {
       die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("database", $con);
    $query = "SELECT * FROM Stat LIMIT 1";
    $result = mysql_query($query);   ?>
    <html><head><title>.. </title></head><body>

    <?php 
        while($products = mysql_fetch_array($result)) 
        { ?> 
             <small><?php echo  $products['Followers'] ; ?> Followers </small>
        <?php } ?>
    </a>

So that display row 1 but if i change to this

<?php
    $con = mysql_connect("localhost","root","pass");
    if (!$con)
    {
       die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("database", $con);
    $query = "SELECT * FROM Stat LIMIT 2";
    $result = mysql_query($query);   ?>
    <html><head><title>.. </title></head><body>

    <?php 
        while($products = mysql_fetch_array($result)) 
        ?> 

It display nothing! Help! Can someone explain to me so i can learn.

main page (row 2) :

<div class="class">
<div class="class">
     <?php include '/includes/stat/connection2.php';?>
        <a href="<?php echo  $products['Site'] ; ?>" target="_blank"><img alt="<?php echo  $products['Title'] ; ?>" class="img-responsive" src="<?php echo  $products['Banner'] ; ?>
"></a>
    </div>
    <div class="class" data-toggle="tooltip" data-placement="top" title="Total Votes">
        <?php include '/includes/stat/connection2.php';?>
<?php echo  $products['Votes'] ; ?>
    </div>
    <div class="class" data-toggle="tooltip" data-placement="top" title="Followers">
        <?php include '/includes/stat/connection2.php';?>
<?php echo  $products['Followers'] ; ?>
    </div>

    <div class="reset"></div><hr>
    <h5>
        <a href="<?php echo  $products['Site'] ; ?>">
            <img alt="Profile" src="/images/home.gif">
        </a>
        <a href="<?php echo  $products['Site'] ; ?> " target="_blank">
        <?php include '/includes/stat/connection2.php';?>

<?php echo  $products['Title'] ; ?>
        </a>
    </h5>
<?php include '/includes/stat/connection2.php';?>
<?php echo  $products['Description'] ; ?>
</div>

Here is the main page. Connection2 is the query that has LIMIT 2

Upvotes: 0

Views: 218

Answers (6)

Amrinder Singh
Amrinder Singh

Reputation: 5482

Try this:

$query="select * from Stat limit 0,1";

Upvotes: 0

arshad
arshad

Reputation: 883

(Recommended for your case)You can optimize your query to select one row from the table -

  $query = "SELECT * FROM Stat LIMIT 1";

or you can use mysql_fetch_row() to get only one row from the whole result array

  while($products = mysql_fetch_row($result)) {?>`

Upvotes: -1

arif_suhail_123
arif_suhail_123

Reputation: 2509

Add LIMIT to your query than it will return only one row. In limit you can limit the no. of returning rows in the query, and you can give range as well like this LIMIT 0,10 limit min,max

so change your query like this

$query = "SELECT Followers FROM Stat LIMIT 1";

also mysql are depriciated so use mysqli or PDO

For mysqli use check this link http://php.net/manual/en/book.mysqli.php

for PDO check this link http://php.net/manual/en/book.pdo.php

Upvotes: 1

akshaynagpal
akshaynagpal

Reputation: 3137

$query = "SELECT * FROM Stat LIMIT 1";

LIMIT gives a limited amount of results to return.

Upvotes: 1

Max Pain
Max Pain

Reputation: 1237

Use LIMIT 1 to limit your query to the first row. The code below will return only the first raw in your table:

$query = "SELECT * FROM Stat LIMIT 1";

Upvotes: 0

bigonez
bigonez

Reputation: 111

You can use the LIMIT clause in the SELECT query.

SELECT * FROM Stat LIMIT 1;

Upvotes: 1

Related Questions