How to display number of rows in a sql table using php? - php

I'm trying to get lines' number from the result of the sql query, so I started by connecting to the database, then checking if the connection was established and finally I tried to count the number of lines, here's the code :
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$res = $conn->query('SELECT COUNT (id_offre) AS nb FROM offres');
$data = $res->fetch();
$nb = $data['nb'];
echo $nb;
?>
I get these errors :
Uncaught Error: Call to a member function fetch() on boolean
Call to a member function fetch() on boolean
I used echo to check the 'nb' value, what is the problem?

Your query fails which makes $conn->query() to return false.
So, why does the query fail?
SQL doesn't allow for any spaces between function names and the parentheses, like you have at COUNT ().
So if you change:
SELECT COUNT (id_offre) AS nb FROM offres
to
SELECT COUNT(id_offre) AS nb FROM offres
...it should work.
Note: I would highly recommend you to have a read about mysqli::error() and add some error handling to your code.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_test";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (! $conn) {
die("Connection failed: " . mysqli_connect_error());
}
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ".$conn - > connect_error);
}
$res = $conn->query('SELECT * FROM offres');
$data_count = mysqli_num_rows($res);
echo($data_count);
?>
check this out!

Related

Selecting data from sql database doesn't work. how do I fix it?

I am trying to select data from a database. I do have a successful connection, but it seems like the query doesn't work even though I know for sure that the query is right. What am I doing wrong?
If I execute the code below, the result I get is: "Connected successfullyBad query". The 'Bad query' should mean that the query is wrong, but I checked it and it isn't wrong...
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>
I expect to only see "connected successfully"
You are missing your database name. You can do it two ways, or in the connect statement:
$conn = new mysqli($servername, $username, $password,$database);
Or you can do it in your select statement:
$sql ="SELECT * FROM `yourdatabase`.`producten`";
If you donĀ“t set your database your query is wrong
Your query is right just write your database name in mysqli constructor.
$conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
Visit: https://www.php.net/manual/en/mysqli.construct.php
Please give database name also, check below code.
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = ""; //Enter database name
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql ="SELECT * FROM `producten`";
$result = mysqli_query($conn, $sql) or die("Bad query");
$conn->close();
?>

MySQL table value be equal with php var (then display it)

I have the following MySQL table and I would like to do that RVR11 (500) be equal with a php var $RVR11
I have this code but it doesnt work.
$servername = "localhost";
$username = "root";
$password = "";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$price = mysql_query("SELECT RVR11 FROM tablename");
$result = mysql_fetch_array($RVR11);
echo $result['RVR11'];
Sorry about this beginner question.
You should choose a database before any queries, you can pass the fourth arguments to new mysqli constructor.

can anyone explain where have i done something wrong?

When I run this code it shows parse error. Can anyone help me with this?
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "form";
// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " .$conn->connect_error);
}
SELECT * FROM my_db;
$conn->close();
You should use php tags and remove those ">" also you should use mysqli_query to query the data from mysql
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "form";
// Create connection
$conn = new mysqli($servername, $username,$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " .$conn->connect_error);
}
$query = mysqli_query($conn,"SELECT * FROM my_db");
$conn->close();
?>

Mysqli to variable in PHP

I've been a few days working on my project, but just stay stuck a specific part.
My problem is that i like to get a value from the database (mysqli)
But i always receive a 0. So, no value.
This is my code:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user_idSQL = "SELECT user_id FROM ss_devices WHERE serialkey = '$serialkey'";
$user_idResult = $conn->query($user_idSQL);
$user_id = $user_idResult;
The script should see what the userid, it is in equality with the specified serial number.
But when i take a serial number like: FJRI433. I always get a 0. But in the database has this serial number user_id: 3.
I hope someone can help me out whith this problem.
Thanks.
You are executing the query but not fetching any results.
The manual has plenty of examples of using mysqli :
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
//Do stuff with the next row.
}
}
Try this perhaps...
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn=mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ( mysqli_connect_errno() ) {
die("Connection failed: " . mysqli_connect_error() );
}
$user_idSQL = "SELECT `user_id` FROM `ss_devices` WHERE `serialkey` = '$serialkey'";
if( $user_idResult = mysqli_query( $conn, $user_idSQL ) ) {
while ($row = mysqli_fetch_assoc($user_idResult)) {
echo $row['user_id'].'<br />';
}
}
mysqli_close( $conn );

I cant insert in mysql from php

whats the solution for this problem Number of elements in type definition string doesn't match number of bind variables
session_start();
$regValue1 = $_GET['un'];
$regValue2 = $_GET['pass'];
$regValue3 = $_GET['fn'] ;
$regValue4 = $_GET['ln'];
$regValue5 = $_GET['age'] ;
$regValue6 = $_GET['sex'];
$regValue7 = $_GET['em'] ;
echo "hello: ".$regValue3.".";
$servername = "localhost";
username = "root";
$password = "b4sonic";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$stmt = $conn->prepare("INSERT INTO register(un,pass,fn,ln,age,sex,email) VALUES (?,?,?,?,?,?,?)");
bind_param("sss",$regValue1,
$regValue2,$regValue3,$regValue4,$regValue5,$regValue6,$regValue7);
$stmt->execute();
echo "New records created successfully";
$stmt->close();
$conn->close();
?>
I have this code in php , i am trying to insert data to mysql but i face this problem Number of elements in type definition string doesn't match number of bind variables
Your type string in your statement doesn't have enough type specifiers in it.
bind_param("sss",$regValue1,$regValue2,$regValue3,$regValue4,$regValue5,$regValue6,$regValue7);
says that you have type "sss" which only corresponds to 3 of the 7 variables you specified. You need to add types for the rest.
From the documentation:
var1
The number of variables and length of string types must match the parameters in the statement.
The commands you are using are for using PDO to connect to an sql database and you are using mysqli. I gave an example using mysqli below that should work. The other option would be to change the connection to PDO type rather than mysqli.
<?php
session_start();
$regValue1 = $_GET['un'];
$regValue2 = $_GET['pass'];
$regValue3 = $_GET['fn'] ;
$regValue4 = $_GET['ln'];
$regValue5 = $_GET['age'] ;
$regValue6 = $_GET['sex'];
$regValue7 = $_GET['em'] ;
echo "hello: ".$regValue3.".";
$servername = "localhost";
$username = "root";
$password = "b4sonic";
$dbname = "blog";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO register(un,pass,fn,ln,age,sex,email)".
"VALUES (`$regValue1`,`$regValue2`,`$regValue3`,`$regValue4`,`$regValue5`,`$regValue6`,`$regValue7`)";
$conn->query($sql);
echo $conn->affected_rows. " new records created successfully";
$conn->close();
?>

Categories