Cannot get php to show number of rows

#1
<?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.

#2

you can’t mix mysqli and mysql.

1 Like
#3

I think it’s possible, but it’s not good practice.

#4

[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 have done it for you this time.
[/off-topic]

#5

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?

#6

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).

1 Like
#7
<?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;
?>
#8

Thanks, have it to work by using a loop to go though all the rows and counting the amount of loops, hence the number of rows. Code below:-

<?php
$servername = "localhost";
$username="abilityi_test1";
$password="~ws4K+4=kc]V";
$dbname = "abilityi_Rnumber";
$conn = new mysqli($servername, $username, $password, $dbname);
$counter=0;
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully" . "<br>";
// String to store mysql query
$sql= "SELECT * FROM Rtest";
// Store array from $sql varaiable in $result
$result= $conn->query($sql);
echo "<table border=1><th colspan=4>Rnumber database</th><tr><td>Rnumber</td><td>file opened</td><td>Sheet number</td><td>Description</td></tr>";
while ($row = $result->fetch_assoc())
{
	echo "<tr>";
    echo "<td>" . $row["COL 1"] . "</td>";
	echo "<td>" . $row["COL 2"] . "</td>";
	echo "<td>" . $row["COL 3"] . "</td>";
	echo "<td>" . $row["COL 4"] . "</td>";
	echo "</tr>";
$counter++;

}
echo $counter;
$conn->close();
?>
#9

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.

#10

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.

closed #11

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.