How to Select Specific Info From MySQL - php

I'm trying to a number from a specific table, row, and column but can't seem to get it right.
The table is tblproducts. The column is qty. And I need to select where id is 13.
This is the code I'm using but it isn't returning anything:
<?php
$con=mysqli_connect("localhost","blank","blank","blank");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT qty FROM tblproducts");
while($row = mysqli_fetch_array($result))
{
echo $row['13'];
}
mysqli_close($con);
?>
Any help would be appreciated

do it all in the query like so
SELECT qty FROM tblproducts where id =13
no loop needed
$result = mysqli_query($con,"SELECT qty FROM tblproducts where id =13");
$row = mysqli_fetch_assoc($result);
echo $row['qty'];

Your sql syntax is a bit off, see my corrections. Your echo of the row['13'] also looks suspicous. You can use print_r() to print arrays to find which key you need to use. It is probably $row['qty'].
<?php
$con=mysqli_connect("localhost","blank","blank","blank");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT qty FROM tblproducts where id = 13");
while($row = mysqli_fetch_array($result))
{
print_r($row); // You can use print_r to have a look at your full array. The index '13' might not be what you where looking for.
}
mysqli_close($con);
?>

Related

MySQL result in PHP by ID

How can i display something out of my databse by an ID number?
I already can get the data out of the database by searching the row 'content' but i want to get it by an ID number.
I have three rows in my database table: ID, pagename and content.
Here is the code that i already have for getting the data:
<?php
$con=mysqli_connect("localhost","jordyyd108_mario","testdb","jordyyd108_mario");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM pages");
while($row = mysqli_fetch_array($result)) {
echo $row['content'] ;
}
mysqli_close($con);
?>
Thanks!
use WHERE clause and pass your id try
mysqli_query($con,"SELECT * FROM pages WHERE ID = '2'");
or pass a variable
mysqli_query($con,"SELECT * FROM pages WHERE ID = '$id'");
You can by using WHERE :-
$result = mysqli_query($con,"SELECT * FROM pages where id = 1");

Previous data still shows after filter using PHP

I have a query to show all data using PHP and then I have a second query for filtering using startdate and enddate but when I use the second query, the data table shows the first query output plus the second query output.
Here's my code.
Here is my query for the first output:
<?php
session_start();
$con=mysqli_connect("localhost","root","","leavecalendar");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = mysqli_query($con,"SELECT * FROM timekeeping ");
while($row = mysqli_fetch_array($sql))
if(!empty($row)){
$_SESSION['row'][] = $row;
header("location:timekeepinglogs.php");
//print_r($_SESSION);
}
//header("location:timekeepinglogs.php");
//print_r($_SESSION);
?>
Here's a screenshot of what the result looks like:
Now here's the second query which is triggered by the generate button
<?php
session_start();
$row = $_SESSION['row'];
$con=mysqli_connect("localhost","root","","leavecalendar");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = mysqli_query($con,"SELECT * FROM timekeeping WHERE createddate >= '$_POST[startdate]' AND createddate <='$_POST[enddate]' ");
while($row = mysqli_fetch_array($sql))
if(!empty($row)){
$_SESSION['row'][] = $row;
//header("location:timekeepinglogs.php");
//print_r($_SESSION);
}
header("location:timekeepinglogs.php");
//print_r($sql);
?>
Now here's what the second query does (notice that in the first picture the last row has empty clockout) I filtered it using date range may 2 to may 3 so it should only show 3 rows. Take a look
As you can see it just select the data in date range and shows it on the bottom of the data table. I want the filtered data only to show.
Any help will be greatly appreciated.
Probably you are appending the data on each request in table, So you need to clear the sessions data on each new request
Suppose you are searching by dates , before that you need to clear sessions data using
unset($_SESSION['row']);

Fastest way to find, fetch and replace part of the string in database

I have a set of records in database table and one of the columns has string value. Something like HeyHelloWorld1 or HeyGoodDayWorld2 or HeyHowdyWorld32. I need to change them all to GoodEveningWorld (and whatever comes at the end of the string). I'm trying to run script but it takes too long and I was wondering if anyone knows of a fastest way to implement this
The script:
$con=mysqli_connect("localhost","user","password","db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$str_to_look_for = 'Hey';
$new_str = 'GoodEvening';
$result = mysqli_query($con,"SELECT * FROM table1 WHERE item LIKE '%$str%'");
if(!$result) echo "No records found?";
else
{
echo " Found ".mysqli_num_rows($result)." rows<br/>... Executing script...";
while($row = mysqli_fetch_array($result))
{
$val = strstr($row['item'], '/World');
$new_val = $new_str.$val;
$id = $row['ID'];
insert($new_val, $id, $con);
}
echo " DONE!";
}
function insert($x, $id, $con)
{
$result = mysqli_query($con,"UPDATE table1 SET item = '$x' WHERE ID = '$id'");
if (!$result) echo "missed..<br/>";
}
Check out the MySQL String functions and consider replacing it directly in the database.
UPDATE table1
SET item =
CONCAT(
'GoodEvening',
SUBSTRING( item, INSTR( item, 'World') )
)
This would work for anything followed by World, rather than just HelloWorld or HayWorld.

Nested while loop is not working as expected

I'm having a problem in php. I tried to show all data and then put them in a nested loop. but the second loop only returns nulls. I don't know what I did wrong.
<?php
ini_set('max_execution_time', 36000);
$con=mysqli_connect("localhost","root","XXX","YahooFin");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"show tables from yahooFin where not tables_in_yahooFin = 'nasdaqCompanyList' and not tables_in_yahooFin = 'companylist'");
while($row = mysqli_fetch_array($result)) {
$result2 = mysqli_query($con, "select * from ".$row['Tables_in_yahoofin']." where entry_date = '2013-06-03'order by entry_date asc limit 1");
while ($row2 = mysqli_fetch_array($result2)); //<== This line gives me null
{
var_dump( $row2);
echo "<br>";
}
}
var_dump($row);
mysqli_close($con);
?>
There is an extra ; semicolon which shouldn't be there after your loop
while ($row2 = mysqli_fetch_array($result2)); //<== This line gives me null
//^ remove this one
Also You probably have a typo tables_in_yahooFin is used in first query while Tables_in_yahoofin is used in second.

MySQL - count total number of rows in php

What is the best MySQL command to count the total number of rows in a table without any conditions applied to it? I'm doing this through php, so maybe there is a php function which does this for me? I don't know. Here is an example of my php:
<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("some command");
$row = mysql_fetch_array($result);
mysql_close($con);
?>
<?php
$con = mysql_connect("server.com","user","pswd");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("db", $con);
$result = mysql_query("select count(1) FROM table");
$row = mysql_fetch_array($result);
$total = $row[0];
echo "Total rows: " . $total;
mysql_close($con);
?>
Either use COUNT in your MySQL query or do a SELECT * FROM table and do:
$result = mysql_query("SELECT * FROM table");
$rows = mysql_num_rows($result);
echo "There are " . $rows . " rows in my table.";
mysqli_num_rows is used in php 5 and above.
e.g
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("Result set has %d rows.\n",$rowcount);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
Use COUNT in a SELECT query.
$result = mysql_query('SELECT COUNT(1) FROM table');
$num_rows = mysql_result($result, 0, 0);
you can do it only in one line as below:
$cnt = mysqli_num_rows(mysql_query("SELECT COUNT(1) FROM TABLE"));
echo $cnt;
use num_rows to get correct count for queries with conditions
$result = $connect->query("select * from table where id='$iid'");
$count=$result->num_rows;
echo "$count";
for PHP 5.3 using PDO
<?php
$staff=$dbh->prepare("SELECT count(*) FROM staff_login");
$staff->execute();
$staffrow = $staff->fetch(PDO::FETCH_NUM);
$staffcount = $staffrow[0];
echo $staffcount;
?>
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="SELECT Lastname,Age FROM Persons ORDER BY Lastname";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
echo "number of rows: ",$rowcount;
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
it is best way (I think) to get the number of special row in mysql with php.
<?php
$conn=mysqli_connect("127.0.0.1:3306","root","","admin");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="select count('user_id') from login_user";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
echo "$row[0]";
mysqli_close($conn);
?>
Still having problem visit my tutorial http://www.studentstutorial.com/php/php-count-rows.php
$sql = "select count(column_name) as count from table";
Well, I used the following approach to do the same: I have to get a count of many tables for listing the number of services, projects, etc on the dashboard. I hope it helps.
PHP Code
// create a function 'cnt' which accepts '$tableName' as the parameter.
function cnt($tableName){
global $conection;
$itemCount = mysqli_num_rows(mysqli_query($conection, "SELECT * FROM `$tableName`"));
echo'<h6>'.$itemCount.'</h6>';
}
Then when I need to get the count of items in the table, I call the function like following
<?php
cnt($tableName = 'projects');
?>
In my HTML front end, so it renders the count number
It's to be noted that I create the cnt() function as a global function in a separate file which I include in my head, so I can call it from anywhere in my code.

Categories