I need to know If php, mysql or even my script has a limit of values or memory, or something that doesn't show me all the values of my database table.
Here is my script:
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "SELECT id_team,name FROM teams ORDER BY id_team LIMIT 50;";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('id_team'=>$row[0],'name'=>$row[1]));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
If I run this script with the LIMIT 50 condition, it works, but when I change the limit to 70 or more It doesn't work anymore and doesn't print me anything. I don't know if there is like a limit of memory or values or something. Can someone help me please?
This is my output when I use Limite 5 (for example):
{"result":[{"id_team":"3","name":"Tonkas"},{"id_team":"4","name":"Combis"},{"id_team":"5","name":"Looney
Tunes"},{"id_team":"6","name":"Bullets"},{"id_team":"7","name":"Valkiry"}]}
I use a counter inside the loop and It always return me the number of the LIMIT I give in the condition, If I give LIMIT 100, the counter prints me 100, but I don't get anything from the array.
PHP Version 5.6.29 & JSON version 1.2.1 - Hosted in 1and1
SOLVED
It was the codification (charset) when the loop found a invalid character (in this case letter 'ñ') it just break and didn't continue filling the array, just set the charset and worked:
mysqli_set_charset($con,'utf8');
Related
i have a large data in my mysql database (500 rows), when i am trying to get all of them , the data is not coming from database.
Working Code
<?php
include '../db/db.php';
$categoriesDataQuery = mysqli_query($conn, "SELECT * FROM subcategories LIMIT 268");
while ($row = mysqli_fetch_assoc ($categoriesDataQuery)) {
$categoryData [] = $row;
}
$categoryNewData = $categoryData;
print (json_encode ($categoryNewData)) ;
mysqli_close ($conn);
?>
Not Working Code
<?php
include '../db/db.php';
$categoriesDataQuery = mysqli_query($conn, "SELECT * FROM subcategories");
while ($row = mysqli_fetch_assoc ($categoriesDataQuery)) {
$categoryData [] = $row;
}
$categoryNewData = $categoryData;
print (json_encode ($categoryNewData)) ;
mysqli_close ($conn);
?>
Maybe Mysql database have some limitation when user select a large data ?!
Who ever encountered this?
The problem is not with the SQL query or the amount of data that you're trying to encode. For some reason, json_encode can return an empty string instead of throwing an error when it encounters an invalid character.
You have to make sure that the SQL connection's charater encoding matches your database table's encoding. For example if you're using UTF-8 encoding on your table, you need to call the following function to force the same encoding on the PHP client:
mysqli_set_charset($conn, "utf8");
See the PHP docs for more information.
So i'm working with huge MYSQL database of english words. the list has upwards of 180k words. Im trying to run a query, and then compute something using every word in the database. The problem is the database is so big, and it seems to be freezing my browser (Im working locally with MAMP). Is there a way I can do five queries at a time, instead of trying to run through the whole thing, so i can see the results coming in gradually in my browser? Heres the code:
<?php
require 'class.thing.php';
require 'db_config.php';
$result = mysql_query("SELECT * FROM words") or die ("Could not complete query");
while($row = mysql_fetch_array($result))
{
$word = $row['word'];
$compute = $word."thing";
$finished = new method( $compute );
if ($finished->successfull()) {
echo "<br/>";
echo "worked!";
echo "<br/>";
} else {
echo "uh oh..";
}
}
?>
** im looking for something like:
$result = mysql_query("SELECT * FROM words") or die ("Could not complete query LIMIT 0,5");
get results
wait 5 seconds
$result = mysql_query("SELECT * FROM words") or die ("Could not complete query LIMIT 0,5");
get results
etc...
You should take a look at this related question:
What is the best way to paginate results in SQL Server
Note that you can compare words in SQL alphabetically (check whether your settings use case sensitive sorting or not), so you can do something like:
SELECT * FROM words WHERE word <= "ferrous" ORDER BY word
If you're using an auto-incrementing id, you can use that to subdivide as well (it'd probably be easier).
EDIT:
As pointed out below, you can use LIMIT in MySQL.
Example:
SELECT * FROM words LIMIT 0, 1000
(This would display the first 1000 results).
1.) Can you nest a msqli_query inside a while loop?
2.) If yes, why would the PHP below not write any data to the precords table?
If I echo a $build array variable it shows properly, but the mysqli insert writes nothing to the table in the DB. THe code does not error out anywhere, so what am I missing about this?
$data = mysqli_query($con,"SELECT * FROM Cart WHERE Buyer_ID='$_SESSION[cid]' AND Cart_Date='$_SESSION[cdate]'");
while($build = mysqli_fetch_array($data))
{
//echo $build[idex]."<br>";
mysqli_query($con,"INSERT INTO precords (precord,Buyer_ID,Account,Purchase_Date,Item_Number,Item_Qty,Item_Title,Item_FPrice,Item_FFLFlag,ccpass) VALUES ('$build[idex]','$build[Buyer_ID]','$build[Cart_Date]','$build[Item_Number]','$build[Item_Qty]','$build[Item_Title]','$build[Item_FPrice]','$build[Item_FFLFlag]','N')");
};
Thanks for any help.
** P.S. - This code is meant to move certain values from a TEMPORARY table/session variables, over to a permanent record table, but the loop is needed since there is more than one product in the cart associated with the user/session.
yes you can use it in a loop and
you may wanna add mysql_error() function to find out what's wrong with it and try to fix it or by adding the error to the question so we can tell you what to do
$data = mysqli_query($con,"SELECT * FROM Cart WHERE Buyer_ID='$_SESSION[cid]' AND Cart_Date='$_SESSION[cdate]'");
while($build = mysqli_fetch_array($data))
{
// echo $build[idex]."<br>";
mysqli_query($con,"INSERT INTO precords(precord,Buyer_ID,Account,Purchase_Date,Item_Number,Item_Qty,Item_Title,Item_FPrice,Item_FFLFlag,ccpass)
VALUES ('$build[idex]','$build[Buyer_ID]','$build[Cart_Date]','$build[Item_Number]','$build[Item_Qty]','$build[Item_Title]','$build[Item_FPrice]','$build[Item_FFLFlag]','N')")
or die (mysql_error());
};
in a simplified form when you want to fetch data from a database to display in html list I intentionally added mysqli ORDER BY which have only two order ASC[ascending] and DESC[descending] and I also used mysqli LIMIT which i set to 3 meaning that number of result fetch from the database should be three rows only
I concur with the answer of ali alomoulim
https://stackoverflow.com/users/2572853/ali-almoullim
MY SIMPLIFIED CODE FOR THE LOOPING WHILE MYSQLI ORDER BY AND LIMIT
$usersQuery = "SELECT * FROM account ORDER BY acc_id DESC LIMIT 3";
$usersResult=mysqli_query($connect,$usersQuery);
while($rowUser = mysqli_fetch_array($usersResult)){
echo $rowUser["acc_fullname"];
}
Hi Guys my code is below.
What I'm trying to do is to update all of the value in mysql that satisfy a specific a specific condition. Now when I just type in the mysql query:
UPDATE `$DBNAME`.`video` SET `Secret_key` = '0'
The query works fine but I can't seem to write php code that will do this.
Any help is welcomed
<?php
// Part 1 (works fine)
include("/home3/kintest2/public_html/include/config.local.php");
$connect= mysql_connect ($DBHOST,$DBUSER,$DBPASSWORD);
$select= mysql_select_db($DBNAME, $connect);
// End of Part 1
// Part 2 (works fine)
$test2= "SELECT * FROM `video`";
$results= mysql_query($test2, $connect);
$num_rows = mysql_num_rows($results);
// End of part 2
// Part 3 ( Not too sure about this part)
for ($num_rows= $count; $count >= 0; $count--)
{
mysql_query("UPDATE `$DBNAME`.`video` SET `Secret_key` = '0'",$connect);
}
// End of part 3 ( Not too sure about this part)
?>
<?php
// Part 1 (works fine)
include("/home3/kintest2/public_html/include/config.local.php");
$connect= mysql_connect ($DBHOST,$DBUSER,$DBPASSWORD);
$select= mysql_select_db($DBNAME, $connect);
// End of Part 1
//no need of part 2 to update records in table
//part 3 loop removed
mysql_query("UPDATE `$DBNAME`.`video` SET `Secret_key` = '0' ",$connect);
?>
Try this. May it will solve ur problem.
For starters I'd take the query out of the loop (and just delete the loop). If it still doesn't work, try hard coding the database name. If that doesn't work, make sure $connect is what you think it is. Look up mysql_query() and make sure you're using it right.
If you try everything above, you'll either find the problem or at least figure out what the problem is not.
i will keep it simple to understand :
1) i have a database with 2 columns - word and text
2) Each word has approx. 600,000 lines of text associated with it.
3) Iam a .net person shifting to php and mysql - so a little knowledge.
MY requirement :
1) I will pass the word through a form
2) The form will connect to the database and should display 2000 random lines from those 600,000 which should be non-repetitive
My current progress :
<?php
$con = mysql_connect("localhost","text_minx","pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM Data
WHERE word='health'");
while($row = mysql_fetch_array($result))
{
echo $row['lines'];
echo "<br />";
}
?>
This shows all the lines. What i want is to read this $row['lines'] in a possible array and randomly select 2000 lines from it - and they should be non-repetitive.
Any help please?
Something like this:
$result = mysql_query("SELECT DISTINCT * FROM Data WHERE word='health' ORDER BY RAND() LIMIT 2000");
It's more efficient to select the 2000 random lines by MySQL instead of by PHP, as above.
Also note that SELECT DISTINCT will select only unique rows, which you should probably remove anyway. If you specify the column names instead of using * then you can choose which columns you want to be unique - although this also depends somewhat on how your table is built.
i think this is the way you want
SELECT * FROM Data
WHERE word='health' ORDER BY RAND() LIMIT 0,2000
this will give you 2000 records sorted by any random order
You need to split your row result into array (as it's currently a string), and then you can select 2000 lines randomly.
Assuming that the text has like breaks as line separators, it would look like that:
echo $row['lines']; //starts from this line of your code
$data = explode("\n", $row['lines']);
shuffle($data);
$random_lines = array_slice($data, 0, 2000);
This does not take care of non-repetition, though. If I understand your need correctly, you can use array_unique() function before passing it to shuffle().