php mysqli error on some browsers only - php

I am getting this error on only some browsers and I am not sure why. I am hoping this is a simple fix. It sounds like it should be. Here is the error and below that is the code.
warning : mysqli_fetch_array expects parameters 1 to mysqli_result, boolean given in /home/content/yada/html/myapp/main.php on line 71
By the way, this is line 71:
while($row = mysqli_fetch_array($result))
and below is the full code
$type = $_POST[type];
$user="theUser";
$password="thePassword";
$database="theDatabase";
$TABLE = "user";
#mysql_connect("mydb.com",$user,$password);
#mysql_select_db($database) or die("Unable to select database");
if($_POST[type]) {
$query = "UPDATE $TABLE
SET type = $type
WHERE fbId = $id";
if(mysql_query($query)) {
//echo "Settings saved successfully!";
} else {
echo ("MySQL Error: ".mysql_error());
}
}
$con=mysqli_connect('localhost',"$user","$password","$database");
// Check connection
if(mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM $TABLE WHERE fbID = $id");
while($row = mysqli_fetch_array($result)) {
$currentType = $row['type'];
//echo $currentType;
}
if ($result = mysqli_query($con, "SELECT * FROM $TABLE WHERE fbID = $id", MYSQLI_USE_RESULT)) {
//echo "True";
//mysqli_free_result($result);
}

Use
echo mysqli_error($con);
to show the error MySQL server gives when executing the SQL query on line 71. This will reveal what is wrong with the query.

Related

mysqli_free_result() expects parameter 1 to be mysqli_result, null given in

I just migrated a site from one domain to another domain (and another host). I made sure that all links were replaced and exported/imported my database. The migration seemed to have worked, but for some reason I am getting an error on my new domain that I did not get on my old domain (with, as far as I know, the same code).
My error is: "Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, null given in path on line 84". I looked at the other StackOverflow questions that address this error, but I have not found a solution yet.
This is my code:
<?php
session_start();
// 1. Create a database connection
$dbhost =
$dbuser =
$dbpass =
$dbname =
$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
// Test if connection occurred.
if(mysqli_connect_errno()) {
die("Database connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
// 2. Perform database query
if (empty($_SESSION['order'])) {
$query = "INSERT INTO `orders` (`order_id`) VALUES (NULL)";
$result = mysqli_query($connection, $query);
// Test if there was a query error
if (!$result) {
die("Database query failed.");
}
// 3. Use returned data (if any)
$order_id_recent = mysqli_insert_id($connection);
$_SESSION['order'] = $order_id_recent;
}
$size = $_POST["size"];
$paper = $_POST["paper"];
$type = $_POST["type"];
$quantity = $_POST["quantity"];
// 2. Perform database query
$query2 = "SELECT product_id FROM product WHERE product_type = '$type' AND size = '$size' AND paper = '$paper'";
$result2 = mysqli_query($connection, $query2);
// Test if there was a query error
if (!$result2) {
die("Database query failed.");
}
// 3. Use returned data (if any)
while($row = mysqli_fetch_assoc($result2)) {
$product_id = $row['product_id'];
}
$order_id = $_SESSION['order'];
// 2. Perform database query
$order_id = $_SESSION['order'];
$query3 = "SELECT * FROM order_item WHERE order_id = '$order_id' AND product_id = '$product_id'";
$result3 = mysqli_query($connection, $query3);
// Test if there was a query error
if (!$result3) {
die("Database query failed.");
}
while($row = mysqli_fetch_assoc($result3)) {
$itemexistrows = mysqli_num_rows($result3);
}
if ($itemexistrows > 0) {
$query4 = "UPDATE order_item SET quantity = quantity + '$quantity' WHERE product_id = '$product_id' AND order_id = '$order_id'";
$result4 = mysqli_query($connection, $query4);
if (!$result4) {
die("Database query failed.");
} else {
echo 'The item has been added to your cart. <a class="text-red" href="viewcart.php">View your cart</a></div>.';
}
} else {
$query5 = "INSERT INTO `order_item`(`order_item_id`, `product_id`, `quantity`,`order_id`) VALUES (NULL,'$product_id','$quantity','$order_id')";
$result5 = mysqli_query($connection, $query5);
if (!$result5) {
die("Database query failed.");
} else {
echo 'The item has been added to your cart. <a class="text-red" href="viewcart.php">View your cart</a></div>.';
}
}
// 4. Release returned data
mysqli_free_result($result);
// 5. Close database connection
mysqli_close($connection);
?>
The weird thing is that my website still seems to work. These lines of code are part of a shopping cart module and the shopping cart seems to get updated.
I suspect that $_SESSION['order'] is not empty and thus $result is not set as it does not seem to be addressed inside the same if statement.
The following would almost certainly make the problem go away.
if(isset($result) && $result!=null){
// 4. Release returned data
mysqli_free_result($result);
}
Or, as suggested in comments, and probably better:
if(isset($result) && is_resource($result)){
// 4. Release returned data
mysqli_free_result($result);
}

Getting Data From Multiple MySQL Tables Using PHP and mysqli

I am trying to draw data from multiple tables that have been indexed to relate to one another. I ran this query in MySQLWorkbench, and it ran successfully. However when I tried to run a PHP test, nothing showed up, not even for the first field. Here is my code:
<?php
$db = new mysqli('host', 'user', 'password', 'database');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$query = "
SELECT
`Contact`.`firstName`,
`Contact`.`lastName`,
`ssn`.`ssn`,
`Contact`.`country`,
`Allergies`.`allergy`,
`Allergies`.`allergyType`,
`Allergies_Contact`.`allergyNotes`,
`CurrentPrescriptions`.`prescriptionName`,
`CurrentPrescriptions`.`prescribedDate`,
`BloodType`.`bloodType`
FROM
`database`.`Contact`,
`database`.`Allergies_Contact`,
`database`.`Allergies`,
`database`.`ssn`,
`database`.`CurrentPrescriptions`,
`database`.`BloodType`
WHERE
`Contact`.`contactKey` = `Allergies_Contact`.`contactKey`
AND `Allergies`.`allergiesKey` = `Allergies_Contact`.`allergiesKey`
AND `ssn`.`contactKey` = `Contact`.`contactKey`
AND `CurrentPrescriptions`.`contactKey` = `Contact`.`contactKey`
AND `BloodType`.`contactKey` = `Contact`.`contactKey`;
";
$result = $db->query($query) or die($db->error.__LINE__);
if ($result = mysqli_query($db, $query)) {
while ($row = mysqli_fetch_row($result)) {
print(row[0]);
}
mysqli_free_result($result);
}
mysqli_close($db);
?>
Please tell me what I am doing wrong here, because from what I can see its formatted correctly.
Several things:
1.- You have two query sentences, change:
$result = $db->query($query) or die($db->error.__LINE__);
if ($result = mysqli_query($db, $query)) {
With this
$result = $db->query($query) or die($db->error.__LINE__);
if ($result !== false) {
2.- Yo made a mistake when trying to print the variable, change:
while ($row = mysqli_fetch_row($result)) {
print(row[0]);
}
With this
while ($row = mysqli_fetch_row($result)) {
print($row[0]); // You missed a $
}
<?php
//conection:
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
//consultation:
$query = "SELECT name FROM mytable" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = $link->query($query);
//display information:
while($row = mysqli_fetch_array($result)) {
echo $row["name"] . "<br>";
}
?>
http://php.net/manual/en/function.mysqli-connect.php

Errors when adding data to a database with PHP

I have to make a web app that gets information from my database, that gets its info from an API). Then I have to show items under certain conditions.
But when I try to add the data from the API, I got a strange message:
Notice: Trying to get property of non-object in c:\xampp\htdocs\IMP03\inleveropdracht3\libs\php\function.php on line 21
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\IMP03\inleveropdracht3\libs\php\function.php on line 21
Here is my PHP code:
<?php
require_once 'settings.php';
$mysqli = mysqli_connect($db_host, $db_user, $db_password, $db_database);
if (mysqli_connect_error()) {
echo mysqli_connect_error($mysqli) . "We are not able to connect to the online database";
}
jsondecode($mysqli);
if (isset($_GET['club']) && !empty($_GET['club'])) {
jsondecode($mysqli);
} else if (isset($_GET['thuisPoint']) && !empty($_GET['thuisPoint']) && ($_GET['uitPoint']) && ($_GET['uitPoint'])) {
updatePoints($mysqli);
} else {
getWedstrijd($mysqli);
}
function jsondecode($mysqli) {
$apiLink = 'http://docent.cmi.hr.nl/moora/imp03/api/wedstrijden?club=';
// $club = $_GET['club'];
$data = json_decode(file_get_contents($apiLink . "Ajax"));
foreach ($data->data as $info) {
$thuisClub = $info->homeClub;
$uitClub = $info->awayClub;
addWestrijden($mysqli, $thuisClub, $uitClub);
}
}
//querys
function addWestrijden($mysqli, $thuisClub, $uitClub) {
$query = "INSERT INTO wedstrijd VALUES(null, '$thuisClub', '$uitClub')";
$resultAddWedstrijd = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
getWedstrijd($mysqli);
}
function getWedstrijd($mysqli) {
$query = "SELECT * FROM wedstrijd ORDER BY thuisClub DESC";
$resultGetWedstijd = mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
while ($result = mysqli_fetch_assoc($resultGetWedstijd)) {
$rows [] = $result;
}
header("Content-Type: application/json");
echo json_encode($rows);
exit;
}
function updatePoints($mysqli) {
$id = $_GET['id'];
$thuisPoints = $_GET['thuisPoint'];
$uitPoints = $_GET['uitPoint'];
$query = "UPDATE wedstrijd "
. "SET thuisPunt = '$thuisPoints', uitPunt = '$uitPoints') "
. "WHERE id = '$id'";
mysqli_query($mysqli, $query) or die(mysqli_error($mysqli));
getWedstrijd($mysqli);
}
I did modify it a bit so it would add data from the API. I really would appreciate it if someone could help me.
Change your foreach to:
foreach ($data as $data => $info)

PHP Mysql script error

I send data too this script from a C# script inside of Unity 3d . What I want to do is get the page to display the result of whatever query i end up running from it , and inside of Unity I get the text of that page and display it .
This is a very jerryridged way of doing this , but i'm just starting with PHP and mysql
Here's the error that unity 3d gives me
"Warning: mysql_result(): supplied argument is not a valid MySQL result resource in WEBSITE.com/newget.php on line 30"
<?php
$db = mysql_connect('host, user, pass') or die('Could not connect: ' . mysql_error());
mysql_select_db('dbname') or die('Could not select database');
$gotString = mysql_real_escape_string($_GET['GetString'], $db);
$hash = $_GET['hash'];
//SELECT * FROM table_name
$real_hash = md5($gotString . $secretKey);
$secretKey = "KeyHERE";
$real_hash = md5($gotString . $secretKey);
$locString = "SELECT A FROM Quiz1 WHERE Question = 1";
if ($real_hash == $hash) {
Compare();
}
function Compare() {
if ($gotString == "1B") {
$result = mysql_query("SELECT B FROM Quiz1 WHERE Question = 1") or die(mysql_error());
} else {
$result = mysql_query("SELECT A FROM Quiz1 WHERE Question = 1") or die(mysql_error());
}
}
print( $result);
?>
Try use
if (!$result) {
die('error: ' . mysql_error());
} else {
$row = mysql_fetch_array($result);
var_dump($row);
}
instead of Print( $result) ; at the end

mysql_fetch_array not working

mysql_fetch_array not working in my code :- I got an error like this ...
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Program Files\xampp\htdocs\finalreports\generatereport.php on line 38
My code is So far ....
if(array_key_exists('server_guid',$_GET))
{
$guids = $_GET['server_guid'];
$guid_array = explode(",",$guids);
//$reporttype = "Server Resources";
for($i=0 ; $i<count($guid_array); $i++)
{
$query = "select vid from vendor_registration where bussname='".$guid_array[$i]."'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$name_array[$i] = $row[0];
}
}
}
Make sure the result is not tainted:
$result = mysql_query($query) or die(mysql_error());
Your query may be returning an error:
if (($result = mysql_query($query)) === false) {
echo "Error running query: " . mysql_error() . "\n";
}

Categories