Thanks a lot to anyone who can help me out! I've uploaded a gift-shop style website to a server running PHP v 5.4.19. The problem is that the image and page references that were fetched on xampp don't appear in the array on the server. Why is this and what is wrong with my code (apart from me not adopting msqli yet)?
function smallDisplay()
{
$query = mysql_query($con, "SELECT `imageSrcQ`, `productCode` FROM `products` WHERE `displayCode` >= 11 && `displayCode` <= 12");
while ($results_row = mysql_fetch_array($query))
{
$returned_results[] = array(
'productCode' => $results_row['productCode'],
'imageSrcQ' => $results_row['imageSrcQ']
);
}
return $returned_results;
}
$results = smallDisplay();
echo $results;
$x = 0;
foreach ($results as $result)
{
${'img'.$x} = $result['imageSrcQ'];
$x++;
}
echo $img0;
Edit: mysql_query does not take $con as the first argument, see: http://php.net/manual/en/function.mysql-query.php.
Change your code to:
$query = mysql_query("SELECT `imageSrcQ`, `productCode` FROM `products` WHERE `displayCode` >= 11 && `displayCode` <= 12");
and it should work.
As a note you might want to declare your $returned_results - array before the loop, and use the mysql_fetch_assoc() instead
Related
I have a weird problem. I read all related topic but there was no any solution for me. There are bunch of php codes on my project and all work perfectly but that one :
<?php
require("connection.php");
$query = "SELECT * FROM mrf WHERE completed=0 AND archive IS NULL ORDER BY projectname";
$get = $db->prepare($query);
$get->execute();
if ($get->rowCount()!=0){
foreach ($get as $res) {
'<li class="list-group-item"><span class="font-weight-bold">Product(s) : </span>';
$pieces = explode(",", $res['products']);
$pieces_count = count($pieces);
for ($i=0; $i <= $pieces_count -1 ; $i++) {
$query2 = "SELECT * FROM suppliers WHERE id = :id";
$get2 = $db->prepare($query2);
$get2->bindValue(":id",$pieces[$i]);
$get2->execute();
if ($get2->rowCount()!=0){
foreach ($get2 as $res2) {
echo $res2['name'].'<b>/</b>';
}
}
}
echo '</li>';
It works localhost perfectly but on server the result is empty.
I have checked the error(s)
error_reporting(E_ALL);
ini_set('display_errors', 1);
But there is no any error. Also If there had been an error, I would have seen it on localhost, because error_reporting works on localhost as well as on live server.
I checked typos, no any typo
Checked cpanel everything related to PHP. All is OK. By the way php ver is 5.6
Any idea??
-----------------------------------RESULT----------------------------
-I have checked all codes again and didn't find any error.
-I contacted my hosting company. They said my server has reached RLimitMEM (don't know what it is) So they increased my RlimitMEM value.
- I amnot satisfied with hosting company's answer but the code is working now.
- I appreciate for all answers and advices.
Replace your code with the following and check
<?php
require("connection.php");
$query = "SELECT * FROM mrf WHERE completed=0 AND archive IS NULL ORDER BY projectname";
$get = $db->prepare($query);
$get->execute();
if ($get->rowCount()!=0){
foreach ($get as $res) { ?>
'<li class="list-group-item"><span class="font-weight-bold">Product(s) :
</span>';
<?php
$pieces = explode(",", $res['products']);
$pieces_count = count($pieces);
for ($i=0; $i <= $pieces_count -1 ; $i++) {
$query2 = "SELECT * FROM suppliers WHERE id = :id";
$get2 = $db->prepare($query2);
$get2->bindValue(":id",$pieces[$i]);
$get2->execute();
if ($get2->rowCount()!=0){
foreach ($get2 as $res2) {
echo $res2['name'].'<b>/</b>';
}
}
}
echo '</li>';
}
}
You have not closed the PHP, closing curly braces were not there...
Try echoing it out:
foreach ($get as $res) {
'<li class="list-group-item"><span class="font-weight-bold">Product(s) : </span>';
Missing something there, no?
foreach ($get as $res) {
echo '<li class="list-group-item"><span class="font-weight-bold">Product(s) : </span>';
Sometimes it's the simple things.
Of note is PHP is perfectly fine with just having a useless string defined:
'foo';
Sandbox
It won't complain about that. For other languages this may be an issue, but PHP is very fault tolerant (sometimes to much so).
PS require("connection.php"); is not a function, while it will work with the () it's considered a bad practice. Instead require "connection.php"; works fine. This helps to differentiate it from a function when you glance at the code.
You should also move this around a bit:
foreach ($get as $res) {
//...
for ($i=0; $i <= $pieces_count -1 ; $i++) {
$query2 = "SELECT * FROM suppliers WHERE id = :id";
$get2 = $db->prepare($query2);
$get2->bindValue(":id",$pieces[$i]);
$get2->execute();
And do it like this:
$query2 = "SELECT * FROM suppliers WHERE id = :id";
$get2 = $db->prepare($query2);
foreach ($get as $res) {
//...
for ($i=0; $i <= $pieces_count -1 ; $i++) {
$get2->execute(['id'=>$pieces[$i]]); //instead of binding, you can just do it in execute too
And do the Prepare outside of the loop, this will improve performance in most cases. Every time you do a Prepare the DB is used to compile the query instructions, then when you execute it only the data is sent. This is how they defeat SQl Injection, because the SQL and the DATA are handled separately.
So by preparing it within the Loop you are asking the DB for unnecessary work. The format of the SQL does not change per/loop only the data does. Therefor, there is only the need to execute the query for each iteration.
Lastly:
Your missing two closing }} in your code for the question. Make sure to fix those syntax errors. I suspect they are just a omission in the question as you stated the code works locally.
I need to check if a value exists in my database
I have a table where every user has an unique code. For example: 5h27f.
These values and users add up very quickly. So very soon I might have +2000 unique codes. What's the best, fastest and most efficient way to check if a value is unique?
foreach ($users as $user) {
$is_unique = FALSE;
while ($is_unique == FALSE) {
$code = unique_code();
$query = "SELECT * FROM unique_code_table WHERE code='$code';";
$res = $mysqli->query($query);
if ($res->num_rows > 0 {
} else {
$is_unique = TRUE;
}
}
}
OR
$query = "SELECT code FROM unique_code_table;";
$res = mysqli->query($query);
$codes = array();
$i = 0;
while ($row = $res->fetch_object()) {
$codes[$i] = $row->code;
$i++;
}
$code = unique_code();
while (in_array($code, $codes) {
$code = unique_code();
}
(this code might not be 100% accurate, I've written this just to explain the purpose of the question.)
I'd say that one query trip to the database vs. potentially 2000+ is significantly better to do. Second script will be significantly faster.
On the first code a LIMIT 1 would do wonders but compared to the second query it will pale as far as benchmarks are concerned.
Put the following at the bottom of your script to fine tune and benchmark:
PHP 5.4 +
$sParseTime = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
echo $sParseTime;
CODE :
$nerd_result = mysql_query("select * from nerd_profile where nerd_reg_no = '$reg_no'");
$nerd_data = mysql_fetch_array($nerd_result);
$tags = array();
$tags = explode(",",$nerd_data['nerd_interests']);
for($i = 0; $i < sizeof($tags)-1; $i++)
{
if($i != sizeof($tags)-2)
{
$sub_query = $sub_query."`tags` like %".$tags[$i]."% or ";
}
else
{
$sub_query = $sub_query."`tags` like %".$tags[$i]."% ";
}
}
$proper_query = "select * from `qas_posts` where ".$sub_query." and `post_date` like '%$today%'";
$result = mysql_query($proper_query);
while($each_qas = mysql_fetch_array($result))
Description :
I am adding the like clause along with php variable in a string and concatenating it with the further variables with like clause to come. In the end when I echo I get the perfect query that I want but
mysql_fetch_array()
does not accept that generated query rather if I hard code it , it works perfect what am I doing wrong ?? can I do that ??
When doing string comparisons in mysql you need to make sure you have quotes around your comparison value.
$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' or ";
and
$sub_query = $sub_query."`tags` like '%".$tags[$i]."%' ";
I have php code with two queries. One is in an Oracle database, with the result being about 100 rows of data (within the data there are SKU numbers). The other is in a mysql database with about 30 rows of data (with the data there are also SKU numbers).
I need to compare each SKU in the first query to the second query, line by line. If the SKU in the first query also appears in the second query, then I need it to echo the SKU.
All SKUs in the second query are in the first query, so I would expect the result to echo all 30 SKUs, but it does not. Depending on how I change the syntax, it echos 17 rows, 100 rows, or no rows at all.
It's worth noting that both queries work fine. The Oracle query is insanely long and has a lot of sub-queries in it, so I will not include that full query below. The Oracle query returns the results perfectly in SQL Developer, and the MySQL query returns the results perfectly in HeidiSQL. Both queries have been tested and echoed as tables in other php files to make sure that they were working fine.
Below is what the php file that I am trying to fix looks like so far;
<?php
$conn = oci_connect($user, $pswd, $hst);
$sql = oci_parse($conn,"[Really long Oracle Query]");
while ($row = oci_fetch_assoc($sql))
{
$sku = $row['SKU'];
$con = mysql_connect("[database host]", "[database user]", "[database password]");
mysql_select_db("[database_name]");
$sqls = "SELECT * FROM [table_name] WHERE [this_date_column] BETWEEN
DATE_SUB(NOW(), INTERVAL 14 DAY) AND NOW()";
$result = mysql_query($sqls);
$mailer = NULL;
while($rows = mysql_fetch_assoc($result))
{
$m_sku = $rows['sku'];
if ($m_sku == $sku)
{
$mailer == 'false';
}
}
if ($mailer == 'false')
{
echo $m_sku;
echo "<br>";
}
}
?>
Again; there are only 30 SKUs in the MySQL query, but over 100 SKUs in the Oracle query. All SKUs in the MySQL query are definitely in the Oracle query.
Anyone see what I am doing wrong?
Thanks in advance,
-Anthony
You have basic sintacsis errors:
= is used to assign values
== compares 2 variables (not counsidering there type *)
=== compares 2 variables including there types.
your code should look something like this:
while($rows = mysql_fetch_assoc($result))
{
$m_sku = $rows['sku'];
if ($m_sku == $sku)
{
$mailer = false; // == is for comparison, = is for assign
}
}
if ($mailer === false)
{
echo $m_sku;
echo "<br>";
}
if($member == 'false'){...}
when 'false' is compared with == to a falsefull value (0, null, false, array(), '') .. it will NOT be mach it as it is parsed as a "not empty string" so it is not falsefull .
Here is your code with the MySQL connect moved outside the loop and the == assignment fixed.
<?php
//Connect to databases
$oracle = oci_connect($user, $pswd, $hst);
$mysql = mysql_connect("[database host]", "[database user]", "[database password]");
mysql_select_db("[database_name]");
//Get rows from Oracle
$oracle_result = oci_parse($oracle, "[Really long Oracle Query]");
$oracle_rows = array();
while ($row = oci_fetch_assoc($oracle_result)) $oracle_rows[] = $row;
//Get rows from MySQL
$mysql_sql = "SELECT * FROM [database_name] WHERE this_date_column BETWEEN
DATE_SUB(NOW(), INTERVAL 14 DAY) AND NOW()";
$mysql_result = mysql_query($mysql_sql);
$mysql_rows = array();
while ($row = mysql_fetch_assoc($mysql_result)) $mysql_rows[] = $row;
foreach ($oracle_rows as $oracle_row) {
$oracle_sku = $oracle_row['SKU'];
foreach ($mysql_rows as $mysql_row) {
$mysql_sku = $mysql_row['sku'];
$mailer = null;
if ($mysql_sku == $oracle_sku) {
$mailer = false;
echo $mysql_sku . "<br>";
}
}
}
Need a little help, advise, or link to an example or useful tutorial so I can learn this. As I am very new to programming in general.
I have 11 Select boxes in a form named 'ore1' thru 'ore11' that send data up to $_POST.
This little block turns that into an array that is used in another function.
//function calculateValue($sellValue){ -- add when you figure it out
if(isset($_POST['submit'])){
$i = 11 //total number of select boxes
$pdquery = "SELECT * FROM ore
WHERE '".$_POST['ore1']."'
LIKE id";
$result = mysql_query($pdquery);
$oredata1 = array();
while ($row = mysql_fetch_row($result)) {
$oredata1 = $row; }
}
else {}
}
I'd like like to be able to use this one bit of code with all 11 Select boxes without having to copy it 11 times by getting
.$_POST['ore#']. //5th line
$oredata# = $row; //10th line
to replace # with the appropriate # depending on the select box it is used on.
Any tips?? Thanks in advance.
In your HTML:
<select name="ore[]">
...
</select>
<select name="ore[]">
...
</select>
In your PHP
if(isset($_POST['submit']) && isset($_POST['ore'])){
foreach($_POST['ore'] as $ore){
$ore = (int)$ore;
$pdquery = "SELECT * FROM ore WHERE id = $ore";
...
}
}
Also do not use mysql_* function since deprecation process has begun
for ($i = 1; $i <= 11; $i++) {
$ore = 'ore'.$i;
$pdquery = "SELECT * FROM ore WHERE '".$_POST[$ore]."' like id";
...
execute it in a for-loop - from 1 to 11. Then you can use the variable $i to access stuff.
// I assume that there is some MySQLi object called $mysqli already present. You'll have to create it if it does not
if(isset($_POST['submit'])){
$pdquery = 'SELECT * FROM ore WHERE id LIKE ?';
$oredata = array();
if($stmt = $mysqli->prepare($pdquery)) {
for($i = 1; $i < 12; $i++) {
$ore = 'ore' . $i;
if(empty($_POST[$ore])) {
continue;
}
$oreValue = (int)$_POST[$ore];
$stmt->bind_Param('i', $oreValue);
$stmt->execute();
$oredata[$i] = $stmt->get_result()->fetch_array(MYSQLI_ASSOC);
}
}
$stmt->close();
}
This does NOT take into consideration the fact that there might be more than just those eleven inputs. Using an array on HTML side would certainly be cleaner (And you'd just have to replace the for-loop with a foreach-loop and use the key instead of $ore)