FIND_IN_SET not retrieving results (PHP) - php

I am troubles with this method not returning results. There are ids within 'table' that match the array. I guess it's not liking something, except I cannot quite put my finger on it.
$define = ",8,9,10,";
// ** Data Retrieve ** //
mysql_select_db($database_db, $db);
$query_data = "SELECT * FROM table WHERE FIND_IN_SET('".$define."', id)";
$data = mysql_query($query_data, $db) or die(mysql_error());
$row_data = mysql_fetch_assoc($data);
$totalRows_data = mysql_num_rows($data);

In FIND_IN_SET the target value is first, and the list second. So, the query should be as follows:
SELECT * FROM table WHERE FIND_IN_SET(id,'".$define."');

Related

How to store a PHP variable from a SQL table INT camp

This is my table:
All I want to do is to obtain the '75' int value from the 'expquim' column to later addition that number into another (75+25) and do an UPDATE to that camp (now it is 100).
Foremost, there are dozens of ways to accomplish what you want to do. If you're querying the table, iterating over results and doing some conditional checks, the following will work for you. This is pseudo code... Check out the function names and what parameters they require. $db is your mysqli connection string. Obviously replace tablename with the name of your table. The query is designed to only select values that are equal to 75. Modify the query to obtain whatever results you want to update.
This should get you close to where you want to be.
$query = "SELECT * FROM tablename WHERE idus='1'";
$result = mysqli_query($db, $query);
while($row = mysqli_fetch_assoc($result)) {
if($row['expquim'] == 75){
$query2 = "UPDATE tablename SET expquim='".$row['expquim']+25."' WHERE idus='".$row['idus']."' LIMIT 1 ";
$result2 = mysqli_query($db,$query2);
}
}

PHP MySQL limits query results when in WHILE loop

I have a weird problem where mysql_ limits results from a query, but only when it's in a while loop & adding ORDER BY listing_name ASC. Here's an example:
$listings = mysql_query("SELECT * FROM listings");
$listingcount = mysql_num_rows($listings);
$listingcount returns 157 rows in the listings table.
And here's what happens in the while loop.
$listings = mysql_query("SELECT * FROM listings ORDER BY listing_name ASC");
while($listingresult = mysql_fetch_assoc($listings)){
//grab some data from the query using $listingresult
//e.g: $somedata = $listingresult['listing_name'];
echo "some data here:". $somedata;
}
This only echos 97 rows. None of the values in listing_name are NULL, empty etc they all contain & start with letters.
What am I missing here? It doesn't seem right

Return frequency of string in MYSQL column with PHP

after searching for several questions, i'm still struggling with mysql queries in PHP, my current goal is to do a MYSQL query that counts how many repeated strings are in a column and then return this amount in a INT variable to be written in the database.
The current code looks like:
//Fetch value from form and uppercase the string
$glitter = utf8_decode(strtoupper($_POST['code_string']));
$magic = mysql_query("SELECT COUNT(*) FROM table WHERE CODE_STR = '$glitter'");
The next step is inserting the var $magic into a INT field in the database, however the value is always 0.
Where is my mistake?
Thanks.
mysql_query returns a resource on success, or FALSE on error.
try this
$magic = mysql_query("SELECT COUNT(*) as count FROM table WHERE CODE_STR = '$glitter'");
$row = mysql_fetch_assoc($magic);
$count = $row['count'];
Your approach is correct. What you need to do now is
Change your query from
$magic = mysql_query("SELECT COUNT(*) FROM table WHERE CODE_STR = '$glitter'");
to
$magic = mysql_query("SELECT COUNT(*) as total_num FROM table WHERE CODE_STR = '$glitter'");
mysql_fetch_assoc() Use the returned value from table
$magic_row = mysql_fetch_assoc($magic);
echo $magic_row['total_num'];
See
http://php.net/manual/en/function.mysql-fetch-assoc.php

Using mysql_num_rows with multiple tables?

Here's the code I've started with:
$result = mysql_query("SELECT * FROM apis_hashes_a", $link);
$count = mysql_num_rows($result);
What I need to do is to get the total sum of all rows for all tables that start with apis_hashes_.
There are tons of tables and new ones are being added all the time, but all of these tables start with apis_hashes_ at the beginning. Is this something that would be possible to do or do I have to list every table individually in the PHP code?
JUST use SUM() IN SQL.Use the code below
<?php
$query = "SELECT SUM(TABLE_ROWS) as score
FROM INFORMATION_SCHEMA.TABLES
WHERE SCHEMA = '{your_db_name}'";
$result = mysql_query($query, $link);
while($row=mysql_fetch_array($result)){
$total_rows = $row['score'];
}
echo $total_rows;
?>
Hope this helps you
The sql:
show tables like 'apis_hashes_%'
will return the list of all your tables, then you need loop thru all the names of the tables with the mysql sum() function.

Is it possible to avoid the "AS" parameter when counting total rows of a mysql table? (php)

Now I use this code to count the total rows of a table:
$sql = "SELECT COUNT(*) AS Total FROM MyTable";
$result = mysqli->query($sql);
$numberOfRows = mysqli_fetch_object($result);
$numberOfRows = $numberOfRows->Total;
I've tried to dump different results that I get when I don't use the "AS" parameter in the query and searched around in the internet about it, but despite the many examples I've found none of them shows the code to retrieve directly the result without the "AS" paramenter.
...
From the answers and comments received I've tried these two code blocks that give the expected result:
$sql = "SELECT COUNT(*) FROM MyTable";
$result = mysqli->query($sql);
And then:
With fetch array:
$numberOfRows = $result->fetch_array($result);
$numberOfRows = $numberOfRows[0];
With fetch assoc:
$numberOfRows = $result->fetch_assoc($result);
$numberOfRows = $numberOfRows["COUNT(0)"];
Performance wise I've found the fetch array works slightly better (tried without opcode).
If you don't use AS to assign an alias, the name of the column in the output will be COUNT(*). You should then be able to retrieve it with:
$numberOfRows = $numberOfRows->{"COUNT(*)"}

Categories