<?php
$servername = "localhost";
$username="abilityi_test1";
$password="";
$dbname = "abilityi_Rnumber";
$link = mysqli_connect($servername, $username, $password, $dbname);
// check connection workied if not why
echo "loading";
if (!$link)
{
$output = 'Unable to connect to the database server.';
include 'Output.html.php';
exit();
}
if (!mysqli_set_charset($link, 'utf8'))
{
$output = 'Unable to set database connection encoding';
include 'Output.html.php';
exit();
}
if (!mysqli_select_db($link, 'abilityi_Rnumber'))
{
$output = 'Unable to locate the database';
include 'Output.html.php';
exit();
}
else
{
$output = 'Connected to database';
include 'Output.html.php';//works with ' best
}
$result = mysql_query("SELECT COUNT(*) FROM Rnumdescrip");
$num_rows = mysql_num_rows($result);
echo $num_rows;
?>
removed password
Have tried all sorts of php to get it to work all I want is to display the number of rows in a table, connects to the database okay. Please help me get this sorted.
[off-topic] @onlyme when you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.
I think this will always display “1”, won’t it, once the mysql / mysqli thing is sorted? You either have to select the rows, or select the count of the rows and display the count returned.
ETA:
What happens with your code? What does it do that it should not, or what does it not do that it should?
Nope, mysql runs on a resource, while mysqli runs on an object.
It might look as if it worked if you have appropriate DB settings in your php.ini (since mysql automatically opens a connection (or attempts to), if none exists).
<?php
$servername = "localhost";
$username="abilityi_test1";
$password="";
$dbname = "abilityi_Rnumber";
$link = mysqli_connect($servername, $username, $password, $dbname);
// check connection workied if not why
echo "loading";
if (!$link)
{
$output = 'Unable to connect to the database server.';
include 'Output.html.php';
exit();
}
if (!mysqli_set_charset($link, 'utf8'))
{
$output = 'Unable to set database connection encoding';
include 'Output.html.php';
exit();
}
if (!mysqli_select_db($link, 'abilityi_Rnumber'))
{
$output = 'Unable to locate the database';
include 'Output.html.php';
exit();
}
else
{
$output = 'Connected to database';
include 'Output.html.php';//works with ' best
}
$result = mysql_query("SELECT COUNT(*) FROM Rnumdescrip");
$num_rows = mysql_num_rows($result);
echo $num_rows;
?>
Well, if you need to display the stuff anyway, then that’s fine but you were closer the first time, if all you need is the row count and don’t want to display the results.
Hi thanks only just spotted after leaving it for a while will change the mysql to mysqli. I have got it working a different way a php script that works and loops through all the rows to print them on the screen I have add a loop counter to get the number of rows and this works. I would like this to work as well though.