Mysql COUNT function not working - php

I have a database called articles and I just wanna print out the amount of rows in my database. This is my code:
<?php
$con = mysql_connect('link', 'database', 'password');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT COUNT(*) FROM articles");
$row=mysql_fetch_array($result)
?>
<p>The amount of rows is <?php echo $row ?>.</p>
When I test this code, is just gives me 'The amount of rows is .' as output. What am I doing wrong?
Edit: This is my full code now, but it still doens't work:
$con = mysqli_connect('url', 'database', 'password');
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
$result = mysqli_query($con, "SELECT COUNT(URL) AS row_count FROM articles");
if (!$result ) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysqli_error($con);
exit;
}
$row=mysqli_fetch_array($result)
?>
<p>The amount of rows is <?= $row['row_count'] ?>.</p>
The error: DB Error, could not query the database MySQL Error: No database selected

You're trying to print the row.
Use echo $row[0] instead
Also, as it's printing nothing, I suspect your query is failing, as FALSE will be echoed as nothing in PHP.
Sidenote: mysql_* is deprecated, consider using PDO instead.
EDIT:
full code below
<?php
$con = new mysqli('url', 'database', 'password', 'db_name');
if (!$con) {
die('Could not connect: ' . mysqli_error());
}
$result = $con->query("SELECT COUNT(*) AS row_count FROM articles");
if (!$result) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . $con->error;
exit;
}
$row = $result->fetch_assoc();
?>
<p>The amount of rows is <?= $row['row_count'] ?>.</p>

You are echoing an array (which should give you error). Instead you can set alias for COUNT:
$result = mysqli_query($con, "SELECT COUNT(*) AS Total FROM articles");
And echo it like:
<p>The amount of rows is <?php echo $row['Total'] ?>.</p>

As others said, you're trying to print the array (hence the fetch_array), when you need to print something in the array.
Also, mysql_* is depreciated, I use mysqli_* which is very similar and easy to switch to.
Also, instead of *, try a fieldname in the count. I usually use the index, or primary key or something.
<?php
$link = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if (mysqli_connect_errno($link)) {
echo 'Could not select database:' . DB_NAME;
exit;
}
$result = mysqli_query($link, "SELECT COUNT(fieldname) AS row_count FROM articles");
if (!$result ) {
echo "DB Error, could not query the database\n";
echo 'MySQL Error: ' . mysqli_error($link);
exit;
}
$row=mysqli_fetch_array($result)
?>
<p>The amount of rows is <?= $row['row_count'] ?>.</p>

Related

Filtering a postgresql database using a search bar on my website

The Problem
Hello, I am creating a search function that will allow users to search for a specific E-Number and see whether it is derived from animals or not. Firstly, I want to dump all the results on the webpage to see if it is 100% correct. I have successflly connected to the database using PHP on my website but the pg_query is not displaying any results, is there more code I need?
The Code
<?php
$conn = pg_connect("***** port=*****
dbname=***** user=***** password=*****");
$res = pg_query($conn, "SELECT * FROM enumbers");
$connection = pg_connect ("host=***** port=*****
dbname=***** user=***** password=*****");
if($connection) {
echo 'connected';
} else {
echo 'there has been an error connecting';
}
?>
Thanks to #LaurenzAlbe for directing me to an answer, I thought I would show it here:
<?php
// Connecting, selecting database
$dbconn = pg_connect("host=***** port=*****
dbname=***** user=***** password=*****")
or die('Could not connect: ' . pg_last_error());
// Performing SQL query
$query = 'SELECT * FROM enumbers';
$result = pg_query($query) or die('Query failed: ' . pg_last_error());
// Printing results in HTML
echo "<table>\n";
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td>$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
?>
As you can see by the code above, I completely forgot about actually printng the results in HTML, this is the reason nothing was showing up.

How to use Insert query in PHP code

I do not understand what is going wrong with code. The result is get is "connected successfully success Query failed". I tried few combinations and I get the same result. Please help me in solving this. Thanks in advance.
<?php
$link = mysql_connect('localhost', 'root1', '')
or die('Could not connect: ' . mysql_error());
if ($link) {
echo 'connected successfully';
}
$l = mysql_select_db('vtflix', $link) or die ('Could not select the database');
if ($l) {
echo ' success';
}
/*$varCNAME = 'John';
$varCONTENT = '4';
$varVID = '1';*/
$sql = "INSERT INTO mpaa(C_Name, ContentRating, V_ID) VALUES ('Jon', 4, 3)";
mysql_query($sql, $link) or die("Query failed");
$que = "SELECT * FROM mpaa";
$query = mysql_query($que, $link);
if (!$query) {
echo 'query failed';
}
while ($sqlrow = mysql_fetch_array($query, MYSQL_ASSOC)) {
$row = $sqlrow['C_Name'];
$nrow = $sqlrow['Content Rating'];
$mrow = $sqlrow['V_ID'];
echo "<br>" . $row . " " . $nrow . " " . $mrow . "<br>";
}
mysql_close($link);
?>
1.Don't use mysql_* library (deprecated from php5 onward + removed from php7) .Use mysqli_* OR PDO.
2.An example of mysqli_*(with your code)is given below:-
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
Note:- To check php version (either on localhost or on live server) create a file with name phpInfo.php, and just write one line code in that file:-
<?php
phpinfo();
?>
Now run this file and you will get the current php version.
Like this:- https://eval.in/684551
Here it seems that you are using deprecated API of mysql_* .
1) Check your PHP version
<?php phpinfo();exit;//check version ?>
2) avoid the usage of mysql use mysqli or PDO
3) change your db connection string with this :
new Mysqlidb($hostname, $username, $pwd, $dbname);
example with you code
<?php
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>

php mysql query string

I need help to query string from database please help.
<?php
$phone="8165526693#vtext.com";
$link = mysql_connect('localhost', 'root', 'toor');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db('wizarddb')) {
die('Could not select database: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM Phone WHERE phone LIKE '%$phone%'");
if (!$result) {
die('Could not query:' . mysql_error());
}
echo mysql_result($result(1); // outputs phone
mysql_close($link);
?>
I have issues with echo or is the query wrong?
Your error is on the echo.
echo mysql_result($result(1); // outputs phone
That SHOULD be
echo mysql_result($result[1]); // outputs phone

Strange behaviour trying to detect errors selecting a database

I have the following code in php:
<?php
$con = mysql_connect("localhost","john","john");
mysql_select_db("data", $con);
if (!$mysql_select_db) {
die ('Cannot use data : ' . mysql_error());
}
$result = mysql_query("SELECT * FROM test where huss='hussein'");
while ($row = mysql_fetch_array($result)) {
echo "ADSDAD";
echo "<br />";
}
if (!$con) {
die('ERROR Could not connect: ' . mysql_error());
}
mysql_close($con);
?>
I was able to connect to the MySQL server, but I can't seem to select the relevant database.
Being printed is the hard-coded text Cannot use data :, but no MySQL error message follows it. If the MySQL connection had failed then I would have expected a MySQL error message to appear after the hard-coded text.
What am I doing wrong?
The problem's here:
mysql_select_db("data", $con);
if (!$mysql_select_db) {
die ('Cannot use data : ' . mysql_error());
}
You don't have a variable $mysql_select_db, and it doesn't magically map to the result of the last time you called mysql_select_db.
So, instead:
$result = mysql_select_db("data", $con);
if (!$result) {
die ('Cannot use data : ' . mysql_error());
}
Or:
if (!mysql_select_db("data", $con)) {
die ('Cannot use data : ' . mysql_error());
}

Cannot select from database?

I'm trying to select from my MySQL database, from the table Lines. I made it really simple, it should simply show all rows in the database. However I'm getting an error on line 16 (I put an * next to it) that says: "mysql_fetch_array(): supplied argument is not a valid MySQL result resource"
$con = mysql_connect("...","...","mypass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$result = mysql_query("SELECT * FROM Lines");
while($row = mysql_fetch_array( $result )) ***************
{
echo $row['Text'];
echo "<br />";
}
mysql_close($con);
What am I doing wrong? Thanks
Lines is a reserved word in MySQL. Use backticks.
SELECT * FROM `Lines`
Add mysql_error() after mysql_query() to see if there's any error, I'm guessing table Lines doesn't exist:
$con = mysql_connect("copoetry.db.6945202.hostedresource.com","dbname","mypass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("dbname", $con);
$result = mysql_query("SELECT * FROM Lines");
echo mysql_error(); die;
while($row = mysql_fetch_array( $result )) ***************
{
echo $row['Text'];
echo "<br />";
}
mysql_close($con);

Categories