/    Sign up×
Community /Pin to ProfileBookmark

How to echo table php+mysql

Hi guys,
i need echo first 2 rows of my table with connect to mysql database and get it to html table.
This code echo all items of my table but i need just 2 rows. Please rewrite my code . Or send to me condition which i can use. THX!

<?php
$host=’xxx’;
$user=’xxx’;
$password=’xxx’;
$database=’xxx’;
// Connects to your Database

mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db(“$database”) or die(mysql_error());
mysql_query(‘SET NAMES UTF8’);
mysql_query(‘SET COLLATION_CONNECTION=uft8_slovak_ci’);
$table=’culture’;
$query=’SELECT * FROM ‘.$table;/* */
$result=mysql_query($query) or die(mysql_error($db));

//tabulka
echo “<table class=’table’>

<tr>

<th>názov</th>
<th>Obrázok</th>
</tr>”;

/*while ($record=mysql_fetch_array($result)
echo “<tr>”;

echo “<td>”.record[‘name’].”</td>”;

echo “<td><img src=”.$record[‘img’].”></td>”;
echo “</tr>”;
};*
/

echo “</table>”;

/* Mysql_close($query);*/
?>

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@Sup3rkirbyApr 05.2014 — If you want to just return 2 rows of a table you need to change your MySQL query. It would look something like this:
[CODE]
'SELECT * FROM '.$table.' LIMIT 2';
[/CODE]
Copy linkTweet thisAlerts:
@Error404Apr 05.2014 — You should re-write your script using mysqli or PDO functions to connect to your MySQL instead of mysql functions.

Assuming there's a column in your table that always has unique values that do not repeat, just add that into your SQL statement in a WHERE clause. For example, if it was called Counter and contained only integer values 0, 1, 2, 3... then just change it to "SELECT * FROM MyTable WHERE Counter <= 1;" Change the table and column names accordingly for your table.
Copy linkTweet thisAlerts:
@majko123456authorApr 06.2014 — I tried change my code to connect with mysqli but it dont work, i dont know i have bad connection to database or bad syntax in query or i have bad while condition ... I am really sad. Please rewrite my code and correct my mistakes THX!

My table with all items look like this : http://www.treetechsolutions.sk/test/vzorove_skripty/getfromdb.php

(i need echo only first 2 rows) of this table)

<?php

//conection:

$host='xxx';

$user='xxx';

$password='xxx';

$database='xxx';

$db = mysqli_connect($host,$user,$password,$database) or die("Error " . mysqli_error($db));

//consultation:

$table='culture'; // for examle

$query='SELECT * FROM '.$table.'WHERE ID <= 2';

$result=mysqli_query($query) or die(mysqli_error($db));

//display information:

while($row = mysqli_fetch_array($result)) {

echo "<tr>";

echo "<td>".$row['nazov']."</td>";

echo "<td><img src=".$row['obr']."></td>";

echo "</tr>";

}

?>
Copy linkTweet thisAlerts:
@Sup3rkirbyApr 06.2014 — You probably just need a space before [B]WHERE[/B] in your query.
[CODE]
$query='SELECT * FROM '.$table.' WHERE ID <= 2';
[/CODE]
Copy linkTweet thisAlerts:
@techboy992Apr 07.2014 — $db_user = "db user";

$db_password= "password";

$db_name="db name";

$con=mysqli_connect("localhost","$db_user","$db_password","$db_name");

// Check connection

if (mysqli_connect_errno())

{

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}

$result = mysqli_query($con,"SELECT * FROM table" WHERE id='2'");

while($row = mysqli_fetch_array($result))

no you can get your things from database using $row[some field]
×

Success!

Help @majko123456 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.18,
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,
)...